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