ADTF
Loading...
Searching...
No Matches
property.h
Go to the documentation of this file.
1
7#pragma once
8
9#include "property_intf.h"
11
13
14#include <variant>
15#include <string>
16#include <string_view>
17#include <cstring>
18
19namespace adtf
20{
21namespace base
22{
28class cPropertyValue : public IPropertyValue
29{
30public:
35 {
39 std::string strType;
43 std::string strValue;
44 };
45
46 template<typename T>
47 cPropertyValue(const T& xValue)
48 {
49 if constexpr (std::is_base_of_v<IPropertyValue, T>)
50 {
51 auto oResult = AssignRaw(xValue);
52
53 if (oResult == ERR_EMPTY)
54 {
55 oResult = AssignString(xValue);
56 }
57
58 THROW_IF_FAILED(oResult);
59 }
60 else
61 {
62 m_xValue = xValue;
63 }
64 }
65
66 cPropertyValue(const char* strValue)
67 {
68 m_xValue = std::string(strValue);
69 }
70
71 cPropertyValue(std::string_view strValue)
72 {
73 m_xValue = std::string(strValue);
74 }
75
76 cPropertyValue(cPropertyValue&&) = default;
77 cPropertyValue(const cPropertyValue&) = default;
78 cPropertyValue& operator=(cPropertyValue&&) = default;
79 cPropertyValue& operator=(const cPropertyValue&) = default;
80
81 tResult GetType(IString&& strType) const override;
82 tResult Set(const IPropertyValue& oValue) override;
83
84 template<typename T>
85 tResult Set(const T& xValue)
86 {
87 if constexpr (std::is_base_of_v<IPropertyValue, T>)
88 {
89 return this->Set(static_cast<const IPropertyValue&>(xValue));
90 }
91 else
92 {
93 m_xValue = xValue;
95 }
96 }
97
98 tResult FromString(const IString& strValueAsString) override;
99 tResult ToString(IString&& strIToString) const override;
100 tResult FromRaw(const IRawMemory& oRawValue) override;
101 tResult ToRaw(IRawMemory&& oRawValue) const override;
102
103 template<typename T>
104 T& Get()
105 {
106 return std::get<T>(m_xValue);
107 }
108
109 template<typename T>
110 const T& Get() const
111 {
112 return std::get<T>(m_xValue);
113 }
114
115 tResult Validate(const IPropertyValue& oTargetValue, const IProperty& oValidationScope) const noexcept;
116
117private:
118 tResult AssignType(const IPropertyValue& oValue);
119 tResult AssignRaw(const IPropertyValue& oValue);
120 tResult AssignString(const IPropertyValue& oValue);
121
122 std::variant<bool,
123 int8_t,
124 uint8_t,
125 int16_t,
126 uint16_t,
127 int32_t,
128 uint32_t,
129 int64_t,
130 uint64_t,
131 float,
132 double,
133 std::string,
134 tUserDefined>
135 m_xValue; //@todo std::unique_ptr for tUserDefined
136};
137
138namespace detail
139{
140template<typename T>
141using is_user_defined_property_value =
142 std::integral_constant<bool,
143 !(std::is_same_v<T, bool> || std::is_arithmetic_v<T> || std::is_same_v<T, std::string>)>;
144
145template<typename T>
146typename std::enable_if<is_user_defined_property_value<T>::value, tResult>::type
147 get_property_value(const cPropertyValue& oValue, T& xValue)
148{
149 const auto& strValue = oValue.Get<cPropertyValue::tUserDefined>().strValue;
150 RETURN_IF_FAILED(property_type_definition<T>::con_type::FromString(adtf::util::cString(strValue.c_str()), xValue));
152}
153
154template<typename T>
155typename std::enable_if<!is_user_defined_property_value<T>::value, tResult>::type
156 get_property_value(const cPropertyValue& oValue, T& xValue)
157{
158 xValue = oValue.Get<T>();
160}
161template<typename T>
162using storage_type =
163 typename std::conditional<is_user_defined_property_value<T>::value, cPropertyValue::tUserDefined, T>::type;
164
165} // namespace detail
166
167template<typename T>
168tResult get_property_value(const IPropertyValue& oValue, T& xValue)
169{
170 cPropertyValue oHelper(detail::storage_type<T>{});
171 RETURN_IF_FAILED(oHelper.Set(oValue));
172 RETURN_IF_FAILED(detail::get_property_value(oHelper, xValue));
174}
175
176template<typename T>
177T get_property_value(const IPropertyValue& oValue)
178{
179 T xValue;
180 THROW_IF_FAILED(get_property_value<T>(oValue, xValue));
181 return xValue;
182}
183
187template<typename T>
188class property_value : public cPropertyValue
189{
190public:
191 property_value(): cPropertyValue(InitValue(T{}))
192 {
193 }
194
195 property_value(const T& oValue): cPropertyValue(InitValue(oValue))
196 {
197 }
198
199public:
200 const T& GetValue() const
201 {
202 if constexpr (std::is_same_v<T, const char*>)
203 {
204 m_oValue = Get<std::string>().c_str();
205 return m_oValue;
206 }
207 else if constexpr (detail::is_user_defined_property_value<T>::value)
208 {
209 THROW_IF_FAILED(detail::get_property_value(*this, m_oValue));
210 return m_oValue;
211 }
212 else
213 {
214 return Get<T>();
215 }
216 }
217
218private:
219 static auto InitValue(const T& oValue)
220 {
221 if constexpr (std::is_same_v<T, const char*>)
222 {
223 return std::string(oValue ? oValue : "");
224 }
225 else if constexpr (detail::is_user_defined_property_value<T>::value)
226 {
227 adtf::util::cString strValue;
229
231 }
232 else
233 {
234 return oValue;
235 }
236 }
237
238private:
239 mutable T m_oValue{};
240};
241
245class cProperty : public IProperty
246{
247public:
248 cProperty();
249 cProperty(const cProperty& oProp);
250 cProperty(cProperty&& oProp);
251 cProperty(const IProperty& oProp);
252
256 cProperty& operator=(const cProperty& oProp);
257
261 cProperty& operator=(cProperty&& oProp);
262
266 cProperty& operator=(const IProperty& oProp);
267
268 template<typename T>
269 cProperty(std::string_view strName, const T& xValue): m_oValue(xValue), m_strName(strName)
270 {
271 }
272
273 template<typename T>
274 cProperty(const char* strName, const T& xValue): cProperty(std::string_view(strName), xValue)
275 {
276 }
277
278 template<typename T>
279 cProperty(const T& xValue): m_oValue(xValue)
280 {
281 }
282
283 ~cProperty();
284
285 const IPropertyValue* GetValue() const override;
286 IPropertyValue* GetValue() override;
287 tResult SetValue(const IPropertyValue& oValue) override;
288
289 template<typename T>
290 tResult SetValue(const T& xValue)
291 {
292 if constexpr (std::is_base_of_v<IPropertyValue, T>)
293 {
294 return this->SetValue(static_cast<const IPropertyValue&>(xValue));
295 }
296 else
297 {
298 RETURN_IF_FAILED(m_oValue.Set(xValue));
299 NotifyObservers();
301 }
302 }
303
304 tResult GetName(IString&& strName) const override;
305 tResult SetName(const IString& strName) override;
306 tResult Set(const IProperty& oProp) override;
307 bool HasProperties() const override;
308 bool HasAttachedProperties() const override;
309 tResult SetProperties(const IProperties& pProperties) override;
311 tResult GetAttachedProperties(IProperty& pProperty) const override;
316 void RegisterObserver(IPropertyObserver& oObserver);
317 void UnregisterObserver(IPropertyObserver& oObserver);
318
319private:
320 tResult SetName(const IProperty& oProp);
321 tResult SetProperties(const IProperty& oProp);
322 void NotifyObservers();
323 void CreateProperties() const;
324
325 cPropertyValue m_oValue;
326 std::string m_strName;
327 mutable ucom::ant::object_ptr<IProperties> m_pChildProperties;
328 std::vector<IPropertyObserver*> m_oObservers;
329 bool m_bAttached = false;
330};
331
335template<typename T, typename Enable = void>
336class property : public cProperty, private IPropertyObserver
337{
338public:
340 {
341 RegisterObserver(*this);
342 }
343
344 property(property&& oProp): cProperty(std::move(oProp))
345 {
346 RegisterObserver(*this);
347 }
348
349 property(const T& oValue): cProperty(cPropertyValue::tUserDefined{})
350 {
351 InitValue(oValue);
352 }
353
354 property(std::string_view strName, const T& oValue):
356 {
357 InitValue(oValue);
358 }
359
360 property(const char* strName, const T& oValue): property(std::string_view(strName), oValue)
361 {
362 }
363
364 ~property()
365 {
366 UnregisterObserver(*this);
367 }
368
369 const T& GetValueT() const
370 {
371 return m_xValue;
372 }
373
374 bool operator==(const T& xOther) const
375 {
376 return GetValueT() == xOther;
377 }
378
379 property& operator=(const T& xValue)
380 {
381 ConvertAndSetValue(xValue);
382 return *this;
383 }
384
385 property& operator=(property&& oProp)
386 {
387 if (this != &oProp)
388 {
389 cProperty::operator=(std::move(oProp));
390 }
391 return *this;
392 }
393
394private:
395 void InitValue(const T& oValue)
396 {
397 ConvertAndSetValue(oValue);
398 RegisterObserver(*this);
399 }
400
401 void ConvertAndSetValue(const T& oValue)
402 {
403 adtf::util::cString strHelper;
405 cProperty::SetValue(cPropertyValue::tUserDefined{property_type_definition<T>::TYPE_NAME, strHelper.GetPtr()});
406 }
407
408 void Notify(const IProperty& /* oProperty*/)
409 {
410 THROW_IF_FAILED(detail::get_property_value(*static_cast<cPropertyValue*>(GetValue()), m_xValue));
411 }
412
413 T m_xValue;
414};
415
419template<typename T>
420class property<T, typename std::enable_if<!detail::is_user_defined_property_value<T>::value>::type> : public cProperty
421{
422public:
423 property(): cProperty(T{})
424 {
425 }
426
427 using cProperty::cProperty;
428
429 const T& GetValueT() const
430 {
431 return static_cast<const cPropertyValue*>(GetValue())->Get<T>();
432 }
433
434 bool operator==(const T& xOther) const
435 {
436 return GetValueT() == xOther;
437 }
438
439 property& operator=(const T& xValue)
440 {
441 SetValue(xValue);
442 return *this;
443 }
444};
445
446} // namespace base
447} // namespace adtf
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:775
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult
Definition result.h:88
Definition property_intf.h:213
Definition property_intf.h:190
Definition property_intf.h:61
Definition property_intf.h:119
Definition rawmemory_base.h:23
Definition string_intf.h:29
Definition property.h:29
tResult ToRaw(IRawMemory &&oRawValue) const override
tResult ToString(IString &&strIToString) const override
tResult FromString(const IString &strValueAsString) override
tResult GetType(IString &&strType) const override
tResult FromRaw(const IRawMemory &oRawValue) override
tResult Set(const IPropertyValue &oValue) override
Definition property.h:246
cProperty & operator=(const IProperty &oProp)
tResult AttachProperties(const ucom::ant::iobject_ptr< IProperties > &pProperties) override
tResult GetProperties(ucom::ant::iobject_ptr< IProperties > &pProperties) override
get subproperties for writing access
cProperty & operator=(const cProperty &oProp)
tResult GetAttachedProperties(IProperty &pProperty) const override
tResult DetachProperties() override
cProperty & operator=(cProperty &&oProp)
tResult SetProperties(const IProperties &pProperties) override
will copy given properties
tResult GetProperties(ucom::ant::iobject_ptr< const IProperties > &pProperties) const override
get subproperties for readonly access
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 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
std::string strValue
Definition property.h:43
std::string strType
Definition property.h:39
Definition property_type_definitions.h:36