ADTF
Loading...
Searching...
No Matches
property_helper.h
Go to the documentation of this file.
1
7#pragma once
8
11#include <adtfbase/properties.h>
12#include <adtfbase/property.h>
13#include <type_traits>
14
15namespace adtf
16{
17namespace base
18{
19namespace detail
20{
21template<typename PropertyType>
22void call_callback(const ant::IProperty& oProperty, const std::function<void(const char*, PropertyType)>& fnCallback)
23{
24 oProperty.GetName(cStringRedirect(
25 [&](const IString& strName)
26 {
27 fnCallback(strName.Get(), adtf::base::get_property_value<std::decay_t<PropertyType>>(*oProperty.GetValue()));
29 }));
30}
31
32template<typename PropertyType>
33tResult get_property_value_from_property(const IProperty& oProperty, PropertyType& oFoundValue)
34{
35 if (const auto oResult = adtf::base::get_property_value(*oProperty.GetValue(), oFoundValue); IS_FAILED(oResult))
36 {
37 std::string strPropertyName;
38 RETURN_IF_FAILED(oProperty.GetName(adtf_string_intf(strPropertyName)));
39
40 const auto oExtendedResult = ADTF_UTIL_COMPOSED_RESULT(oResult, "Property: %s", strPropertyName.c_str());
41 LOG_WARNING("%s", oExtendedResult.GetDescription());
42
43 return oExtendedResult;
44 }
45
47}
48
49} // namespace detail
50
56std::pair<std::string, std::string> split_parents_and_leaf(const char* strPropertyPathAndName);
57
65tResult create_property_tree(ant::IProperties& oProperties, const char* strPath);
66
67using tPropertyVisitorCallback = std::function<void(const ant::IProperty& oProperty)>;
68
74void visit_properties(const ant::IProperties& oProperties, const tPropertyVisitorCallback& fnCallback);
75
82template<typename SubPropertyType>
83void visit_properties(const ant::IProperties& oProperties,
84 const std::function<void(const char*, SubPropertyType)>& fnCallback)
85{
86 visit_properties(oProperties, std::bind(detail::call_callback<SubPropertyType>, std::placeholders::_1, fnCallback));
87}
88
94void visit_sub_properties(const ant::IProperty& oProperty, const tPropertyVisitorCallback& fnCallback);
95
102template<typename SubPropertyType>
104 const std::function<void(const char*, SubPropertyType)>& fnCallback)
105{
106 visit_sub_properties(oProperty,
107 std::bind(detail::call_callback<SubPropertyType>, std::placeholders::_1, fnCallback));
108}
109
117 const char* strPropertyName,
118 const tPropertyVisitorCallback& fnCallback);
119
127template<typename SubPropertyType>
129 const char* strPropertyName,
130 const std::function<void(const char*, SubPropertyType)>& fnCallback)
131{
132 visit_sub_properties(oProperties, strPropertyName,
133 std::bind(detail::call_callback<SubPropertyType>, std::placeholders::_1, fnCallback));
134}
135
136tResult visit_property(const ant::IProperties& oProperties,
137 const char* strPropertyName,
138 const std::function<tResult(const IProperty& oProperty)>& fnCallback);
139
140tResult visit_property_by_path(const ant::IProperties& oProperties,
141 const char* strPropertyPathAndName,
142 const std::function<tResult(const IProperty& oProperty)>& fnCallback);
143
157template<typename VALUETYPE>
158tResult set_property(ant::IProperties& oProperties, const char* strNameOfValue, VALUETYPE oValue)
159{
160 return oProperties.SetProperty(property<VALUETYPE>(strNameOfValue, oValue));
161}
162
175inline tResult set_property(ant::IProperties& oProperties, const char* strNameOfValue, const char* poValue)
176{
177 return set_property<std::string>(oProperties, strNameOfValue, poValue);
178}
179
192template<typename T>
193T get_property(const ant::IProperties& oProperties, const char* strNameOfValue, const T& oDefaultValue)
194{
195 T oFoundValue;
196
197 const auto oResult = visit_property(oProperties, strNameOfValue, [&](const IProperty& oProperty) -> tResult
198 { return detail::get_property_value_from_property(oProperty, oFoundValue); });
199 if (IS_FAILED(oResult))
200 {
201 return oDefaultValue;
202 }
203
204 return oFoundValue;
205}
206
218template<typename VALUETYPE>
219VALUETYPE get_property(const ant::IProperties& oProperties, const char* strNameOfValue)
220{
221 return get_property<VALUETYPE>(oProperties, strNameOfValue, VALUETYPE());
222}
223
224template<typename VALUETYPE>
225tResult set_property_by_path(ant::IProperties& oProperties, const char* strPathAndName, VALUETYPE oValue)
226{
227 util::cString strHelper(strPathAndName);
228 size_t nLastSlashPosition = strHelper.RFind('/');
229 if (nLastSlashPosition == util::cString::InvalidPos)
230 {
231 return set_property<VALUETYPE>(oProperties, strPathAndName, oValue);
232 }
233
234 util::cString strParent = strHelper.Mid(0, nLastSlashPosition);
235 return oProperties.SetPropertyByPath(strParent, property<VALUETYPE>(strHelper.Mid(nLastSlashPosition + 1), oValue));
236}
237
238template<typename T>
239T get_property_by_path(const ant::IProperties& oProperties, const char* strPathAndName, const T& oDefaultValue)
240{
241 T oFoundValue;
242
243 const auto oResult =
244 visit_property_by_path(oProperties, strPathAndName, [&](const IProperty& oProperty) -> tResult
245 { return detail::get_property_value_from_property(oProperty, oFoundValue); });
246 if (IS_FAILED(oResult))
247 {
248 return oDefaultValue;
249 }
250
251 return oFoundValue;
252}
253
254template<typename VALUETYPE>
255VALUETYPE get_property_by_path(const ant::IProperties& oProperties, const char* strPathAndName)
256{
257 return get_property_by_path(oProperties, strPathAndName, VALUETYPE());
258}
259
268 const char* strPropertyPathAndName,
269 ant::IProperty& oProperty);
270
274class cPropertiesHelper : public ucom::object<IProperties>
275{
276public:
280 tResult SetProperty(const IProperty& /*oProperty*/) = 0;
281
282public:
283 bool Exists(const char* /*strName*/) const override
284 {
285 return false;
286 }
287
288 tResult GetProperty(const char* /*strName*/, IProperty& /*oProperty*/) const override
289 {
290 RETURN_ERROR(ERR_NOT_IMPL);
291 }
292
293 tResult SetPropertyByPath(const char* /*strParentPath*/, const IProperty& /*pProperty*/) override
294 {
295 RETURN_ERROR(ERR_NOT_IMPL);
296 }
297
298 tResult Set(const IProperties& /*oProperties*/) override
299 {
300 RETURN_ERROR(ERR_NOT_IMPL);
301 }
302
303 tResult Get(IProperties& /*oProperties*/) const override
304 {
305 RETURN_ERROR(ERR_NOT_IMPL);
306 }
307
308 size_t GetSize() const override
309 {
310 return 0;
311 }
312
313 tResult RemoveProperty(const char* /*strName*/) override
314 {
315 RETURN_ERROR(ERR_NOT_IMPL);
316 }
317
318 tResult RegisterPropertyObserver(const char* /*strPropertyName*/, IPropertyObserver& /*oObserver*/) override
319 {
320 RETURN_ERROR(ERR_NOT_IMPL);
321 }
322
323 tResult UnregisterPropertyObserver(IPropertyObserver& /*oObserver*/) override
324 {
325 RETURN_ERROR(ERR_NOT_IMPL);
326 }
327};
328
329} // namespace base
330} // namespace adtf
#define LOG_WARNING(...)
Logs a warning message.
Definition log.h:465
#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:775
#define ADTF_UTIL_COMPOSED_RESULT(_result,...)
for internal use
Definition result.h:68
#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
Definition property_intf.h:213
Definition property_intf.h:119
Definition property_helper.h:275
tResult SetProperty(const IProperty &)=0
Definition property.h:337
Definition object.h:397
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
tResult get_property_object_by_path(const ant::IProperties &oProperties, const char *strPropertyPathAndName, ant::IProperty &oProperty)
std::pair< std::string, std::string > split_parents_and_leaf(const char *strPropertyPathAndName)
tResult create_property_tree(ant::IProperties &oProperties, const char *strPath)
tResult set_property(ant::IProperties &oProperties, const char *strNameOfValue, VALUETYPE oValue)
Definition property_helper.h:158
void visit_properties(const ant::IProperties &oProperties, const tPropertyVisitorCallback &fnCallback)
void visit_sub_properties(const ant::IProperty &oProperty, const tPropertyVisitorCallback &fnCallback)
T get_property(const ant::IProperties &oProperties, const char *strNameOfValue, const T &oDefaultValue)
Definition property_helper.h:193
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