Fast DDS  Version 3.1.0
Fast DDS
Loading...
Searching...
No Matches
Property.hpp
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
18#ifndef FASTDDS_RTPS_COMMON__PROPERTY_HPP
19#define FASTDDS_RTPS_COMMON__PROPERTY_HPP
20
21#include <functional>
22#include <stdexcept>
23#include <string>
24#include <vector>
25
26namespace eprosima {
27namespace fastdds {
28namespace rtps {
29
31{
32public:
33
35 {
36 }
37
39 const Property& property)
40 : name_(property.name_)
41 , value_(property.value_)
42 , propagate_(property.propagate_)
43 {
44 }
45
47 Property&& property)
48 : name_(std::move(property.name_))
49 , value_(std::move(property.value_))
50 , propagate_(property.propagate_)
51 {
52 }
53
55 const std::string& name,
56 const std::string& value,
57 bool propagate = false)
58 : name_(name)
59 , value_(value)
60 , propagate_(propagate)
61 {
62 }
63
65 std::string&& name,
66 std::string&& value,
67 bool propagate = false)
68 : name_(std::move(name))
69 , value_(std::move(value))
70 , propagate_(propagate)
71 {
72 }
73
75 const Property& property)
76 {
77 name_ = property.name_;
78 value_ = property.value_;
79 propagate_ = property.propagate_;
80 return *this;
81 }
82
84 Property&& property)
85 {
86 name_ = std::move(property.name_);
87 value_ = std::move(property.value_);
88 propagate_ = property.propagate_;
89 return *this;
90 }
91
93 const Property& b) const
94 {
95 return (this->name_ == b.name_) &&
96 (this->value_ == b.value_);
97 }
98
99 void name(
100 const std::string& name)
101 {
102 name_ = name;
103 }
104
105 void name(
106 std::string&& name)
107 {
108 name_ = std::move(name);
109 }
110
111 const std::string& name() const
112 {
113 return name_;
114 }
115
116 std::string& name()
117 {
118 return name_;
119 }
120
121 void value(
122 const std::string& value)
123 {
124 value_ = value;
125 }
126
127 void value(
128 std::string&& value)
129 {
130 value_ = std::move(value);
131 }
132
133 const std::string& value() const
134 {
135 return value_;
136 }
137
138 std::string& value()
139 {
140 return value_;
141 }
142
144 bool propagate)
145 {
146 propagate_ = propagate;
147 }
148
149 bool propagate() const
150 {
151 return propagate_;
152 }
153
154 bool& propagate()
155 {
156 return propagate_;
157 }
158
159private:
160
161 std::string name_;
162
163 std::string value_;
164
165 bool propagate_ = false;
166};
167
168typedef std::vector<Property> PropertySeq;
169
171{
172public:
173
174 static size_t serialized_size(
175 const Property& property,
176 size_t current_alignment = 0)
177 {
178 if (property.propagate())
179 {
180 size_t initial_alignment = current_alignment;
181
182 current_alignment += 4 + alignment(current_alignment, 4) + property.name().size() + 1;
183 current_alignment += 4 + alignment(current_alignment, 4) + property.value().size() + 1;
184
185 return current_alignment - initial_alignment;
186 }
187 else
188 {
189 return 0;
190 }
191 }
192
193 static size_t serialized_size(
194 const PropertySeq& properties,
195 size_t current_alignment = 0)
196 {
197 size_t initial_alignment = current_alignment;
198
199 current_alignment += 4 + alignment(current_alignment, 4);
200 for (auto property = properties.begin(); property != properties.end(); ++property)
201 {
202 current_alignment += serialized_size(*property, current_alignment);
203 }
204
205 return current_alignment - initial_alignment;
206 }
207
208private:
209
210 inline static size_t alignment(
211 size_t current_alignment,
212 size_t dataSize)
213 {
214 return (dataSize - (current_alignment % dataSize)) & (dataSize - 1);
215 }
216
217};
218
220{
221
235 template<typename exception_t>
236 inline static int as_int(
237 const Property& property,
238 const bool& check_upper_bound,
239 const int& upper_bound,
240 const bool& check_lower_bound,
241 const int& lower_bound,
242 const exception_t& exception)
243 {
244 return parse_value(
245 std::function<int(const Property& property)>(
246 [](const Property& property)
247 {
248 return std::stoi(property.value());
249 }
250 ),
251 property,
252 check_upper_bound,
253 upper_bound,
254 check_lower_bound,
255 lower_bound,
256 exception);
257 }
258
272 template<typename exception_t>
273 inline static double as_double(
274 const Property& property,
275 const bool& check_upper_bound,
276 const double& upper_bound,
277 const bool& check_lower_bound,
278 const double& lower_bound,
279 const exception_t& exception)
280 {
281 return parse_value(
282 std::function<double(const Property& property)>(
283 [](const Property& property)
284 {
285 return std::stod(property.value());
286 }
287 ),
288 property,
289 check_upper_bound,
290 upper_bound,
291 check_lower_bound,
292 lower_bound,
293 exception);
294 }
295
296private:
297
298 template <typename value_t,
299 typename exception_t>
300 inline static value_t parse_value(
301 const std::function<value_t(const Property&)>& conversor,
302 const Property& property,
303 const bool& check_upper_bound,
304 const value_t& upper_bound,
305 const bool& check_lower_bound,
306 const value_t& lower_bound,
307 const exception_t& exception)
308 {
309 try
310 {
311 value_t converted_value = conversor(property);
312
313 if (check_lower_bound && converted_value < lower_bound)
314 {
315 throw exception_t("Value '" + property.value() +
316 "' for " + property.name() + " must be greater or equal to " +
317 std::to_string(lower_bound));
318 }
319
320 if (check_upper_bound && converted_value > upper_bound)
321 {
322 throw exception_t("Value '" + property.value() +
323 "' for " + property.name() + " must be lower or equal to " +
324 std::to_string(upper_bound));
325 }
326
327 return converted_value;
328 }
329 catch (const std::invalid_argument&)
330 {
331 throw exception;
332 }
333 catch (const std::out_of_range&)
334 {
335 throw exception;
336 }
337 }
338
339};
340
341} //namespace eprosima
342} //namespace fastdds
343} //namespace rtps
344
345#endif // FASTDDS_RTPS_COMMON__PROPERTY_HPP
Definition Property.hpp:171
static size_t serialized_size(const PropertySeq &properties, size_t current_alignment=0)
Definition Property.hpp:193
static size_t serialized_size(const Property &property, size_t current_alignment=0)
Definition Property.hpp:174
Definition Property.hpp:31
Property & operator=(const Property &property)
Definition Property.hpp:74
void name(std::string &&name)
Definition Property.hpp:105
const std::string & name() const
Definition Property.hpp:111
Property(std::string &&name, std::string &&value, bool propagate=false)
Definition Property.hpp:64
void value(const std::string &value)
Definition Property.hpp:121
std::string & value()
Definition Property.hpp:138
Property()
Definition Property.hpp:34
const std::string & value() const
Definition Property.hpp:133
Property(const std::string &name, const std::string &value, bool propagate=false)
Definition Property.hpp:54
std::string & name()
Definition Property.hpp:116
void value(std::string &&value)
Definition Property.hpp:127
bool & propagate()
Definition Property.hpp:154
Property(const Property &property)
Definition Property.hpp:38
void name(const std::string &name)
Definition Property.hpp:99
Property(Property &&property)
Definition Property.hpp:46
bool propagate() const
Definition Property.hpp:149
void propagate(bool propagate)
Definition Property.hpp:143
bool operator==(const Property &b) const
Definition Property.hpp:92
std::vector< Property > PropertySeq
Definition Property.hpp:168
eProsima namespace.
Definition EntityId_t.hpp:388
Definition Property.hpp:220
static double as_double(const Property &property, const bool &check_upper_bound, const double &upper_bound, const bool &check_lower_bound, const double &lower_bound, const exception_t &exception)
Parse a property value as a double.
Definition Property.hpp:273
static int as_int(const Property &property, const bool &check_upper_bound, const int &upper_bound, const bool &check_lower_bound, const int &lower_bound, const exception_t &exception)
Parse a property value as an integer.
Definition Property.hpp:236