ADTF Display Toolbox
Loading...
Searching...
No Matches
runtimeconfig.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <adtf_platform_inc.h>
10#include <adtf_base.h>
11
12namespace adtf::disptb::detail
13{
14class cPropertiesToDOMConverter : public adtf::base::cPropertiesHelper
15{
16private:
17 adtf::util::cDOMElement* m_oDomElement;
18 adtf::util::cString m_strPrefix;
19
20public:
21 explicit cPropertiesToDOMConverter(adtf::util::cDOMElement* pConfig, adtf::util::cString strPrefix):
22 m_oDomElement(pConfig), m_strPrefix(strPrefix)
23 {
24 }
25
26 tResult SetProperty(const adtf::base::IProperty& oProperty)
27 {
28 adtf::util::cString strKey;
29 oProperty.GetName(adtf_string_intf(strKey));
30
31 adtf::util::cString strName = m_strPrefix + strKey;
32 adtf::util::cDOMElement* pDOMProp = 0;
33 RETURN_IF_FAILED(m_oDomElement->CreateChild("property", pDOMProp));
34 pDOMProp->SetAttribute("name", strName);
35
36 adtf::util::cString strValue;
37 oProperty.GetValue()->ToString(adtf_string_intf(strValue));
38
39 pDOMProp->SetAttribute("value", strValue);
40
41 RETURN_NOERROR;
42 }
43};
44
45class cPropertiesNameExtractor : public adtf::base::cPropertiesHelper
46{
47private:
48 adtf::ucom::object_ptr<IProperties> m_oProperties;
49 std::vector<adtf::util::cString>& m_vecKeys;
50
51public:
52 explicit cPropertiesNameExtractor(adtf::ucom::object_ptr<IProperties> pProperties,
53 std::vector<adtf::util::cString>& vecKeys):
54 m_oProperties(pProperties), m_vecKeys(vecKeys)
55 {
56 }
57
58 tResult SetProperty(const adtf::base::IProperty& oProperty)
59 {
60 adtf::util::cString strName;
61 RETURN_IF_FAILED(oProperty.GetName(adtf_string_intf(strName)));
62 m_vecKeys.push_back(strName);
63
64 RETURN_NOERROR;
65 }
66};
67
68inline tResult clear_properties(adtf::ucom::object_ptr<adtf::base::IProperties> pProperties)
69{
70 std::vector<adtf::util::cString> vecKeys;
71 cPropertiesNameExtractor pExtractor(pProperties, vecKeys);
72 pProperties->Get(pExtractor);
73
74 for (const auto& strName : vecKeys)
75 {
76 RETURN_IF_FAILED(pProperties->RemoveProperty(strName));
77 }
78
79 RETURN_NOERROR;
80}
81} // namespace adtf::disptb::detail
82
87class cRuntimeConfigFile : public adtf::ucom::object<adtf::base::configuration<>>
88{
89protected:
91 adtf::util::cFilename m_strConfigFile;
92
93public:
99 cRuntimeConfigFile(const adtf::util::cFilename& strConfigFile): m_strConfigFile(strConfigFile)
100 {
101 }
102
103 virtual ~cRuntimeConfigFile()
104 {
105 }
106
112 tResult Load()
113 {
114 adtf::ucom::object_ptr<adtf::base::IProperties> pProperties;
115 RETURN_IF_FAILED(this->GetProperties(pProperties));
116 RETURN_IF_FAILED(adtf::disptb::detail::clear_properties(pProperties));
117
118 adtf::util::cDOM oDOM;
119 RETURN_IF_FAILED(oDOM.Load(m_strConfigFile));
120
121 adtf::util::cDOMElementRefList lstProperties;
122 if (IS_OK(oDOM.FindNodes("property", lstProperties)))
123 {
124 for (const auto& prop : lstProperties)
125 {
126 adtf::base::property_variable<adtf::util::cString> oPropertyValue(prop->GetAttribute("value").GetPtr());
127 RegisterPropertyVariable(prop->GetAttribute("name").GetPtr(), oPropertyValue);
128 }
129 }
130
131 RETURN_NOERROR;
132 }
133
139 tResult Save()
140 {
141 adtf::util::cDOM oDOM;
142 RETURN_IF_FAILED(StoreProperty("", &oDOM.GetRoot()));
143
144 RETURN_IF_FAILED(oDOM.Save(m_strConfigFile, tTrue));
145 RETURN_NOERROR;
146 }
147
154 {
155 adtf::ucom::object_ptr<adtf::base::IProperties> pProperties;
156 RETURN_IF_FAILED(this->GetProperties(pProperties));
157
158 RETURN_IF_FAILED(adtf::disptb::detail::clear_properties(pProperties));
159 RETURN_NOERROR;
160 }
161
168 tVoid SetConfigFile(const adtf::util::cFilename& strFilename)
169 {
170 m_strConfigFile = strFilename;
171 }
172
178 adtf::util::cFilename GetConfigFile()
179 {
180 return m_strConfigFile;
181 }
182
183private:
192 tResult StoreProperty(adtf::util::cString strPrefix, adtf::util::cDOMElement* pConfig)
193 {
194 adtf::ucom::object_ptr<adtf::base::IProperties> pProperties;
195 this->GetProperties(pProperties);
196
197 // write properties to pConfig
198 adtf::disptb::detail::cPropertiesToDOMConverter oConverter(pConfig, strPrefix);
199 RETURN_IF_FAILED(pProperties->Get(oConverter));
200
201 RETURN_NOERROR;
202 }
203};
204
209{
210public:
217 virtual tResult SaveConfiguration(adtf::base::IConfiguration* pRuntimeConfig) = 0;
218
226 virtual tResult LoadConfiguration(adtf::base::IConfiguration* pRuntimeConfig) = 0;
227};
228
241template<typename T>
242class cRuntimeConfiguration : public T, public IRuntimeConfigurationLoadSave
243{
244protected:
246 adtf::base::property_variable<adtf::util::cFilename> m_strConfigFile;
249
250public:
251 cRuntimeConfiguration();
252 ~cRuntimeConfiguration();
253
259 tResult SaveRuntimeConfigFile();
265 tResult LoadRuntimeConfigFile();
266
267protected:
276 virtual tResult SaveConfiguration(adtf::base::IConfiguration* pRuntimeConfig) = 0;
277
287 virtual tResult LoadConfiguration(adtf::base::IConfiguration* pRuntimeConfig) = 0;
288
289private:
295 adtf::util::cString GetConfigFile();
296};
297
298template<typename T>
299cRuntimeConfiguration<T>::cRuntimeConfiguration():
300 m_strConfigFile("$(ADTF_GRAPH_DIR)/$(THIS_OBJECT_FULL_NAME)"),
301 // config file cant be set at creation because property will be resolved later
302 m_oRuntimeConfig("")
303{
304 m_strConfigFile.SetDescription("Path to configuration file for runtime settings.");
305 T::RegisterPropertyVariable("runtime_config_file_path", m_strConfigFile);
306}
307
308template<typename T>
309cRuntimeConfiguration<T>::~cRuntimeConfiguration()
310{
311}
312
313template<typename T>
315{
316 return static_cast<adtf::util::cFilename>(m_strConfigFile).MakeNativeSlashes();
317}
318
319template<typename T>
321{
322 if (m_oRuntimeConfig.GetConfigFile().Empty)
323 {
324 m_oRuntimeConfig.SetConfigFile(GetConfigFile());
325 }
326
327 RETURN_IF_FAILED(m_oRuntimeConfig.ClearAllProperties());
328
329 RETURN_IF_FAILED(this->SaveConfiguration(&m_oRuntimeConfig));
330 if IS_FAILED (m_oRuntimeConfig.Save())
331 {
332 RETURN_AND_LOG_ERROR_STR(ERR_FAILED, "Runtime configuration could not be stored");
333 }
334 else
335 {
336 LOG_INFO("Runtime configuration was stored to file");
337 }
338 RETURN_NOERROR;
339}
340
341template<typename T>
343{
344 if (!adtf::util::cFileSystem::Exists(GetConfigFile()))
345 {
346 RETURN_ERROR_DESC(ERR_FILE_NOT_FOUND, "Runtime configuration file '%s' does not exist",
347 static_cast<adtf::util::cString>(m_strConfigFile).GetPtr());
348 }
349
350 if (m_oRuntimeConfig.GetConfigFile().Empty)
351 {
352 m_oRuntimeConfig.SetConfigFile(GetConfigFile());
353 }
354
355 RETURN_IF_FAILED(m_oRuntimeConfig.Load());
356
357 if IS_FAILED (this->LoadConfiguration(&m_oRuntimeConfig))
358 {
359 RETURN_ERROR_DESC(ERR_FAILED, "Runtime configuration could not be read");
360 }
361
362 RETURN_NOERROR;
363}
Interface for loading and storing runtime configuration properties to and from passed adtf::base::ICo...
virtual tResult LoadConfiguration(adtf::base::IConfiguration *pRuntimeConfig)=0
Implementation has to get all properties from pRuntimeConfig (contains all loaded properties from run...
virtual tResult SaveConfiguration(adtf::base::IConfiguration *pRuntimeConfig)=0
Implementation has to add all properties to be written to runtime configuration file to pRuntimeConfi...
Class is a adtf::base::configuration that allows the storing and loading of all contained properties ...
tResult Save()
Saves all properties of this instance to the configured xml file.
tResult ClearAllProperties()
Clears all properties currently stored in cRuntimeConfigFile.
cRuntimeConfigFile(const adtf::util::cFilename &strConfigFile)
Constructor.
adtf::util::cFilename m_strConfigFile
path to the xml file
tResult StoreProperty(adtf::util::cString strPrefix, adtf::util::cDOMElement *pConfig)
Stores all properties of this instance in DOM pConfig.
tResult Load()
Loads all properties from the configured xml file and stores it to this instance.
adtf::util::cFilename GetConfigFile()
Setter for xml config file to load and store runtime configuration.
tVoid SetConfigFile(const adtf::util::cFilename &strFilename)
Setter for xml config file to load and store runtime configuration.
adtf::util::cString GetConfigFile()
Getter for path of configuration file.
virtual tResult LoadConfiguration(adtf::base::IConfiguration *pRuntimeConfig)=0
Callback to be implemented by user of runtime_configuration.
tResult SaveRuntimeConfigFile()
Saves all properties to the file configured with adtf-property runtime_config_file_path.
tResult LoadRuntimeConfigFile()
Loads all properties from the file configured with adtf-property runtime_config_file_path.
virtual tResult SaveConfiguration(adtf::base::IConfiguration *pRuntimeConfig)=0
Callback to be implemented by user of runtime_configuration.
adtf::base::property_variable< adtf::util::cFilename > m_strConfigFile
CE property_variable storing path of runtime configuration file.
cRuntimeConfigFile m_oRuntimeConfig
actual runtime configuration