ADTF
properties_v2.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "property_intf.h"
11 
12 #include <adtfucom3/adtf_ucom3.h>
13 
14 #include <variant>
15 #include <string>
16 #include <string_view>
17 #include <cstring>
18 
19 namespace adtf
20 {
21 namespace base
22 {
23 namespace spider
24 {
26 {
27 public:
28  struct tUserDefined
29  {
30  std::string strType;
31  std::string strValue;
32  };
33 
34  template <typename T>
35  cPropertyValue(const T& xValue)
36  {
37  if constexpr(std::is_base_of_v<ant::IPropertyValue, T>)
38  {
39  auto oResult = AssignRaw(xValue);
40 
41  if (oResult == ERR_EMPTY)
42  {
43  oResult = AssignString(xValue);
44  }
45 
46  THROW_IF_FAILED(oResult);
47  }
48  else
49  {
50  m_xValue = xValue;
51  }
52  }
53 
54  cPropertyValue(const char* strValue)
55  {
56  m_xValue = std::string(strValue);
57  }
58 
59  cPropertyValue(std::string_view strValue)
60  {
61  m_xValue = std::string(strValue);
62  }
63 
64  cPropertyValue(cPropertyValue&&) = default;
65  cPropertyValue(const cPropertyValue&) = default;
66  cPropertyValue& operator=(cPropertyValue&&) = default;
67  cPropertyValue& operator=(const cPropertyValue&) = default;
68 
69  tResult GetType(IString&& strType) const override;
70  tResult Set(const IPropertyValue& oValue) override;
71 
72  template <typename T>
73  tResult Set(const T& xValue)
74  {
75  if constexpr(std::is_base_of_v<ant::IPropertyValue, T>)
76  {
77  return this->Set(static_cast<const IPropertyValue&>(xValue));
78  }
79  else
80  {
81  m_xValue = xValue;
83  }
84  }
85 
86  tResult FromString(const IString& strValueAsString) override;
87  tResult ToString(IString&& strIToString) const override;
88  tResult FromRaw(const IRawMemory& oRawValue) override;
89  tResult ToRaw(IRawMemory&& oRawValue) const override;
90 
91  template <typename T>
92  T& Get()
93  {
94  return std::get<T>(m_xValue);
95  }
96 
97  template <typename T>
98  const T& Get() const
99  {
100  return std::get<T>(m_xValue);
101  }
102 
103 private:
104  tResult AssignRaw(const IPropertyValue& oValue);
105  tResult AssignString(const IPropertyValue& oValue);
106 
107  std::variant<bool, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, double, std::string, tUserDefined> m_xValue; //@todo std::unique_ptr for tUserDefined
108 };
109 
110 namespace detail
111 {
112 template <typename T>
113 using is_user_defined_property_value = std::integral_constant<bool, !(std::is_same_v<T, bool> || std::is_arithmetic_v<T> || std::is_same_v<T, std::string>)>;
114 
115 template <typename T>
116 typename std::enable_if<is_user_defined_property_value<T>::value, tResult>::type get_property_value(const cPropertyValue& oValue, T& xValue)
117 {
118  const auto& strValue = oValue.Get<cPropertyValue::tUserDefined>().strValue;
119  RETURN_IF_FAILED(ant::property_type_definition<T>::con_type::FromString(adtf::util::cString(strValue.c_str()), xValue));
121 }
122 
123 template <typename T>
124 typename std::enable_if<!is_user_defined_property_value<T>::value, tResult>::type get_property_value(const cPropertyValue& oValue, T& xValue)
125 {
126  xValue = oValue.Get<T>();
128 }
129 template <typename T>
130 using storage_type = typename std::conditional<is_user_defined_property_value<T>::value, cPropertyValue::tUserDefined, T>::type;
131 
132 }
133 
134 template <typename T>
135 tResult get_property_value(const IPropertyValue& oValue, T& xValue)
136 {
137  cPropertyValue oHelper(detail::storage_type<T>{});
138  RETURN_IF_FAILED(oHelper.Set(oValue));
139  RETURN_IF_FAILED(detail::get_property_value(oHelper, xValue));
141 }
142 
143 template <typename T>
144 T get_property_value(const IPropertyValue& oValue)
145 {
146  T xValue;
147  THROW_IF_FAILED(get_property_value<T>(oValue, xValue));
148  return xValue;
149 }
150 
151 class cProperties;
152 
154 {
155 
156 public:
157  cProperty();
158  cProperty(const cProperty& oProp);
159  cProperty(cProperty&& oProp);
160  cProperty(const ant::IProperty& oProp);
161 
165  cProperty& operator=(const cProperty& oProp);
166 
171 
176 
177  template <typename T>
178  cProperty(std::string_view strName, const T& xValue):
179  m_oValue(xValue),
180  m_strName(strName)
181  {
182  }
183 
184  template <typename T>
185  cProperty(const char* strName, const T& xValue): cProperty(std::string_view(strName), xValue)
186  {
187  }
188 
189  template <typename T>
190  cProperty(const T& xValue):
191  m_oValue(xValue)
192  {
193  }
194 
195  ~cProperty();
196 
197  const IPropertyValue* GetValue() const override;
198  IPropertyValue* GetValue() override;
199  tResult SetValue(const IPropertyValue& oValue) override;
200 
201  template <typename T>
202  tResult SetValue(const T& xValue)
203  {
204  if constexpr(std::is_base_of_v<ant::IPropertyValue, T>)
205  {
206  return this->SetValue(static_cast<const IPropertyValue&>(xValue));
207  }
208  else
209  {
210  RETURN_IF_FAILED(m_oValue.Set(xValue));
211  NotifyObservers();
213  }
214  }
215 
216  tResult GetName(IString&& strName) const override;
217  tResult SetName(const IString& strName) override;
218  tResult Set(const IProperty& oProp) override;
219  bool HasProperties() const override;
220  bool HasAttachedProperties() const override;
221  tResult SetProperties(const IProperties& pProperties) override;
223  tResult GetAttachedProperties(IProperty& pProperty) const override;
228  void RegisterObserver(IPropertyObserver& oObserver);
229  void UnregisterObserver(IPropertyObserver& oObserver);
230 
231 private:
232  tResult SetName(const IProperty& oProp);
233  tResult SetProperties(const IProperty& oProp);
234  void NotifyObservers();
235  void CreateProperties() const;
236 
237  cPropertyValue m_oValue;
238  std::string m_strName;
239  mutable ucom::ant::object_ptr<ant::IProperties> m_pChildProperties;
240  std::vector<IPropertyObserver*> m_oObservers;
241  bool m_bAttached = false;
242 };
243 
244 template <typename T, typename Enable = void>
245 class property: public cProperty, private IPropertyObserver
246 {
247  public:
249  {
250  RegisterObserver(*this);
251  }
252 
253  property(property&& oProp)
254  : cProperty(std::move(oProp))
255  {
256  RegisterObserver(*this);
257  }
258 
259  property(const T& oValue): cProperty(cPropertyValue::tUserDefined{})
260  {
261  InitValue(oValue);
262  }
263 
264  property(std::string_view strName, const T& oValue): cProperty(strName, cPropertyValue::tUserDefined{ant::property_type_definition<T>::TYPE_NAME})
265  {
266  InitValue(oValue);
267  }
268 
269  property(const char* strName, const T& oValue): property(std::string_view(strName), oValue)
270  {
271  }
272 
273  ~property()
274  {
275  UnregisterObserver(*this);
276  }
277 
278  const T& GetValueT() const
279  {
280  return m_xValue;
281  }
282 
283  bool operator==(const T& xOther) const
284  {
285  return GetValueT() == xOther;
286  }
287 
288  property& operator=(const T& xValue)
289  {
290  ConvertAndSetValue(xValue);
291  return *this;
292  }
293 
294  property& operator=(property&& oProp)
295  {
296  if (this != &oProp)
297  {
298  cProperty::operator=(std::move(oProp));
299  }
300  return *this;
301  }
302 
303  private:
304  void InitValue(const T& oValue)
305  {
306  ConvertAndSetValue(oValue);
307  RegisterObserver(*this);
308  }
309 
310  void ConvertAndSetValue(const T& oValue)
311  {
312  adtf::util::cString strHelper;
314  cProperty::SetValue(cPropertyValue::tUserDefined{ant::property_type_definition<T>::TYPE_NAME, strHelper.GetPtr()});
315  }
316 
317  void Notify(const IProperty& /* oProperty*/)
318  {
319  THROW_IF_FAILED(detail::get_property_value(*static_cast<cPropertyValue*>(GetValue()), m_xValue));
320  }
321 
322  T m_xValue;
323 };
324 
325 template <typename T>
326 class property<T, typename std::enable_if<!detail::is_user_defined_property_value<T>::value>::type>: public cProperty
327 {
328  public:
329  property(): cProperty(T{})
330  {
331  }
332 
333  using cProperty::cProperty;
334 
335  const T& GetValueT() const
336  {
337  return static_cast<const cPropertyValue*>(GetValue())->Get<T>();
338  }
339 
340  bool operator==(const T& xOther) const
341  {
342  return GetValueT() == xOther;
343  }
344 
345  property& operator=(const T& xValue)
346  {
347  SetValue(xValue);
348  return *this;
349  }
350 };
351 
352 class cProperties: public ucom::catwo::object<ant::IProperties>
353 {
354  public:
355  cProperties() = default;
356  cProperties(const ant::IProperties& oProperties);
357  bool Exists(const char* strName) const override;
358  tResult Get(IProperties& pProperties) const override;
359  tResult Set(const IProperties& pProperties) override;
360  size_t GetSize() const override;
361  tResult GetProperty(const char* strName, IProperty& pProperty) const override;
362  tResult SetProperty(const IProperty& oProperty) override;
363 
364  template<typename T>
365  cProperties& CreateProperty(std::string_view strName, const T& xValue)
366  {
367  const auto emplaced = m_oProperties.emplace(std::piecewise_construct,
368  std::forward_as_tuple(strName),
369  std::forward_as_tuple(strName, xValue));
370  if (!emplaced.second)
371  {
372  emplaced.first->second.SetValue(xValue);
373  }
374 
375  return static_cast<cProperties&>(emplaced.first->second.GetProperties());
376  }
377 
378  tResult SetPropertyByPath(const char* strParentPath, const IProperty& pProperty) override;
379  tResult RemoveProperty(const char* strName) override;
380  tResult RegisterPropertyObserver(const char* strPropertyName,
381  IPropertyObserver& oObserver) override;
382  tResult UnregisterPropertyObserver(IPropertyObserver& oObserver) override;
383 
384  private:
385  std::map<std::string, cProperty, std::less<>> m_oProperties;
386 };
387 
388 }
389 
390 using spider::cProperties;
391 using spider::cProperty;
393 using spider::property;
394 using spider::get_property_value;
395 
396 }
397 }
Copyright © Audi Electronics Venture GmbH.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Defintion of a property set container interface.
The IProperty interface provides methods for getting and setting property values, name of the propert...
Observer Interface to react on property changes.
The IPropertyValue interface provides methods for getting and setting property values.
Definition: property_intf.h:61
The IRawMemory interface provides methods for getting and setting memory values through abstract inte...
The IString interface provides methods for getting and setting strings through abstract interfaces.
Definition: string_intf.h:28
tResult DetachProperties() override
tResult GetProperties(ucom::ant::iobject_ptr< const IProperties > &pProperties) const override
get subproperties for readonly access
tResult GetProperties(ucom::ant::iobject_ptr< IProperties > &pProperties) override
get subproperties for writing access
cProperty & operator=(const ant::IProperty &oProp)
Copy assignment, mind that this will perform a merge of the subproperties of oProp into this,...
cProperty & operator=(cProperty &&oProp)
Move assignment, mind that this will perform a merge of the subproperties of oProp into this (via cop...
cProperty & operator=(const cProperty &oProp)
Copy assignment, mind that this will perform a merge of the subproperties of oProp into this,...
tResult GetAttachedProperties(IProperty &pProperty) const override
tResult SetProperties(const IProperties &pProperties) override
will copy given properties
tResult AttachProperties(const ucom::ant::iobject_ptr< IProperties > &pProperties) override
tResult FromRaw(const IRawMemory &oRawValue) override
Implement to create a fast value copy in memory.
tResult ToRaw(IRawMemory &&oRawValue) const override
Implement to create a fast value copy in memory.
tResult FromString(const IString &strValueAsString) override
Implement to deserialize the value from a textfile and/or to set by string.
tResult ToString(IString &&strIToString) const override
Implement to serialize the value to a textfile and/or to show it on a display.
tResult Set(const IPropertyValue &oValue) override
Sets the value by a deep copy.
tResult GetType(IString &&strType) const override
Retrieves the string for the property value type.
void Notify(const IProperty &)
Implements the observer pattern.
Base object pointer to realize binary compatible reference counting in interface methods.
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:397
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2784
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Concept template to define the Name and the conversion type for the given type TYPE.
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult