/src/Fast-DDS/include/fastdds/rtps/common/BinaryProperty.h
Line | Count | Source (jump to first uncovered line) |
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 | | /*! |
16 | | * @file BinaryProperty.h |
17 | | */ |
18 | | |
19 | | #ifndef _FASTDDS_RTPS_COMMON_BINARYPROPERTY_H_ |
20 | | #define _FASTDDS_RTPS_COMMON_BINARYPROPERTY_H_ |
21 | | |
22 | | #include <string> |
23 | | #include <vector> |
24 | | #include <iostream> |
25 | | #include <cstdint> |
26 | | |
27 | | namespace eprosima { |
28 | | namespace fastrtps { |
29 | | namespace rtps { |
30 | | |
31 | | class BinaryProperty |
32 | | { |
33 | | public: |
34 | | |
35 | 0 | BinaryProperty() : propagate_(false) {} |
36 | | |
37 | | BinaryProperty(const BinaryProperty& property) : |
38 | | name_(property.name_), |
39 | | value_(property.value_), |
40 | 0 | propagate_(property.propagate_) {} |
41 | | |
42 | | BinaryProperty(BinaryProperty&& property) : |
43 | | name_(std::move(property.name_)), |
44 | | value_(std::move(property.value_)), |
45 | 0 | propagate_(property.propagate_) {} |
46 | | |
47 | | BinaryProperty(const std::string& name, |
48 | | const std::vector<uint8_t>& value) : |
49 | 0 | name_(name), value_(value) {} |
50 | | |
51 | | BinaryProperty(std::string&& name, |
52 | | std::vector<uint8_t>&& value) : |
53 | 0 | name_(std::move(name)), value_(std::move(value)) {} |
54 | | |
55 | | BinaryProperty& operator=(const BinaryProperty& property) |
56 | 0 | { |
57 | 0 | name_ = property.name_; |
58 | 0 | value_ = property.value_; |
59 | 0 | propagate_ = property.propagate_; |
60 | 0 | return *this; |
61 | 0 | } |
62 | | |
63 | | BinaryProperty& operator=(BinaryProperty&& property) |
64 | 0 | { |
65 | 0 | name_ = std::move(property.name_); |
66 | 0 | value_ = std::move(property.value_); |
67 | 0 | propagate_ = property.propagate_; |
68 | 0 | return *this; |
69 | 0 | } |
70 | | |
71 | | bool operator==(const BinaryProperty& b) const |
72 | 0 | { |
73 | 0 | return (this->name_ == b.name_) && |
74 | 0 | (this->value_ == b.value_); |
75 | 0 | } |
76 | | |
77 | | void name(const std::string& name) |
78 | 0 | { |
79 | 0 | name_ = name; |
80 | 0 | } |
81 | | |
82 | | void name(std::string&& name) |
83 | 0 | { |
84 | 0 | name_ = std::move(name); |
85 | 0 | } |
86 | | |
87 | | const std::string& name() const |
88 | 0 | { |
89 | 0 | return name_; |
90 | 0 | } |
91 | | |
92 | | std::string& name() |
93 | 0 | { |
94 | 0 | return name_; |
95 | 0 | } |
96 | | |
97 | | void value(const std::vector<uint8_t>& value) |
98 | 0 | { |
99 | 0 | value_ = value; |
100 | 0 | } |
101 | | |
102 | | void value(std::vector<uint8_t>&& value) |
103 | 0 | { |
104 | 0 | value_ = std::move(value); |
105 | 0 | } |
106 | | |
107 | | const std::vector<uint8_t>& value() const |
108 | 0 | { |
109 | 0 | return value_; |
110 | 0 | } |
111 | | |
112 | | std::vector<uint8_t>& value() |
113 | 0 | { |
114 | 0 | return value_; |
115 | 0 | } |
116 | | |
117 | | void propagate(bool propagate) |
118 | 0 | { |
119 | 0 | propagate_ = propagate; |
120 | 0 | } |
121 | | |
122 | | bool propagate() const |
123 | 0 | { |
124 | 0 | return propagate_; |
125 | 0 | } |
126 | | |
127 | | bool& propagate() |
128 | 0 | { |
129 | 0 | return propagate_; |
130 | 0 | } |
131 | | |
132 | | private: |
133 | | |
134 | | std::string name_; |
135 | | |
136 | | std::vector<uint8_t> value_; |
137 | | |
138 | | bool propagate_; |
139 | | }; |
140 | | |
141 | | typedef std::vector<BinaryProperty> BinaryPropertySeq; |
142 | | |
143 | | class BinaryPropertyHelper |
144 | | { |
145 | | public: |
146 | | |
147 | | static size_t serialized_size(const BinaryProperty& binary_property, size_t current_alignment = 0) |
148 | 0 | { |
149 | 0 | if(binary_property.propagate()) |
150 | 0 | { |
151 | 0 | size_t initial_alignment = current_alignment; |
152 | 0 |
|
153 | 0 | current_alignment += 4 + alignment(current_alignment, 4) + binary_property.name().size() + 1; |
154 | 0 | current_alignment += 4 + alignment(current_alignment, 4) + binary_property.value().size(); |
155 | 0 |
|
156 | 0 | return current_alignment - initial_alignment; |
157 | 0 | } |
158 | 0 | else |
159 | 0 | return 0; |
160 | 0 | } |
161 | | |
162 | | static size_t serialized_size(const BinaryPropertySeq& binary_properties, size_t current_alignment = 0) |
163 | 0 | { |
164 | 0 | size_t initial_alignment = current_alignment; |
165 | 0 |
|
166 | 0 | current_alignment += 4 + alignment(current_alignment, 4); |
167 | 0 | for(auto binary_property = binary_properties.begin(); binary_property != binary_properties.end(); ++binary_property) |
168 | 0 | current_alignment += serialized_size(*binary_property, current_alignment); |
169 | 0 |
|
170 | 0 | return current_alignment - initial_alignment; |
171 | 0 | } |
172 | | |
173 | | private: |
174 | | |
175 | 0 | inline static size_t alignment(size_t current_alignment, size_t dataSize) { return (dataSize - (current_alignment % dataSize)) & (dataSize-1);} |
176 | | }; |
177 | | |
178 | | } //namespace rtps |
179 | | } //namespace fastrtps |
180 | | } //namespace eprosima |
181 | | |
182 | | #endif // _FASTDDS_RTPS_COMMON_BINARYPROPERTY_H_ |