adtf_file_library  0.13.2
configuration.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include <string>
20 #include <utility>
21 #include <unordered_map>
22 #include <stdexcept>
23 #include <string_view>
24 
25 namespace adtf_file
26 {
27 
32 {
36  PropertyValue() = default;
40  PropertyValue(const PropertyValue&) = default;
55 
63  PropertyValue(std::string value, std::string type)
64  : value(std::move(value)), type(std::move(type))
65  {}
66 
71  template <typename T,
72  std::enable_if_t<std::is_unsigned<T>::value && !std::is_same<T, bool>::value, bool> = true>
73  PropertyValue(T value) : value(std::to_string(value)),
74  type("unsigned")
75  {
76  }
77 
82  template <typename T,
83  std::enable_if_t<std::is_integral<T>::value&& std::is_signed<T>::value, bool> = true>
84  PropertyValue(T value) : value(std::to_string(value)),
85  type("signed")
86  {
87  }
88 
93  template <typename T,
94  std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
95  PropertyValue(T value) : value(std::to_string(value)),
96  type("float")
97  {
98  }
99 
104  template <typename T,
105  std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
106  PropertyValue(T value) : value((value ? "true" : "false")),
107  type("bool")
108  {
109  }
110 
115  PropertyValue(std::string_view string_value) : value(string_value),
116  type("string")
117  {
118  }
120  std::string value;
122  std::string type;
123 };
124 
133 inline bool operator==(const PropertyValue& left, const PropertyValue& right)
134 {
135  return left.type == right.type &&
136  left.value == right.value;
137 }
138 
143 using Configuration = std::unordered_map<std::string, PropertyValue>;
144 
153 inline Configuration operator+(const Configuration& config_left, const Configuration& config_right)
154 {
155  Configuration result_configuration(config_left);
156  for (const auto& current_right : config_right) {
157  result_configuration.insert_or_assign(current_right.first, current_right.second);
158  }
159  return result_configuration;
160 }
161 
162 namespace detail
163 {
165  template <typename T>
166  T propertyValueAs(const std::string& property_value)
167  {
168  static_assert(sizeof(T) == -1,
169  "please use the specializations for bool, int64_t, uint64_t, double and std::string.");
170  }
171 
172  template <>
173  inline bool propertyValueAs<bool>(const std::string& property_value)
174  {
175  if (property_value == "true")
176  {
177  return true;
178  }
179  else if (property_value == "false")
180  {
181  return false;
182  }
183  else
184  {
185  return (std::stoll(property_value, 0, 0) != 0);
186  }
187  }
188 
189  template <>
190  inline uint64_t propertyValueAs<uint64_t>(const std::string& property_value)
191  {
192  return std::stoull(property_value, 0, 0);
193  }
194 
195  template <>
196  inline int64_t propertyValueAs<int64_t>(const std::string& property_value)
197  {
198  return std::stoll(property_value, 0, 0);
199  }
200 
201  template <>
202  inline uint32_t propertyValueAs<uint32_t>(const std::string& property_value)
203  {
204  return std::stoul(property_value, 0, 0);
205  }
206 
207  template <>
208  inline int32_t propertyValueAs<int32_t>(const std::string& property_value)
209  {
210  return std::stol(property_value, 0, 0);
211  }
212 
213  template <>
214  inline double propertyValueAs<double>(const std::string& property_value)
215  {
216  return std::stod(property_value);
217  }
218 
219  template <>
220  inline std::string propertyValueAs<std::string>(const std::string& property_value)
221  {
222  return property_value;
223  }
225 }
226 
227 
236 template <typename T>
237 T getPropertyValue(const Configuration& configuration, const std::string& property_name)
238 {
239  try
240  {
241  auto const& property_value = configuration.at(property_name);
242  if constexpr(std::is_arithmetic<T>::value) {
243  if (property_value.type == "bool") {
244  return (property_value.value == "true") ? 1 : 0;
245  }
246  }
247  return detail::propertyValueAs<T>(property_value.value);
248  }
249  catch (...)
250  {
251  std::throw_with_nested(std::runtime_error("cannot set property value of '" + property_name));
252  }
253 }
254 
265 template <typename T>
266 T getPropertyValue(const Configuration& configuration, const std::string& property_name, const T& default_value)
267 {
268  try
269  {
270  const auto& found = configuration.find(property_name);
271  if (found != configuration.end()) {
272  if constexpr(std::is_arithmetic<T>::value) {
273  if (found->second.type == "bool") {
274  return (found->second.value == "true") ? 1 : 0;
275  }
276  }
277  return detail::propertyValueAs<T>(found->second.value);
278  }
279  else {
280  return default_value;
281  }
282  }
283  catch (...)
284  {
285  std::throw_with_nested(std::runtime_error("cannot set property value of '" + property_name));
286  }
287 }
288 
289 
297 template <typename T>
298 void setPropertyValue(Configuration& configuration, const std::string& property_name, const T& property_value)
299 {
300  try
301  {
302  configuration.insert_or_assign(property_name, PropertyValue(property_value));
303  }
304  catch (...)
305  {
306  std::throw_with_nested(std::runtime_error("cannot set property value of '" + property_name));
307  }
308 }
309 
314 {
315 public:
319  virtual ~Configurable() = default;
323  virtual const Configuration& getConfiguration() const
324  {
325  return _configuration;
326  }
327 
332  virtual void setConfiguration(const Configuration& configuration)
333  {
334  _configuration = _configuration + configuration;
335  }
336 
337 private:
338  Configuration _configuration;
339 };
340 
341 } // namespace adtf_file
Definition: configuration.h:314
virtual ~Configurable()=default
DTOR.
virtual const Configuration & getConfiguration() const
Definition: configuration.h:323
virtual void setConfiguration(const Configuration &configuration)
Definition: configuration.h:332
namespace for ADTF File library
Definition: adtf2_adtf_core_media_sample_deserializer.h:25
Configuration operator+(const Configuration &config_left, const Configuration &config_right)
Add operator for configurations.
Definition: configuration.h:153
bool operator==(const PropertyValue &left, const PropertyValue &right)
compare operator for PropertyValue
Definition: configuration.h:133
void setPropertyValue(Configuration &configuration, const std::string &property_name, const T &property_value)
Definition: configuration.h:298
T getPropertyValue(const Configuration &configuration, const std::string &property_name)
Definition: configuration.h:237
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:143
PropertyValue variant class.
Definition: configuration.h:32
PropertyValue & operator=(PropertyValue &&)=default
move assignment
PropertyValue(std::string_view string_value)
CTOR for string value and "string" type.
Definition: configuration.h:115
PropertyValue(std::string value, std::string type)
CTOR.
Definition: configuration.h:63
PropertyValue()=default
CTOR.
PropertyValue(PropertyValue &&)=default
move CTOR
std::string type
Type as string.
Definition: configuration.h:122
PropertyValue(const PropertyValue &)=default
copy CTOR
PropertyValue & operator=(const PropertyValue &)=default
copy assignment
PropertyValue(T value)
CTOR for unsigned integer value (uint8-uint64) and "unsigned" type.
Definition: configuration.h:73
std::string value
Value as string.
Definition: configuration.h:120

Copyright © CARIAD SE.
Generated on Mon Jun 10 2024 by doxygen 1.9.1
GIT Commit Hash: eb3af397a6b49ad6fcad9a60d8277d909b458b48