ADTF
property.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include "property_intf.h"
9 #include "propertyconvert.h"
11 #include <optional>
12 #include <cstring>
13 
14 namespace adtf
15 {
16 namespace base
17 {
18 namespace ant
19 {
20  template<typename T>
22  {
23  protected:
24  T m_oValue;
25  public:
26  property_value() : m_oValue()
27  {
28  }
30  {
31  m_oValue = oValue.m_oValue;
32  }
34  {
35  m_oValue = oValue.m_oValue;
36  }
37  property_value& operator=(const property_value& oValue)
38  {
39  m_oValue = oValue.m_oValue;
40  return *this;
41  }
42  property_value& operator=(property_value&& oValue)
43  {
44  m_oValue = oValue.m_oValue;
45  return *this;
46  }
47  virtual ~property_value()
48  {
49  }
50  property_value(const T& oValue) : property_value()
51  {
52  m_oValue = oValue;
53  }
54  void Release()
55  {
56  delete this;
57  }
58  const property_value& operator=(const T& oValue)
59  {
60  m_oValue = oValue;
61  return *this;
62  }
63  bool operator==(const T& oValue) const
64  {
65  return (m_oValue == oValue);
66  }
67  bool operator==(const property_value& oValue) const
68  {
69  return (m_oValue == oValue.GetValue());
70  }
71  const T& GetValue() const
72  {
73  return m_oValue;
74  }
75 
76  tResult Set(const IPropertyValue& oValue)
77  {
78  std::string strTypeOfValue;
79  std::string strTypeOfMe;
80  if (IS_OK(oValue.GetType(adtf_string_intf(strTypeOfValue))))
81  {
82  bool bTryStringConversion = false;
83 
84  GetType(adtf_string_intf(strTypeOfMe));
85  if (strTypeOfValue == strTypeOfMe)
86  {
87  bool bCalled = false;
89  {
90  bCalled = true;
91  //if Memory Value is present ...
92  //it happens by reading from XML, that only the String Representation is there !!!
93  if (oMem.GetSize() > 0)
94  {
95  //Try fast memory copy way
96  RETURN_IF_FAILED(FromRaw(oMem));
97  }
98  else
99  {
100  bTryStringConversion = true;
101  }
102 
104  })));
105 
106  if (!bCalled)
107  {
108  bTryStringConversion = true;
109  }
110  }
112  {
113  // if destination type is cString: use the ToString() of every class
114  // this is a helpful when converting i.e. cFilepath to cString
115  bTryStringConversion = true;
116  }
117  else
118  {
120  }
121 
122  if (bTryStringConversion)
123  {
124  RETURN_IF_FAILED(oValue.ToString(cStringRedirect([this](const IString& strValue) -> tResult
125  {
126  RETURN_IF_FAILED(FromString(strValue));
128  })));
129  }
130  }
131  else
132  {
133  A_UTILS_ASSERT(IS_FAILED(oValue.GetType(adtf_string_intf(strTypeOfValue))));
134  RETURN_ERROR(ERR_INVALID_TYPE);
135  }
137  }
138 
139  tResult GetType(IString&& strType) const
140  {
141  return strType.Set(property_type_definition<T>::TYPE_NAME);
142  }
143 
144  tResult ToRaw(IRawMemory&& oToMem) const
145  {
146  return property_type_definition<T>::con_type::ToRaw(static_cast<const void*>(&m_oValue), sizeof(T), oToMem);
147  }
148  tResult FromRaw(const IRawMemory& oFromMem)
149  {
150  return property_type_definition<T>::con_type::FromRaw(oFromMem, static_cast<void*>(&m_oValue), sizeof(T));
151  }
152 
153  tResult ToString(IString&& oStringValue) const
154  {
155  adtf_util::cString strValue;
157  RETURN_IF_FAILED(oStringValue.Set(strValue.GetPtr()));
159  }
160  tResult FromString(const IString& oStringValue)
161  {
162  adtf_util::cString strValue = oStringValue.Get();
163  return property_type_definition<T>::con_type::FromString(strValue, m_oValue);
164  }
165  };
166 
167  class cPropertyBase : public IProperty
168  {
169  protected:
170  util::cString m_strName;
171  mutable ucom::object_ptr<IProperties> m_poSubProperties = nullptr;
172  ucom::object_ptr<IProperties> m_poAttachedProperties = nullptr;
173 
174  protected:
175  cPropertyBase();
176  virtual ~cPropertyBase() = default;
177 
178  public:
179  tResult GetName(IString&& strName) const override;
180 
181  bool HasProperties() const override;
182  bool HasAttachedProperties() const override;
183 
185  tResult SetProperties(const IProperties& oProperties) override;
186 
188  tResult AttachProperties(const ucom::ant::iobject_ptr<IProperties>& pAttachedProperties) override;
189  tResult GetAttachedProperties(IProperty& oProperty) const override;
190  tResult DetachProperties() override;
191 
196 
197  public:
198  tResult SetName(const IString& strName);
199 
200  };
201 
203  {};
204 
210  template <typename T>
211  class property : public cPropertyBase
212  {
213  private:
216 
217  public:
220  {
221  }
223  ~property() = default;
224 
226  property(const property& oProperty) : property()
227  {
228  Set(oProperty);
229  }
231  property(property&& oProperty)
232  {
233  m_oValue = oProperty.m_oValue;
234  m_strName = oProperty.m_strName;
235  m_poSubProperties = oProperty.m_poSubProperties;
236  oProperty.m_poSubProperties = nullptr;
237  m_poAttachedProperties = oProperty.m_poAttachedProperties;
238  oProperty.m_poAttachedProperties = nullptr;
239  }
241  property& operator=(const property& oValue)
242  {
243  Set(oValue);
244  return *this;
245  }
246 
248  property& operator=(property&& oProperty)
249  {
250  m_oValue = oProperty.m_oValue;
251  m_strName = oProperty.m_strName;
252  m_poSubProperties = oProperty.m_poSubProperties;
253  oProperty.m_poSubProperties = nullptr;
254  m_poAttachedProperties = oProperty.m_poAttachedProperties;
255  oProperty.m_poAttachedProperties = nullptr;
256  return *this;
257  }
258 
264  property(const adtf_util::cString& strName, const T& oValue)
265  {
266  m_strName = strName;
267  m_oValue = oValue;
268  }
269 
275  property(std_string_helper, std::string_view strName)
276  {
277  m_strName.Set(strName.data(), strName.size());
278  }
279 
284  property(const T& oValue)
285  {
286  m_oValue = oValue;
287  }
292  const property& operator=(const T& oValue)
293  {
294  m_oValue = oValue;
295  return *this;
296  }
297 
304  bool operator==(const property& oProperty) const
305  {
306  return (m_oValue.operator==(oProperty.GetValueT()));
307  }
308 
315  bool operator==(const T& oValue) const
316  {
317  return (m_oValue == oValue);
318  }
319 
324  T GetValueT() const
325  {
326  return m_oValue.GetValue();
327  }
328 
336  tResult Set(const property& oProperty)
337  {
338  m_oValue = oProperty.m_oValue;
339  m_strName = oProperty.m_strName;
341  oProperty.GetProperties(pSubProperties);
342 
343  //if the subproperties are attached we attach them too
344  //mind : If we there are already set properties in our own m_poSubProperties they are hidden then
345  if (oProperty.HasAttachedProperties())
346  {
347  oProperty.GetAttachedProperties(*this);
348  }
349  else
350  {
351  m_poSubProperties->Set(*pSubProperties.Get());
352  }
354  }
355 
356  public:
357  virtual const IPropertyValue* GetValue() const
358  {
359  return static_cast<const IPropertyValue*>(&m_oValue);
360  }
361  virtual IPropertyValue* GetValue()
362  {
363  return static_cast<IPropertyValue*>(&m_oValue);
364  }
365 
366  virtual tResult SetValue(const IPropertyValue& oValue)
367  {
368  return m_oValue.Set(oValue);
369  }
370 
371  tResult Set(const IProperty& oProp)
372  {
373  oProp.GetName(adtf_string_intf(m_strName));
374  RETURN_IF_FAILED_DESC(m_oValue.Set(*oProp.GetValue()),
375  "Unable to set value of property '%s'", m_strName.GetPtr());
376 
377  if (oProp.HasProperties())
378  {
379  //if the the subproperties are attached we attach them too
380  //mind : If we would have set properties already in our own m_poSubProperties they are hidden then
381  if (oProp.HasAttachedProperties())
382  {
383  oProp.GetAttachedProperties(*this);
384  }
385  else
386  {
388  oProp.GetProperties(pSubProperties);
389  SetProperties(*pSubProperties.Get());
390  }
391  }
392 
394  }
395  };
396 
397 
398 
399 } //namespace ant
400 
401 using ant::property_type_definition;
402 using ant::property_value;
403 using elasto::tree_node_property_type;
404 
405 } //namespace base
406 } // namespace adtf
407 
408 #include "properties_v2.h"
#define A_UTILS_ASSERT
This macro is used for platform independent assertion expressions.
#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.
#define RETURN_ERROR(code)
Return specific error code, 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...
The IPropertyValue interface provides methods for getting and setting property values.
Definition: property_intf.h:60
virtual tResult ToString(IString &&strIToString) const =0
Implement to serialize the value to a textfile and/or to show it on a display.
virtual tResult GetType(IString &&strType) const =0
Retrieves the string for the property value type.
The IRawMemory interface provides methods for getting and setting memory values through abstract inte...
virtual size_t GetSize() const =0
Returns the size in bytes of the memory.
virtual tResult ToRaw(IRawMemory &&oRawValue) const =0
Implement to create a fast value copy in memory.
The IString interface provides methods for getting and setting strings through abstract interfaces.
Definition: string_intf.h:28
virtual const char * Get() const =0
Gets the pointer to the current associated nullterminated-string.
tResult AttachProperties(const ucom::ant::iobject_ptr< IProperties > &pAttachedProperties) override
will set given properties to be attached
tResult SetProperties(const IProperties &oProperties) override
will copy given properties
tResult GetProperties(ucom::ant::iobject_ptr< IProperties > &pSubProperties) override
get subproperties for writing access
tResult GetProperties(ucom::ant::iobject_ptr< const IProperties > &pSubProperties) const override
get subproperties for readonly access
tResult Set(const IPropertyValue &oValue)
Sets the value by a deep copy.
Definition: property.h:76
tResult GetType(IString &&strType) const
Retrieves the string for the property value type.
Definition: property.h:139
tResult ToString(IString &&oStringValue) const
Implement to serialize the value to a textfile and/or to show it on a display.
Definition: property.h:153
tResult ToRaw(IRawMemory &&oToMem) const
Implement to create a fast value copy in memory.
Definition: property.h:144
tResult FromString(const IString &oStringValue)
Implement to deserialize the value from a textfile and/or to set by string.
Definition: property.h:160
tResult FromRaw(const IRawMemory &oFromMem)
Implement to create a fast value copy in memory.
Definition: property.h:148
Property property implementation template.
Definition: property.h:212
const property & operator=(const T &oValue)
value assignment
Definition: property.h:292
property(const T &oValue)
CTOR with value.
Definition: property.h:284
property & operator=(property &&oProperty)
move assignment (its not moving yet)
Definition: property.h:248
tResult Set(const property &oProperty)
Sets the property value and name as COPY of oProperty.
Definition: property.h:336
property(std_string_helper, std::string_view strName)
CTOR with name an value.
Definition: property.h:275
T GetValueT() const
Get the containing value.
Definition: property.h:324
~property()=default
DTOR.
bool operator==(const property &oProperty) const
compare operator
Definition: property.h:304
property_value< T > m_oValue
value type container
Definition: property.h:215
property(const adtf_util::cString &strName, const T &oValue)
CTOR with name an value.
Definition: property.h:264
property(const property &oProperty)
Copy CTOR.
Definition: property.h:226
bool operator==(const T &oValue) const
compare operator
Definition: property.h:315
property & operator=(const property &oValue)
copy assignment
Definition: property.h:241
property(property &&oProperty)
implementation of the move CTOR (but it is not moving yet)
Definition: property.h:231
Base object pointer to realize binary compatible reference counting in interface methods.
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:163
virtual T * Get() const
Get pointer to shared object.
Definition: object_ptr.h:381
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.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
#define adtf_string_intf(__string__)
The adtf_string_intf Macro helps to easily create a rvalue reference of a adtf::util::cString.
Definition: string_intf.h:371
Concept template to define the Name and the conversion type for the given type TYPE.
#define RETURN_IF_FAILED_DESC(s,...)
returns if the expression returns a failed tResult and ammends the error message.