Property.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*!
  15. * @file Property.h
  16. */
  17. #ifndef _FASTDDS_RTPS_COMMON_PROPERTYQOS_H_
  18. #define _FASTDDS_RTPS_COMMON_PROPERTYQOS_H_
  19. #include <string>
  20. #include <vector>
  21. namespace eprosima {
  22. namespace fastrtps {
  23. namespace rtps {
  24. class Property
  25. {
  26. public:
  27. Property() : propagate_(false) {}
  28. Property(const Property& property) :
  29. name_(property.name_),
  30. value_(property.value_),
  31. propagate_(property.propagate_) {}
  32. Property(Property&& property) :
  33. name_(std::move(property.name_)),
  34. value_(std::move(property.value_)),
  35. propagate_(property.propagate_) {}
  36. Property(const std::string& name,
  37. const std::string& value) :
  38. name_(name), value_(value) {}
  39. Property(std::string&& name,
  40. std::string&& value) :
  41. name_(std::move(name)), value_(std::move(value)) {}
  42. Property& operator=(const Property& property)
  43. {
  44. name_ = property.name_;
  45. value_ = property.value_;
  46. propagate_ = property.propagate_;
  47. return *this;
  48. }
  49. Property& operator=(Property&& property)
  50. {
  51. name_ = std::move(property.name_);
  52. value_ = std::move(property.value_);
  53. propagate_ = property.propagate_;
  54. return *this;
  55. }
  56. bool operator==(const Property& b) const
  57. {
  58. return (this->name_ == b.name_) &&
  59. (this->value_ == b.value_);
  60. }
  61. void name(const std::string& name)
  62. {
  63. name_ = name;
  64. }
  65. void name(std::string&& name)
  66. {
  67. name_ = std::move(name);
  68. }
  69. const std::string& name() const
  70. {
  71. return name_;
  72. }
  73. std::string& name()
  74. {
  75. return name_;
  76. }
  77. void value(const std::string& value)
  78. {
  79. value_ = value;
  80. }
  81. void value(std::string&& value)
  82. {
  83. value_ = std::move(value);
  84. }
  85. const std::string& value() const
  86. {
  87. return value_;
  88. }
  89. std::string& value()
  90. {
  91. return value_;
  92. }
  93. void propagate(bool propagate)
  94. {
  95. propagate_ = propagate;
  96. }
  97. bool propagate() const
  98. {
  99. return propagate_;
  100. }
  101. bool& propagate()
  102. {
  103. return propagate_;
  104. }
  105. private:
  106. std::string name_;
  107. std::string value_;
  108. bool propagate_;
  109. };
  110. typedef std::vector<Property> PropertySeq;
  111. class PropertyHelper
  112. {
  113. public:
  114. static size_t serialized_size(const Property& property, size_t current_alignment = 0)
  115. {
  116. if(property.propagate())
  117. {
  118. size_t initial_alignment = current_alignment;
  119. current_alignment += 4 + alignment(current_alignment, 4) + property.name().size() + 1;
  120. current_alignment += 4 + alignment(current_alignment, 4) + property.value().size() + 1;
  121. return current_alignment - initial_alignment;
  122. }
  123. else
  124. return 0;
  125. }
  126. static size_t serialized_size(const PropertySeq& properties, size_t current_alignment = 0)
  127. {
  128. size_t initial_alignment = current_alignment;
  129. current_alignment += 4 + alignment(current_alignment, 4);
  130. for(auto property = properties.begin(); property != properties.end(); ++property)
  131. current_alignment += serialized_size(*property, current_alignment);
  132. return current_alignment - initial_alignment;
  133. }
  134. private:
  135. inline static size_t alignment(size_t current_alignment, size_t dataSize) { return (dataSize - (current_alignment % dataSize)) & (dataSize-1);}
  136. };
  137. } //namespace eprosima
  138. } //namespace fastrtps
  139. } //namespace rtps
  140. #endif // _FASTDDS_RTPS_COMMON_PROPERTYQOS_H_