ADTF File Library
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};
233} // namespace detail
234
243template<typename T>
244T getPropertyValue(const Configuration& configuration, const std::string& property_name)
245{
246 try
247 {
248 auto const& property_value = configuration.at(property_name);
249 if constexpr (std::is_arithmetic<T>::value)
250 {
251 if (property_value.type == "bool")
252 {
253 return (property_value.value == "true") ? 1 : 0;
254 }
255 }
256 return detail::propertyValueAs<T>(property_value.value);
257 }
258 catch (...)
259 {
260 std::throw_with_nested(std::runtime_error("cannot get property value of '" + property_name));
261 }
262}
263
274template<typename T>
275T getPropertyValue(const Configuration& configuration, const std::string& property_name, const T& default_value)
276{
277 try
278 {
279 const auto& found = configuration.find(property_name);
280 if (found != configuration.end())
281 {
282 if constexpr (std::is_arithmetic<T>::value)
283 {
284 if (found->second.type == "bool")
285 {
286 return (found->second.value == "true") ? 1 : 0;
287 }
288 }
289 return detail::propertyValueAs<T>(found->second.value);
290 }
291 else
292 {
293 return default_value;
294 }
295 }
296 catch (...)
297 {
298 std::throw_with_nested(std::runtime_error("cannot get property value of '" + property_name));
299 }
300}
301
312 const std::string& property_name,
313 bool top_level_reserved_properties = false)
314{
315 const auto prefix = property_name + "/";
316 Configuration sub_configuration;
317 for (const auto& property : configuration)
318 {
319 if (property.first.find(prefix) == 0)
320 {
321 const auto name = property.first.substr(prefix.size());
322 if (top_level_reserved_properties || name.find(detail::ReservedProperties::reserved_prefix) != 0)
323 {
324 sub_configuration.try_emplace(name, property.second);
325 }
326 }
327 }
328 return sub_configuration;
329}
330
338template<typename T>
339void setPropertyValue(Configuration& configuration, const std::string& property_name, const T& property_value)
340{
341 try
342 {
343 configuration.insert_or_assign(property_name, PropertyValue(property_value));
344 }
345 catch (...)
346 {
347 std::throw_with_nested(std::runtime_error("cannot set property value of '" + property_name));
348 }
349}
350
356inline void setFilePluginVersion(Configuration& configuration, const std::string& version)
357{
358 setPropertyValue(configuration, detail::ReservedProperties::version, version);
359}
360
366inline void setFilePluginDescription(Configuration& configuration, const std::string& description)
367{
368 setPropertyValue(configuration, detail::ReservedProperties::description, description);
369}
370
377inline void setPropertyDescription(Configuration& configuration,
378 const std::string& property_name,
379 const std::string& description)
380{
381 setPropertyValue(configuration, property_name + "/" + detail::ReservedProperties::description, description);
382}
383
390inline void setPropertyDisplayName(Configuration& configuration,
391 const std::string& property_name,
392 const std::string& display_name)
393{
394 setPropertyValue(configuration, property_name + "/" + detail::ReservedProperties::display_name, display_name);
395}
396
405template<typename T>
406void setPropertyValueRange(Configuration& configuration, const std::string& property_name, const T& min, const T& max)
407{
408 setPropertyValue<T>(configuration, property_name + "/" + detail::ReservedProperties::range_min, min);
409 setPropertyValue<T>(configuration, property_name + "/" + detail::ReservedProperties::range_max, max);
410}
411
420template<typename T>
422 const std::string& property_name,
423 const std::vector<std::pair<T, std::string>>& value_list,
424 bool restrict_to_values = true)
425{
426 setPropertyValue<unsigned int>(configuration, property_name + "/" + detail::ReservedProperties::value_list_count,
427 static_cast<unsigned int>(value_list.size()));
428 setPropertyValue<bool>(configuration, property_name + "/" + detail::ReservedProperties::value_list_restricted,
429 restrict_to_values);
430
431 size_t index = 0;
432 for (const auto& value : value_list)
433 {
434 setPropertyValue<T>(configuration,
435 property_name + "/" + detail::ReservedProperties::value_list_value + std::to_string(index),
436 value.first);
437 setPropertyValue(configuration,
438 property_name + "/" + detail::ReservedProperties::value_list_name + std::to_string(index),
439 value.second);
440 ++index;
441 }
442}
443
448{
449public:
453 virtual ~Configurable() = default;
457 virtual const Configuration& getConfiguration() const
458 {
459 return _configuration;
460 }
461
466 virtual void setConfiguration(const Configuration& configuration)
467 {
468 _configuration = _configuration + configuration;
469 }
470
471private:
472 Configuration _configuration;
473};
474
480template<typename T = std::string>
482{
483public:
491 PropertyBuilder(std::string name, T default_value):
492 Configuration{{name, PropertyValue{std::move(default_value)}}}, name{std::move(name)}
493 {
494 }
495
501 PropertyBuilder& setDescription(const std::string& description)
502 {
503 setPropertyDescription(*this, name, description);
504 return *this;
505 }
506
513 PropertyBuilder& setDisplayName(const std::string& display_name)
514 {
515 setPropertyDisplayName(*this, name, display_name);
516 return *this;
517 }
518
526 PropertyBuilder& setValueList(const std::vector<std::pair<T, std::string>>& value_list, bool restrict_to_values)
527 {
528 setPropertyValueList(*this, name, value_list, restrict_to_values);
529 return *this;
530 }
531
538 PropertyBuilder& setMinMax(const T& min, const T& max)
539 {
540 setPropertyValueRange(*this, name, min, max);
541 return *this;
542 }
543
544private:
545 std::string name;
546};
547
551#define FILE_PLUGIN_META_INFORMATION(version, description) \
552 { \
553 adtf_file::Configuration configuration; \
554 setFilePluginVersion(configuration, version); \
555 setFilePluginDescription(configuration, description); \
556 setConfiguration(configuration); \
557 }
558
559} // namespace adtf_file
Definition configuration.h:448
virtual ~Configurable()=default
DTOR.
virtual void setConfiguration(const Configuration &configuration)
Definition configuration.h:466
virtual const Configuration & getConfiguration() const
Definition configuration.h:457
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:491
PropertyBuilder & setMinMax(const T &min, const T &max)
Sets a minimum/maximum range for the property value.
Definition configuration.h:538
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:526
PropertyBuilder & setDescription(const std::string &description)
Sets a description for the property.
Definition configuration.h:501
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:513
namespace for ADTF File library
Definition adtf2_adtf_core_media_sample_deserializer.h:25
void setPropertyValueRange(Configuration &configuration, const std::string &property_name, const T &min, const T &max)
Definition configuration.h:406
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:421
void setFilePluginVersion(Configuration &configuration, const std::string &version)
Definition configuration.h:356
bool operator==(const PropertyValue &left, const PropertyValue &right)
compare operator for PropertyValue
Definition configuration.h:123
void setPropertyDisplayName(Configuration &configuration, const std::string &property_name, const std::string &display_name)
Definition configuration.h:390
void setPropertyValue(Configuration &configuration, const std::string &property_name, const T &property_value)
Definition configuration.h:339
void setPropertyDescription(Configuration &configuration, const std::string &property_name, const std::string &description)
Definition configuration.h:377
T getPropertyValue(const Configuration &configuration, const std::string &property_name)
Definition configuration.h:244
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:311
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:366
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