ADTF
Loading...
Searching...
No Matches
configuration.h
Go to the documentation of this file.
1
16
17#pragma once
18
19#include <string>
20#include <utility>
21#include <unordered_map>
22#include <stdexcept>
23#include <string_view>
24
25namespace adtf_file
26{
31{
35 PropertyValue() = default;
39 PropertyValue(const PropertyValue&) = default;
54
62 PropertyValue(std::string value, std::string type): value(std::move(value)), type(std::move(type))
63 {
64 }
65
70 template<typename T, std::enable_if_t<std::is_unsigned<T>::value && !std::is_same<T, bool>::value, bool> = true>
71 PropertyValue(T value): value(std::to_string(value)), type("unsigned")
72 {
73 }
74
79 template<typename T, std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, bool> = true>
80 PropertyValue(T value): value(std::to_string(value)), type("signed")
81 {
82 }
83
88 template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
89 PropertyValue(T value): value(std::to_string(value)), type("float")
90 {
91 }
92
97 template<typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
98 PropertyValue(T value): value((value ? "true" : "false")), type("bool")
99 {
100 }
101
106 PropertyValue(std::string_view string_value): value(string_value), type("string")
107 {
108 }
109
110 std::string value;
112 std::string type;
113};
114
123inline bool operator==(const PropertyValue& left, const PropertyValue& right)
124{
125 return left.type == right.type && left.value == right.value;
126}
127
132using Configuration = std::unordered_map<std::string, PropertyValue>;
133
142inline Configuration operator+(const Configuration& config_left, const Configuration& config_right)
143{
144 Configuration result_configuration(config_left);
145 for (const auto& current_right : config_right)
146 {
147 result_configuration.insert_or_assign(current_right.first, current_right.second);
148 }
149 return result_configuration;
150}
151
152namespace detail
153{
155template<typename T>
156T propertyValueAs(const std::string& /*property_value*/)
157{
158 static_assert(sizeof(T) == -1,
159 "please use the specializations for bool, int64_t, uint64_t, double and std::string.");
160}
161
162template<>
163inline bool propertyValueAs<bool>(const std::string& property_value)
164{
165 if (property_value == "true")
166 {
167 return true;
168 }
169 else if (property_value == "false")
170 {
171 return false;
172 }
173 else
174 {
175 return (std::stoll(property_value, 0, 0) != 0);
176 }
177}
178
179template<>
180inline uint64_t propertyValueAs<uint64_t>(const std::string& property_value)
181{
182 return std::stoull(property_value, 0, 0);
183}
184
185template<>
186inline int64_t propertyValueAs<int64_t>(const std::string& property_value)
187{
188 return std::stoll(property_value, 0, 0);
189}
190
191template<>
192inline uint32_t propertyValueAs<uint32_t>(const std::string& property_value)
193{
194 return std::stoul(property_value, 0, 0);
195}
196
197template<>
198inline int32_t propertyValueAs<int32_t>(const std::string& property_value)
199{
200 return std::stol(property_value, 0, 0);
201}
202
203template<>
204inline double propertyValueAs<double>(const std::string& property_value)
205{
206 return std::stod(property_value);
207}
208
209template<>
210inline std::string propertyValueAs<std::string>(const std::string& property_value)
211{
212 return property_value;
213}
214
215struct ReservedProperties
216{
217 static constexpr auto reserved_prefix = "#__";
218 static constexpr auto version = "#__version";
219 static constexpr auto display_name = "#__display_name";
220 static constexpr auto description = "#__description";
221
222 static constexpr auto range_min = "#__range_min";
223 static constexpr auto range_max = "#__range_max";
224
225 static constexpr auto value_list_count = "#__value_list_count";
226 static constexpr auto value_list_value = "#__value_list_value_";
227 static constexpr auto value_list_name = "#__value_list_name_";
228 static constexpr auto value_list_restricted = "#__value_list_restricted";
229
230 static constexpr auto default_filename_extension = "#__default_filename_extension";
231
232 static constexpr auto expected_content = "#__expected_content";
233};
234
236} // namespace detail
237
240{
241 reader_configuration
242};
243
252template<typename T>
253T getPropertyValue(const Configuration& configuration, const std::string& property_name)
254{
255 try
256 {
257 auto const& property_value = configuration.at(property_name);
258 if constexpr (std::is_arithmetic<T>::value)
259 {
260 if (property_value.type == "bool")
261 {
262 return (property_value.value == "true") ? 1 : 0;
263 }
264 }
265 return detail::propertyValueAs<T>(property_value.value);
266 }
267 catch (...)
268 {
269 std::throw_with_nested(std::runtime_error("cannot get property value of '" + property_name));
270 }
271}
272
283template<typename T>
284T getPropertyValue(const Configuration& configuration, const std::string& property_name, const T& default_value)
285{
286 try
287 {
288 const auto& found = configuration.find(property_name);
289 if (found != configuration.end())
290 {
291 if constexpr (std::is_arithmetic<T>::value)
292 {
293 if (found->second.type == "bool")
294 {
295 return (found->second.value == "true") ? 1 : 0;
296 }
297 }
298 return detail::propertyValueAs<T>(found->second.value);
299 }
300 else
301 {
302 return default_value;
303 }
304 }
305 catch (...)
306 {
307 std::throw_with_nested(std::runtime_error("cannot get property value of '" + property_name));
308 }
309}
310
321 const std::string& property_name,
322 bool top_level_reserved_properties = false)
323{
324 const auto prefix = property_name + "/";
325 Configuration sub_configuration;
326 for (const auto& property : configuration)
327 {
328 if (property.first.find(prefix) == 0)
329 {
330 const auto name = property.first.substr(prefix.size());
331 if (top_level_reserved_properties || name.find(detail::ReservedProperties::reserved_prefix) != 0)
332 {
333 sub_configuration.try_emplace(name, property.second);
334 }
335 }
336 }
337 return sub_configuration;
338}
339
347template<typename T>
348void setPropertyValue(Configuration& configuration, const std::string& property_name, const T& property_value)
349{
350 try
351 {
352 configuration.insert_or_assign(property_name, PropertyValue(property_value));
353 }
354 catch (...)
355 {
356 std::throw_with_nested(std::runtime_error("cannot set property value of '" + property_name));
357 }
358}
359
365inline void setFilePluginVersion(Configuration& configuration, const std::string& version)
366{
367 setPropertyValue(configuration, detail::ReservedProperties::version, version);
368}
369
375inline void setFilePluginDescription(Configuration& configuration, const std::string& description)
376{
377 setPropertyValue(configuration, detail::ReservedProperties::description, description);
378}
379
386inline void setPropertyDescription(Configuration& configuration,
387 const std::string& property_name,
388 const std::string& description)
389{
390 setPropertyValue(configuration, property_name + "/" + detail::ReservedProperties::description, description);
391}
392
399inline void setPropertyDisplayName(Configuration& configuration,
400 const std::string& property_name,
401 const std::string& display_name)
402{
403 setPropertyValue(configuration, property_name + "/" + detail::ReservedProperties::display_name, display_name);
404}
405
414template<typename T>
415void setPropertyValueRange(Configuration& configuration, const std::string& property_name, const T& min, const T& max)
416{
417 setPropertyValue<T>(configuration, property_name + "/" + detail::ReservedProperties::range_min, min);
418 setPropertyValue<T>(configuration, property_name + "/" + detail::ReservedProperties::range_max, max);
419}
420
429template<typename T>
430inline void setPropertyValueList(Configuration& configuration,
431 const std::string& property_name,
432 const std::vector<std::pair<T, std::string>>& value_list,
433 bool restrict_to_values = true)
434{
435 setPropertyValue<unsigned int>(configuration, property_name + "/" + detail::ReservedProperties::value_list_count,
436 static_cast<unsigned int>(value_list.size()));
437 setPropertyValue<bool>(configuration, property_name + "/" + detail::ReservedProperties::value_list_restricted,
438 restrict_to_values);
439
440 size_t index = 0;
441 for (const auto& value : value_list)
442 {
443 setPropertyValue<T>(configuration,
444 property_name + "/" + detail::ReservedProperties::value_list_value + std::to_string(index),
445 value.first);
446 setPropertyValue(configuration,
447 property_name + "/" + detail::ReservedProperties::value_list_name + std::to_string(index),
448 value.second);
449 ++index;
450 }
451}
452
459inline void setPropertyExpectedContent(Configuration& configuration,
460 const std::string& property_name,
461 ExpectedContent content)
462{
463 switch (content)
464 {
465 case ExpectedContent::reader_configuration:
466 setPropertyValue<std::string>(configuration, property_name + "/" + detail::ReservedProperties::expected_content, "reader_configuration");
467 return;
468 }
469}
470
475{
476public:
480 virtual ~Configurable() = default;
484 virtual const Configuration& getConfiguration() const
485 {
486 return _configuration;
487 }
488
493 virtual void setConfiguration(const Configuration& configuration)
494 {
495 _configuration = _configuration + configuration;
496 }
497
498private:
499 Configuration _configuration;
500};
501
507template<typename T = std::string>
509{
510public:
518 PropertyBuilder(std::string name, T default_value):
519 Configuration{{name, PropertyValue{std::move(default_value)}}}, name{std::move(name)}
520 {
521 }
522
528 PropertyBuilder& setDescription(const std::string& description)
529 {
530 setPropertyDescription(*this, name, description);
531 return *this;
532 }
533
540 PropertyBuilder& setDisplayName(const std::string& display_name)
541 {
542 setPropertyDisplayName(*this, name, display_name);
543 return *this;
544 }
545
553 PropertyBuilder& setValueList(const std::vector<std::pair<T, std::string>>& value_list, bool restrict_to_values)
554 {
555 setPropertyValueList(*this, name, value_list, restrict_to_values);
556 return *this;
557 }
558
565 PropertyBuilder& setMinMax(const T& min, const T& max)
566 {
567 setPropertyValueRange(*this, name, min, max);
568 return *this;
569 }
570
577 {
578 setPropertyExpectedContent(*this, name, content);
579 return *this;
580 }
581
582private:
583 std::string name;
584};
585
589#define FILE_PLUGIN_META_INFORMATION(version, description) \
590 { \
591 adtf_file::Configuration configuration; \
592 setFilePluginVersion(configuration, version); \
593 setFilePluginDescription(configuration, description); \
594 setConfiguration(configuration); \
595 }
596
597} // namespace adtf_file
Definition configuration.h:475
virtual ~Configurable()=default
DTOR.
virtual void setConfiguration(const Configuration &configuration)
Definition configuration.h:493
virtual const Configuration & getConfiguration() const
Definition configuration.h:484
PropertyBuilder(std::string name, T default_value)
Sets a property with name and default value. Hierarchically grouped properties are created with the '...
Definition configuration.h:518
PropertyBuilder & expectedContent(ExpectedContent content)
Defines a content for a property.
Definition configuration.h:576
PropertyBuilder & setMinMax(const T &min, const T &max)
Sets a minimum/maximum range for the property value.
Definition configuration.h:565
PropertyBuilder & setValueList(const std::vector< std::pair< T, std::string > > &value_list, bool restrict_to_values)
Sets a value list for the property.
Definition configuration.h:553
PropertyBuilder & setDescription(const std::string &description)
Sets a description for the property.
Definition configuration.h:528
PropertyBuilder & setDisplayName(const std::string &display_name)
Sets a display name for the property. Please note that '/' is used for hierarchical grouping of prope...
Definition configuration.h:540
namespace for ADTF File library
Definition adtf_file_support_intf.h:12
void setPropertyValueRange(Configuration &configuration, const std::string &property_name, const T &min, const T &max)
Definition configuration.h:415
Configuration operator+(const Configuration &config_left, const Configuration &config_right)
Add operator for configurations.
Definition configuration.h:142
void setPropertyValueList(Configuration &configuration, const std::string &property_name, const std::vector< std::pair< T, std::string > > &value_list, bool restrict_to_values=true)
Definition configuration.h:430
void setFilePluginVersion(Configuration &configuration, const std::string &version)
Definition configuration.h:365
bool operator==(const PropertyValue &left, const PropertyValue &right)
compare operator for PropertyValue
Definition configuration.h:123
void setPropertyExpectedContent(Configuration &configuration, const std::string &property_name, ExpectedContent content)
Definition configuration.h:459
void setPropertyDisplayName(Configuration &configuration, const std::string &property_name, const std::string &display_name)
Definition configuration.h:399
void setPropertyValue(Configuration &configuration, const std::string &property_name, const T &property_value)
Definition configuration.h:348
void setPropertyDescription(Configuration &configuration, const std::string &property_name, const std::string &description)
Definition configuration.h:386
T getPropertyValue(const Configuration &configuration, const std::string &property_name)
Definition configuration.h:253
ExpectedContent
Expected content types for Properties.
Definition configuration.h:240
Configuration getSubConfiguration(const Configuration &configuration, const std::string &property_name, bool top_level_reserved_properties=false)
Get a sub-tree of a configuration property.
Definition configuration.h:320
std::unordered_map< std::string, PropertyValue > Configuration
Configuration class as set of key - property value pairs This configuration is used to adjust the rea...
Definition configuration.h:132
void setFilePluginDescription(Configuration &configuration, const std::string &description)
Definition configuration.h:375
PropertyValue variant class.
Definition configuration.h:31
PropertyValue(std::string_view string_value)
CTOR for string value and "string" type.
Definition configuration.h:106
PropertyValue & operator=(const PropertyValue &)=default
copy assignment
PropertyValue(std::string value, std::string type)
CTOR.
Definition configuration.h:62
PropertyValue()=default
CTOR.
PropertyValue & operator=(PropertyValue &&)=default
move assignment
PropertyValue(PropertyValue &&)=default
move CTOR
std::string type
Type as string.
Definition configuration.h:112
PropertyValue(const PropertyValue &)=default
copy CTOR
PropertyValue(T value)
CTOR for unsigned integer value (uint8-uint64) and "unsigned" type.
Definition configuration.h:71
std::string value
Value as string.
Definition configuration.h:110