ADTF
Loading...
Searching...
No Matches
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
14namespace adtf
15{
16namespace base
17{
18namespace ant
19{
23template<typename T>
24class property_value : public adtf::base::ant::IPropertyValue
25{
26protected:
27 T m_oValue;
28
29public:
30 property_value(): m_oValue()
31 {
32 }
33 property_value(const property_value& oValue): property_value()
34 {
35 m_oValue = oValue.m_oValue;
36 }
37 property_value(property_value&& oValue): property_value()
38 {
39 m_oValue = oValue.m_oValue;
40 }
41 property_value& operator=(const property_value& oValue)
42 {
43 m_oValue = oValue.m_oValue;
44 return *this;
45 }
46 property_value& operator=(property_value&& oValue)
47 {
48 m_oValue = oValue.m_oValue;
49 return *this;
50 }
51 virtual ~property_value()
52 {
53 }
54 property_value(const T& oValue): property_value()
55 {
56 m_oValue = oValue;
57 }
58 void Release()
59 {
60 delete this;
61 }
62 const property_value& operator=(const T& oValue)
63 {
64 m_oValue = oValue;
65 return *this;
66 }
67 bool operator==(const T& oValue) const
68 {
69 return (m_oValue == oValue);
70 }
71 bool operator==(const property_value& oValue) const
72 {
73 return (m_oValue == oValue.GetValue());
74 }
75 const T& GetValue() const
76 {
77 return m_oValue;
78 }
79
80 tResult Set(const IPropertyValue& oValue)
81 {
82 std::string strTypeOfValue;
83 std::string strTypeOfMe;
84 if (IS_OK(oValue.GetType(adtf_string_intf(strTypeOfValue))))
85 {
86 bool bTryStringConversion = false;
87
88 GetType(adtf_string_intf(strTypeOfMe));
89 if (strTypeOfValue == strTypeOfMe)
90 {
91 bool bCalled = false;
92 RETURN_IF_FAILED(oValue.ToRaw(cRawMemoryRedirect(
93 [&](const IRawMemory& oMem) -> tResult
94 {
95 bCalled = true;
96 // if Memory Value is present ...
97 // it happens by reading from XML, that only the String Representation is there !!!
98 if (oMem.GetSize() > 0)
99 {
100 // Try fast memory copy way
101 RETURN_IF_FAILED(FromRaw(oMem));
102 }
103 else
104 {
105 bTryStringConversion = true;
106 }
107
109 })));
110
111 if (!bCalled)
112 {
113 bTryStringConversion = true;
114 }
115 }
117 {
118 // if destination type is cString: use the ToString() of every class
119 // this is a helpful when converting i.e. cFilepath to cString
120 bTryStringConversion = true;
121 }
122 else
123 {
124 RETURN_IF_FAILED(property_type_definition<T>::con_type::Convert(oValue, m_oValue));
125 }
126
127 if (bTryStringConversion)
128 {
129 RETURN_IF_FAILED(oValue.ToString(cStringRedirect(
130 [this](const IString& strValue) -> tResult
131 {
132 RETURN_IF_FAILED(FromString(strValue));
134 })));
135 }
136 }
137 else
138 {
139 A_UTILS_ASSERT(IS_FAILED(oValue.GetType(adtf_string_intf(strTypeOfValue))));
140 RETURN_ERROR(ERR_INVALID_TYPE);
141 }
143 }
144
145 tResult GetType(IString&& strType) const
146 {
147 return strType.Set(property_type_definition<T>::TYPE_NAME);
148 }
149
150 tResult ToRaw(IRawMemory&& oToMem) const
151 {
152 return property_type_definition<T>::con_type::ToRaw(static_cast<const void*>(&m_oValue), sizeof(T), oToMem);
153 }
154 tResult FromRaw(const IRawMemory& oFromMem)
155 {
156 return property_type_definition<T>::con_type::FromRaw(oFromMem, static_cast<void*>(&m_oValue), sizeof(T));
157 }
158
159 tResult ToString(IString&& oStringValue) const
160 {
161 adtf_util::cString strValue;
162 RETURN_IF_FAILED(property_type_definition<T>::con_type::ToString(m_oValue, strValue));
163 RETURN_IF_FAILED(oStringValue.Set(strValue.GetPtr()));
165 }
166 tResult FromString(const IString& oStringValue)
167 {
168 adtf_util::cString strValue = oStringValue.Get();
169 return property_type_definition<T>::con_type::FromString(strValue, m_oValue);
170 }
171};
172
176class cPropertyBase : public IProperty
177{
178protected:
179 util::cString m_strName;
180 mutable ucom::object_ptr<IProperties> m_poSubProperties = nullptr;
181 ucom::object_ptr<IProperties> m_poAttachedProperties = nullptr;
182
183protected:
184 cPropertyBase();
185 virtual ~cPropertyBase() override;
186
187public:
188 tResult GetName(IString&& strName) const override;
189
190 bool HasProperties() const override;
191 bool HasAttachedProperties() const override;
192
194 tResult SetProperties(const IProperties& oProperties) override;
195
198 tResult GetAttachedProperties(IProperty& oProperty) const override;
200
205
206public:
207 tResult SetName(const IString& strName) override;
208};
209
211{
212};
213
219template<typename T>
220class property : public cPropertyBase
221{
222private:
224 property_value<T> m_oValue;
225
226public:
228 property(): cPropertyBase()
229 {
230 }
231
232 ~property() = default;
233
235 property(const property& oProperty): property()
236 {
237 Set(oProperty);
238 }
239
240 property(property&& oProperty)
241 {
242 m_oValue = oProperty.m_oValue;
243 m_strName = oProperty.m_strName;
244 m_poSubProperties = oProperty.m_poSubProperties;
245 oProperty.m_poSubProperties = nullptr;
246 m_poAttachedProperties = oProperty.m_poAttachedProperties;
247 oProperty.m_poAttachedProperties = nullptr;
248 }
249
250 property& operator=(const property& oValue)
251 {
252 Set(oValue);
253 return *this;
254 }
255
257 property& operator=(property&& oProperty)
258 {
259 m_oValue = oProperty.m_oValue;
260 m_strName = oProperty.m_strName;
261 m_poSubProperties = oProperty.m_poSubProperties;
262 oProperty.m_poSubProperties = nullptr;
263 m_poAttachedProperties = oProperty.m_poAttachedProperties;
264 oProperty.m_poAttachedProperties = nullptr;
265 return *this;
266 }
267
273 property(const adtf_util::cString& strName, const T& oValue)
274 {
275 m_strName = strName;
276 m_oValue = oValue;
277 }
278
284 property(std_string_helper, std::string_view strName)
285 {
286 m_strName.Set(strName.data(), strName.size());
287 }
288
293 property(const T& oValue)
294 {
295 m_oValue = oValue;
296 }
297
301 const property& operator=(const T& oValue)
302 {
303 m_oValue = oValue;
304 return *this;
305 }
306
313 bool operator==(const property& oProperty) const
314 {
315 return (m_oValue.operator==(oProperty.GetValueT()));
316 }
317
324 bool operator==(const T& oValue) const
325 {
326 return (m_oValue == oValue);
327 }
328
333 T GetValueT() const
334 {
335 return m_oValue.GetValue();
336 }
337
345 tResult Set(const property& oProperty)
346 {
347 m_oValue = oProperty.m_oValue;
348 m_strName = oProperty.m_strName;
350 oProperty.GetProperties(pSubProperties);
351
352 // if the subproperties are attached we attach them too
353 // mind : If we there are already set properties in our own m_poSubProperties they are hidden then
354 if (oProperty.HasAttachedProperties())
355 {
356 oProperty.GetAttachedProperties(*this);
357 }
358 else
359 {
360 m_poSubProperties->Set(*pSubProperties.Get());
361 }
363 }
364
365public:
366 virtual const IPropertyValue* GetValue() const
367 {
368 return static_cast<const IPropertyValue*>(&m_oValue);
369 }
370 virtual IPropertyValue* GetValue()
371 {
372 return static_cast<IPropertyValue*>(&m_oValue);
373 }
374
375 virtual tResult SetValue(const IPropertyValue& oValue)
376 {
377 return m_oValue.Set(oValue);
378 }
379
380#if defined(_MSC_VER)
381 #pragma warning(push)
382 #pragma warning(disable : 4996)
383#elif defined(__clang__)
384 #pragma clang diagnostic push
385 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
386#else
387 #pragma GCC diagnostic push
388 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
389#endif
390
391 tResult Set(const IProperty& oProp)
392 {
393 oProp.GetName(adtf_string_intf(m_strName));
394 RETURN_IF_FAILED_DESC(m_oValue.Set(*oProp.GetValue()), "Unable to set value of property '%s'",
395 m_strName.GetPtr());
396
397 if (oProp.HasProperties())
398 {
399 // if the the subproperties are attached we attach them too
400 // mind : If we would have set properties already in our own m_poSubProperties they are hidden then
401 if (oProp.HasAttachedProperties())
402 {
403 oProp.GetAttachedProperties(*this);
404 }
405 else
406 {
407 adtf::ucom::object_ptr<const IProperties> pSubProperties;
408 oProp.GetProperties(pSubProperties);
409 SetProperties(*pSubProperties.Get());
410 }
411 }
412
414 }
415};
416#if defined(_MSC_VER)
417 #pragma warning(pop)
418#elif defined(__clang__)
419 #pragma clang diagnostic pop
420#else
421 #pragma GCC diagnostic pop
422#endif
423
424} // namespace ant
425
429
430} // namespace base
431} // namespace adtf
432
433#include "properties_v2.h"
#define IS_FAILED(s)
Check if result is failed.
Definition result.h:20
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
#define IS_OK(s)
Check if result is OK.
Definition result.h:17
#define RETURN_IF_FAILED_DESC(s,...)
Definition result.h:168
Definition property_intf.h:213
Definition property_intf.h:61
virtual tResult ToString(IString &&strIToString) const =0
virtual tResult GetType(IString &&strType) const =0
Definition property_intf.h:119
Definition rawmemory_base.h:23
virtual size_t GetSize() const =0
virtual tResult ToRaw(IRawMemory &&oRawValue) const =0
Definition string_intf.h:29
virtual const char * Get() const =0
tResult DetachProperties() override
tResult GetAttachedProperties(IProperty &oProperty) const override
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 SetProperties(const IProperties &oProperties) override
will copy given properties
tResult AttachProperties(const ucom::ant::iobject_ptr< IProperties > &pAttachedProperties) override
will set given properties to be attached
Definition property.h:25
tResult ToString(IString &&oStringValue) const
Definition property.h:159
tResult FromRaw(const IRawMemory &oFromMem)
Definition property.h:154
tResult GetType(IString &&strType) const
Definition property.h:145
tResult ToRaw(IRawMemory &&oToMem) const
Definition property.h:150
tResult FromString(const IString &oStringValue)
Definition property.h:166
tResult Set(const IPropertyValue &oValue)
Definition property.h:80
~property()=default
DTOR.
const property & operator=(const T &oValue)
Definition property.h:301
bool operator==(const T &oValue) const
Definition property.h:324
property()
CTOR.
Definition property.h:228
T GetValueT() const
Definition property.h:333
property & operator=(property &&oProperty)
move assignment (its not moving yet)
Definition property.h:257
tResult Set(const property &oProperty)
Definition property.h:345
property(property &&oProperty)
implementation of the move CTOR (but it is not moving yet)
Definition property.h:240
property(const adtf_util::cString &strName, const T &oValue)
Definition property.h:273
property(const T &oValue)
Definition property.h:293
property & operator=(const property &oValue)
copy assignment
Definition property.h:250
property(const property &oProperty)
Copy CTOR.
Definition property.h:235
bool operator==(const property &oProperty) const
Definition property.h:313
property(std_string_helper, std::string_view strName)
Definition property.h:284
Definition rawmemory_intf.h:306
Definition string_intf.h:398
Base object pointer to realize binary compatible reference counting in interface methods.
Definition object_ptr_intf.h:112
Definition object_ptr.h:384
Namespace for all functionality of the ADTF Base SDK provided since v3.0.
Definition adtf_class_version.h:16
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
#define adtf_string_intf(__string__)
Definition string_intf.h:465
Definition property_type_definitions.h:38
Definition property.h:211
Definition property_type_definitions.h:211