ADTF
Loading...
Searching...
No Matches
properties_v2.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{
23namespace spider
24{
30class cPropertyValue : public ant::IPropertyValue
31{
32public:
37 {
41 std::string strType;
45 std::string strValue;
46 };
47
48 template<typename T>
49 cPropertyValue(const T& xValue)
50 {
51 if constexpr (std::is_base_of_v<ant::IPropertyValue, T>)
52 {
53 auto oResult = AssignRaw(xValue);
54
55 if (oResult == ERR_EMPTY)
56 {
57 oResult = AssignString(xValue);
58 }
59
60 THROW_IF_FAILED(oResult);
61 }
62 else
63 {
64 m_xValue = xValue;
65 }
66 }
67
68 cPropertyValue(const char* strValue)
69 {
70 m_xValue = std::string(strValue);
71 }
72
73 cPropertyValue(std::string_view strValue)
74 {
75 m_xValue = std::string(strValue);
76 }
77
78 cPropertyValue(cPropertyValue&&) = default;
79 cPropertyValue(const cPropertyValue&) = default;
80 cPropertyValue& operator=(cPropertyValue&&) = default;
81 cPropertyValue& operator=(const cPropertyValue&) = default;
82
83 tResult GetType(IString&& strType) const override;
84 tResult Set(const IPropertyValue& oValue) override;
85
86 template<typename T>
87 tResult Set(const T& xValue)
88 {
89 if constexpr (std::is_base_of_v<ant::IPropertyValue, T>)
90 {
91 return this->Set(static_cast<const IPropertyValue&>(xValue));
92 }
93 else
94 {
95 m_xValue = xValue;
97 }
98 }
99
100 tResult FromString(const IString& strValueAsString) override;
101 tResult ToString(IString&& strIToString) const override;
102 tResult FromRaw(const IRawMemory& oRawValue) override;
103 tResult ToRaw(IRawMemory&& oRawValue) const override;
104
105 template<typename T>
106 T& Get()
107 {
108 return std::get<T>(m_xValue);
109 }
110
111 template<typename T>
112 const T& Get() const
113 {
114 return std::get<T>(m_xValue);
115 }
116
117 tResult Validate(const IProperty& oValidationScope) const noexcept;
118
119private:
120 tResult AssignRaw(const IPropertyValue& oValue);
121 tResult AssignString(const IPropertyValue& oValue);
122
123 std::variant<bool,
124 int8_t,
125 uint8_t,
126 int16_t,
127 uint16_t,
128 int32_t,
129 uint32_t,
130 int64_t,
131 uint64_t,
132 float,
133 double,
134 std::string,
135 tUserDefined>
136 m_xValue; //@todo std::unique_ptr for tUserDefined
137};
138
139namespace detail
140{
141template<typename T>
142using is_user_defined_property_value =
143 std::integral_constant<bool,
144 !(std::is_same_v<T, bool> || std::is_arithmetic_v<T> || std::is_same_v<T, std::string>)>;
145
146template<typename T>
147typename std::enable_if<is_user_defined_property_value<T>::value, tResult>::type
148 get_property_value(const cPropertyValue& oValue, T& xValue)
149{
150 const auto& strValue = oValue.Get<cPropertyValue::tUserDefined>().strValue;
151 RETURN_IF_FAILED(
152 ant::property_type_definition<T>::con_type::FromString(adtf::util::cString(strValue.c_str()), xValue));
154}
155
156template<typename T>
157typename std::enable_if<!is_user_defined_property_value<T>::value, tResult>::type
158 get_property_value(const cPropertyValue& oValue, T& xValue)
159{
160 xValue = oValue.Get<T>();
162}
163template<typename T>
164using storage_type =
165 typename std::conditional<is_user_defined_property_value<T>::value, cPropertyValue::tUserDefined, T>::type;
166
167} // namespace detail
168
169template<typename T>
170tResult get_property_value(const IPropertyValue& oValue, T& xValue)
171{
172 cPropertyValue oHelper(detail::storage_type<T>{});
173 RETURN_IF_FAILED(oHelper.Set(oValue));
174 RETURN_IF_FAILED(detail::get_property_value(oHelper, xValue));
176}
177
178template<typename T>
179T get_property_value(const IPropertyValue& oValue)
180{
181 T xValue;
182 THROW_IF_FAILED(get_property_value<T>(oValue, xValue));
183 return xValue;
184}
185
186class cProperties;
187
191class cProperty : public ant::IProperty
192{
193public:
194 cProperty();
195 cProperty(const cProperty& oProp);
196 cProperty(cProperty&& oProp);
197 cProperty(const ant::IProperty& oProp);
198
202 cProperty& operator=(const cProperty& oProp);
203
207 cProperty& operator=(cProperty&& oProp);
208
212 cProperty& operator=(const ant::IProperty& oProp);
213
214 template<typename T>
215 cProperty(std::string_view strName, const T& xValue): m_oValue(xValue), m_strName(strName)
216 {
217 }
218
219 template<typename T>
220 cProperty(const char* strName, const T& xValue): cProperty(std::string_view(strName), xValue)
221 {
222 }
223
224 template<typename T>
225 cProperty(const T& xValue): m_oValue(xValue)
226 {
227 }
228
229 ~cProperty();
230
231 const IPropertyValue* GetValue() const override;
232 IPropertyValue* GetValue() override;
233 tResult SetValue(const IPropertyValue& oValue) override;
234
235 template<typename T>
236 tResult SetValue(const T& xValue)
237 {
238 if constexpr (std::is_base_of_v<ant::IPropertyValue, T>)
239 {
240 return this->SetValue(static_cast<const IPropertyValue&>(xValue));
241 }
242 else
243 {
244 RETURN_IF_FAILED(m_oValue.Set(xValue));
245 NotifyObservers();
247 }
248 }
249
250 tResult GetName(IString&& strName) const override;
251 tResult SetName(const IString& strName) override;
252 tResult Set(const IProperty& oProp) override;
253 bool HasProperties() const override;
254 bool HasAttachedProperties() const override;
255 tResult SetProperties(const IProperties& pProperties) override;
257 tResult GetAttachedProperties(IProperty& pProperty) const override;
262 void RegisterObserver(IPropertyObserver& oObserver);
263 void UnregisterObserver(IPropertyObserver& oObserver);
264
265private:
266 tResult SetName(const IProperty& oProp);
267 tResult SetProperties(const IProperty& oProp);
268 void NotifyObservers();
269 void CreateProperties() const;
270
271 cPropertyValue m_oValue;
272 std::string m_strName;
273 mutable ucom::ant::object_ptr<ant::IProperties> m_pChildProperties;
274 std::vector<IPropertyObserver*> m_oObservers;
275 bool m_bAttached = false;
276};
277
281template<typename T, typename Enable = void>
282class property : public cProperty, private IPropertyObserver
283{
284public:
286 {
287 RegisterObserver(*this);
288 }
289
290 property(property&& oProp): cProperty(std::move(oProp))
291 {
292 RegisterObserver(*this);
293 }
294
295 property(const T& oValue): cProperty(cPropertyValue::tUserDefined{})
296 {
297 InitValue(oValue);
298 }
299
300 property(std::string_view strName, const T& oValue):
302 {
303 InitValue(oValue);
304 }
305
306 property(const char* strName, const T& oValue): property(std::string_view(strName), oValue)
307 {
308 }
309
310 ~property()
311 {
312 UnregisterObserver(*this);
313 }
314
315 const T& GetValueT() const
316 {
317 return m_xValue;
318 }
319
320 bool operator==(const T& xOther) const
321 {
322 return GetValueT() == xOther;
323 }
324
325 property& operator=(const T& xValue)
326 {
327 ConvertAndSetValue(xValue);
328 return *this;
329 }
330
331 property& operator=(property&& oProp)
332 {
333 if (this != &oProp)
334 {
335 cProperty::operator=(std::move(oProp));
336 }
337 return *this;
338 }
339
340private:
341 void InitValue(const T& oValue)
342 {
343 ConvertAndSetValue(oValue);
344 RegisterObserver(*this);
345 }
346
347 void ConvertAndSetValue(const T& oValue)
348 {
349 adtf::util::cString strHelper;
351 cProperty::SetValue(
353 }
354
355 void Notify(const IProperty& /* oProperty*/)
356 {
357 THROW_IF_FAILED(detail::get_property_value(*static_cast<cPropertyValue*>(GetValue()), m_xValue));
358 }
359
360 T m_xValue;
361};
362
366template<typename T>
367class property<T, typename std::enable_if<!detail::is_user_defined_property_value<T>::value>::type> : public cProperty
368{
369public:
370 property(): cProperty(T{})
371 {
372 }
373
374 using cProperty::cProperty;
375
376 const T& GetValueT() const
377 {
378 return static_cast<const cPropertyValue*>(GetValue())->Get<T>();
379 }
380
381 bool operator==(const T& xOther) const
382 {
383 return GetValueT() == xOther;
384 }
385
386 property& operator=(const T& xValue)
387 {
388 SetValue(xValue);
389 return *this;
390 }
391};
392
396class cProperties : public ucom::catwo::object<ant::IProperties>
397{
398public:
399 cProperties() = default;
400 cProperties(const ant::IProperties& oProperties);
401 bool Exists(const char* strName) const override;
402 tResult Get(IProperties& pProperties) const override;
403 tResult Set(const IProperties& pProperties) override;
404 size_t GetSize() const override;
405 tResult GetProperty(const char* strName, IProperty& pProperty) const override;
406 tResult SetProperty(const IProperty& oProperty) override;
407
408 template<typename T>
409 cProperties& CreateProperty(std::string_view strName, const T& xValue)
410 {
411 const auto emplaced = m_oProperties.emplace(std::piecewise_construct, std::forward_as_tuple(strName),
412 std::forward_as_tuple(strName, xValue));
413 if (!emplaced.second)
414 {
415 emplaced.first->second.SetValue(xValue);
416 }
417
418 return static_cast<cProperties&>(emplaced.first->second.GetProperties());
419 }
420
421 tResult SetPropertyByPath(const char* strParentPath, const IProperty& pProperty) override;
422 tResult RemoveProperty(const char* strName) override;
423 tResult RegisterPropertyObserver(const char* strPropertyName, IPropertyObserver& oObserver) override;
424 tResult UnregisterPropertyObserver(IPropertyObserver& oObserver) override;
425
426private:
427 // TODO Switch to std::unordered_map when we have C++20 with is_transparent support in unordered containers.
428 std::map<std::string, cProperty, std::less<>> m_oProperties;
429};
430
431} // namespace spider
432
433using spider::cProperties;
434using spider::cProperty;
435using spider::cPropertyValue;
436using spider::property;
437using spider::get_property_value;
438
439} // namespace base
440} // namespace adtf
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 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 properties_v2.h:31
Definition properties_v2.h:192
Definition properties_v2.h:397
Definition properties_v2.h:31
tResult ToString(IString &&strIToString) const override
tResult Set(const IPropertyValue &oValue) override
tResult FromRaw(const IRawMemory &oRawValue) override
tResult GetType(IString &&strType) const override
tResult ToRaw(IRawMemory &&oRawValue) const override
tResult FromString(const IString &strValueAsString) override
Definition properties_v2.h:192
tResult DetachProperties() override
cProperty & operator=(cProperty &&oProp)
cProperty & operator=(const ant::IProperty &oProp)
tResult GetProperties(ucom::ant::iobject_ptr< const IProperties > &pProperties) const override
get subproperties for readonly access
tResult AttachProperties(const ucom::ant::iobject_ptr< IProperties > &pProperties) override
tResult GetAttachedProperties(IProperty &pProperty) const override
tResult GetProperties(ucom::ant::iobject_ptr< IProperties > &pProperties) override
get subproperties for writing access
cProperty & operator=(const cProperty &oProp)
tResult SetProperties(const IProperties &pProperties) override
will copy given properties
Base object pointer to realize binary compatible reference counting in interface methods.
Definition object_ptr_intf.h:112
Definition object_ptr.h:384
Definition object.h:397
Namespace for all functionality of the ADTF Base SDK provided since v3.18.
Definition properties_v2.h:24
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
Definition property_type_definitions.h:38
Definition properties_v2.h:37
std::string strValue
Definition properties_v2.h:45
std::string strType
Definition properties_v2.h:41