BinaryProperty.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 BinaryProperty.h
  16. */
  17. #ifndef _FASTDDS_RTPS_COMMON_BINARYPROPERTY_H_
  18. #define _FASTDDS_RTPS_COMMON_BINARYPROPERTY_H_
  19. #include <string>
  20. #include <vector>
  21. #include <iostream>
  22. #include <cstdint>
  23. namespace eprosima {
  24. namespace fastrtps {
  25. namespace rtps {
  26. class BinaryProperty
  27. {
  28. public:
  29. BinaryProperty() : propagate_(false) {}
  30. BinaryProperty(const BinaryProperty& property) :
  31. name_(property.name_),
  32. value_(property.value_),
  33. propagate_(property.propagate_) {}
  34. BinaryProperty(BinaryProperty&& property) :
  35. name_(std::move(property.name_)),
  36. value_(std::move(property.value_)),
  37. propagate_(property.propagate_) {}
  38. BinaryProperty(const std::string& name,
  39. const std::vector<uint8_t>& value) :
  40. name_(name), value_(value) {}
  41. BinaryProperty(std::string&& name,
  42. std::vector<uint8_t>&& value) :
  43. name_(std::move(name)), value_(std::move(value)) {}
  44. BinaryProperty& operator=(const BinaryProperty& property)
  45. {
  46. name_ = property.name_;
  47. value_ = property.value_;
  48. propagate_ = property.propagate_;
  49. return *this;
  50. }
  51. BinaryProperty& operator=(BinaryProperty&& property)
  52. {
  53. name_ = std::move(property.name_);
  54. value_ = std::move(property.value_);
  55. propagate_ = property.propagate_;
  56. return *this;
  57. }
  58. bool operator==(const BinaryProperty& b) const
  59. {
  60. return (this->name_ == b.name_) &&
  61. (this->value_ == b.value_);
  62. }
  63. void name(const std::string& name)
  64. {
  65. name_ = name;
  66. }
  67. void name(std::string&& name)
  68. {
  69. name_ = std::move(name);
  70. }
  71. const std::string& name() const
  72. {
  73. return name_;
  74. }
  75. std::string& name()
  76. {
  77. return name_;
  78. }
  79. void value(const std::vector<uint8_t>& value)
  80. {
  81. value_ = value;
  82. }
  83. void value(std::vector<uint8_t>&& value)
  84. {
  85. value_ = std::move(value);
  86. }
  87. const std::vector<uint8_t>& value() const
  88. {
  89. return value_;
  90. }
  91. std::vector<uint8_t>& value()
  92. {
  93. return value_;
  94. }
  95. void propagate(bool propagate)
  96. {
  97. propagate_ = propagate;
  98. }
  99. bool propagate() const
  100. {
  101. return propagate_;
  102. }
  103. bool& propagate()
  104. {
  105. return propagate_;
  106. }
  107. private:
  108. std::string name_;
  109. std::vector<uint8_t> value_;
  110. bool propagate_;
  111. };
  112. typedef std::vector<BinaryProperty> BinaryPropertySeq;
  113. class BinaryPropertyHelper
  114. {
  115. public:
  116. static size_t serialized_size(const BinaryProperty& binary_property, size_t current_alignment = 0)
  117. {
  118. if(binary_property.propagate())
  119. {
  120. size_t initial_alignment = current_alignment;
  121. current_alignment += 4 + alignment(current_alignment, 4) + binary_property.name().size() + 1;
  122. current_alignment += 4 + alignment(current_alignment, 4) + binary_property.value().size();
  123. return current_alignment - initial_alignment;
  124. }
  125. else
  126. return 0;
  127. }
  128. static size_t serialized_size(const BinaryPropertySeq& binary_properties, size_t current_alignment = 0)
  129. {
  130. size_t initial_alignment = current_alignment;
  131. current_alignment += 4 + alignment(current_alignment, 4);
  132. for(auto binary_property = binary_properties.begin(); binary_property != binary_properties.end(); ++binary_property)
  133. current_alignment += serialized_size(*binary_property, current_alignment);
  134. return current_alignment - initial_alignment;
  135. }
  136. private:
  137. inline static size_t alignment(size_t current_alignment, size_t dataSize) { return (dataSize - (current_alignment % dataSize)) & (dataSize-1);}
  138. };
  139. } //namespace rtps
  140. } //namespace fastrtps
  141. } //namespace eprosima
  142. #endif // _FASTDDS_RTPS_COMMON_BINARYPROPERTY_H_