IceProperties.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Jan Issac (jan dot issac at mail dot com)
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "IceProperties.h"
26 #include <Ice/Initialize.h>
29 
30 namespace armarx
31 {
33  {
34  Ice::StringSeq strSeq;
35 
36  return new IceProperties(Ice::createProperties(strSeq, iceProperties));
37  }
38 
39  Ice::PropertiesPtr IceProperties::create(Ice::StringSeq& propertySeq,
40  const Ice::PropertiesPtr& iceProperties)
41  {
42  Ice::PropertiesPtr armarxIceProperties =
43  new IceProperties(Ice::createProperties(propertySeq, iceProperties));
44 
45  // adds all options and properties
46  armarxIceProperties->parseCommandLineOptions(std::string(), propertySeq);
47 
48  return armarxIceProperties;
49  }
50 
52  char* argv[],
53  const Ice::PropertiesPtr& iceProperties)
54  {
55  Ice::PropertiesPtr armarxIceProperties =
56  new IceProperties(Ice::createProperties(argn, argv, iceProperties));
57 
58  Ice::StringSeq propertySeq = Ice::argsToStringSeq(argn, argv);
59 
60  // adds all options and properties
61  armarxIceProperties->parseCommandLineOptions(std::string(), propertySeq);
62 
63  return armarxIceProperties;
64  }
65 
66 
68  internalProperties(iceProperties),
69  modified(true)
70  {
71  inheritanceSolver = new InheritanceSolver();
72  }
73 
75  Shared(source)
76  {
77  internalProperties = source.internalProperties->clone();
78  modified = source.modified;
79  if (source.inheritanceSolver)
80  {
81  inheritanceSolver = new InheritanceSolver(*source.inheritanceSolver);
82  }
83  }
84 
86  {
87 
88  }
89 
90  std::string IceProperties::getProperty(const ::std::string& name) ICE_NOEXCEPT
91  {
92  update();
93 
94  return internalProperties->getProperty(name);
95  }
96 
97  std::string IceProperties::getPropertyWithDefault(const ::std::string& name,
98  const ::std::string& defaultValue) ICE_NOEXCEPT
99  {
100  update();
101 
102  return internalProperties->getPropertyWithDefault(name, defaultValue);
103  }
104 
105  Ice::Int IceProperties::getPropertyAsInt(const ::std::string& name) ICE_NOEXCEPT
106  {
107  update();
108 
109  return internalProperties->getPropertyAsInt(name);
110  }
111 
113  ::Ice::Int defaultValue) ICE_NOEXCEPT
114  {
115  update();
116 
117  return internalProperties->getPropertyAsIntWithDefault(name, defaultValue);
118  }
119 
120  Ice::StringSeq IceProperties::getPropertyAsList(const ::std::string& name) ICE_NOEXCEPT
121  {
122  update();
123 
124  return internalProperties->getPropertyAsList(name);
125  }
126 
128  const ::std::string& name, const ::Ice::StringSeq& defaultValue) ICE_NOEXCEPT
129  {
130  update();
131 
132  return internalProperties->getPropertyAsListWithDefault(name, defaultValue);
133  }
134 
135  Ice::PropertyDict IceProperties::getPropertiesForPrefix(const ::std::string& prefix) ICE_NOEXCEPT
136  {
137  update();
138 
139  return internalProperties->getPropertiesForPrefix(prefix);
140  }
141 
142  void IceProperties::setProperty(const ::std::string& name, const ::std::string& value)
143  {
144  internalProperties->setProperty(name, value);
145  setModified(true);
146  }
147 
148  Ice::StringSeq IceProperties::getCommandLineOptions() ICE_NOEXCEPT
149  {
150  update();
151 
152  return internalProperties->getCommandLineOptions();
153  }
154 
155  Ice::StringSeq IceProperties::parseCommandLineOptions(const ::std::string& prefix,
156  const ::Ice::StringSeq& options)
157  {
158  Ice::StringSeq optionSeq = internalProperties->parseCommandLineOptions(prefix, options);
159 
160  setModified(true);
161  update();
162 
163  return optionSeq;
164  }
165 
166  Ice::StringSeq IceProperties::parseIceCommandLineOptions(const ::Ice::StringSeq& options)
167  {
168  Ice::StringSeq optionSeq = internalProperties->parseIceCommandLineOptions(options);
169 
170  setModified(true);
171  update();
172 
173  return optionSeq;
174  }
175 
176  void IceProperties::load(const ::std::string& fileName)
177  {
178  internalProperties->load(fileName);
179 
180  setModified(true);
181  }
182 
184  {
185  // return new IceProperties(*this);
186  return internalProperties->clone();
187  }
188 
189  void IceProperties::setModified(bool mod)
190  {
191  this->modified = mod;
192  }
193 
194  bool IceProperties::isModified()
195  {
196  return modified;
197  }
198 
199  void IceProperties::update()
200  {
201  if (isModified())
202  {
203  if (inheritanceSolver.get() != nullptr)
204  {
205  inheritanceSolver->resolveInheritance(internalProperties);
206  }
207  }
208 
209  setModified(false);
210  }
211 
213  {
214  return inheritanceSolver;
215  }
216 
219  {
220  this->inheritanceSolver = solver;
221  }
222 
224  {
225 
226  }
227 
229  {
230  NamespaceMap namespaces;
231 
232  extractNamespaces(properties, namespaces);
233 
234  NamespaceMap::const_iterator namespacesIter = namespaces.begin();
235 
236  while (namespacesIter != namespaces.end())
237  {
238  resolveNamespaceInheritance(namespacesIter->first, properties);
239 
240  ++namespacesIter;
241  }
242  }
243 
245  NamespaceMap& namespaces)
246  {
247  namespaces.clear();
248 
249  Ice::PropertyDict propertyDict = properties->getPropertiesForPrefix(std::string());
250 
251  // extract namespaces
252  Ice::PropertyDict::iterator dictIter = propertyDict.begin();
253 
254  while (dictIter != propertyDict.end())
255  {
256  std::string propertyFullName = dictIter->first;
257 
258  unsigned nameDelimiter = propertyFullName.find_last_of(".");
259  std::string propertyNamespace = propertyFullName.substr(0, nameDelimiter);
260  namespaces[propertyNamespace] = true;
261 
262  ++dictIter;
263  }
264  }
265 
266  bool IceProperties::InheritanceSolver::hasParent(const std::string& childNamespace,
267  const Ice::PropertiesPtr& properties)
268  {
269  std::string inheritanceProperty = childNamespace + "." + INHERITANCE_KEYWORD;
270 
271  if (properties->getPropertyWithDefault(
272  inheritanceProperty, "::NOT-SET::").compare("::NOT-SET::") == 0)
273  {
274  return false;
275  }
276 
277  return true;
278  }
279 
280  std::string IceProperties::InheritanceSolver::getParent(const std::string& childNamespace,
281  const Ice::PropertiesPtr& properties)
282  {
283  const std::string inheritanceProperty = childNamespace + "." + INHERITANCE_KEYWORD;
284 
285  return properties->getProperty(inheritanceProperty);
286  }
287 
288  bool IceProperties::InheritanceSolver::isInHeritageLine(const std::vector<std::string>& heritageLine,
289  const std::string& namespace_)
290  {
291  std::vector<std::string>::const_iterator heritageLineIter = heritageLine.begin();
292 
293  while (heritageLineIter != heritageLine.end())
294  {
295  if ((*heritageLineIter).compare(namespace_) == 0)
296  {
297  return true;
298  }
299 
300  ++heritageLineIter;
301  }
302 
303  return false;
304  }
305 
306  std::string IceProperties::InheritanceSolver::stripNamespace(const std::string& propertyName,
307  const std::string& namespace_)
308  {
309  return propertyName.substr(namespace_.length() + 1);
310  }
311 
313  Ice::PropertiesPtr& properties)
314  {
315  std::vector<std::string> heritageLine;
316 
317  if (hasParent(childNamespace, properties))
318  {
319  inherit(childNamespace, getParent(childNamespace, properties), properties, heritageLine);
320  }
321  }
322 
323  void IceProperties::InheritanceSolver::inherit(const std::string& childNamespace,
324  const std::string& parentNamespace,
325  Ice::PropertiesPtr& properties,
326  std::vector<std::string> heritageLine)
327  {
328  heritageLine.push_back(childNamespace);
329 
330  // check parent inheritance
331  if (hasParent(parentNamespace, properties))
332  {
333  if (isInHeritageLine(heritageLine, parentNamespace))
334  {
335  heritageLine.push_back(parentNamespace);
337  }
338 
339  inherit(parentNamespace, getParent(parentNamespace, properties), properties, heritageLine);
340  }
341 
342  // perform actual inheritance
343  Ice::PropertyDict parentProperties = properties->getPropertiesForPrefix(parentNamespace);
344 
345  Ice::PropertyDict::iterator parentPropIter = parentProperties.begin();
346 
347  while (parentPropIter != parentProperties.end())
348  {
349  // look through all child properties, if the parent variable is referred to as {parentVariable} in the
350  // propertyValue
351  if (parentNamespace == STRINGREPLACEMENT_NAMESPACE)
352  {
353  for (auto property : properties->getPropertiesForPrefix(childNamespace))
354  {
355  std::string propertyVariable = "{" + stripNamespace(parentPropIter->first, parentNamespace) + "}";
356  auto index = property.second.find(propertyVariable);
357  if (index != std::string::npos)
358  {
359  std::string newValue = property.second.replace(index, propertyVariable.size(), parentPropIter->second);
360  properties->setProperty(property.first, newValue);
361  }
362  }
363  }
364  else
365  {
366  std::string newPropertyName = childNamespace + "." +
367  stripNamespace(parentPropIter->first, parentNamespace);
368 
369  // Check whether it has been already set. Defined properties override inherited ones.
370  if (properties->getProperty(newPropertyName).empty())
371  {
372  properties->setProperty(newPropertyName, parentPropIter->second);
373  }
374  }
375  ++parentPropIter;
376  }
377 
378  // remove inheritance tag
379  properties->setProperty(childNamespace + "." + INHERITANCE_KEYWORD, "");
380  }
381 }
armarx::IceProperties::InheritanceSolver::NamespaceMap
std::map< std::string, bool > NamespaceMap
Definition: IceProperties.h:214
armarx::IceProperties::InheritanceSolver::stripNamespace
std::string stripNamespace(const std::string &propertyName, const std::string &namespace_)
Removes the namespace of the property name.
Definition: IceProperties.cpp:306
armarx::exceptions::local::PropertyInheritanceCycleException
This exception is thrown if a property inheritance cycle has been encountered.
Definition: PropertyInheritanceCycleException.h:39
armarx::IceProperties::clone
::Ice::PropertiesPtr clone() ICE_NOEXCEPT override
Definition: IceProperties.cpp:183
armarx::IceProperties::InheritanceSolver::extractNamespaces
void extractNamespaces(const Ice::PropertiesPtr &properties, NamespaceMap &namespaces)
Extracts all existing namespace within the passed property container.
Definition: IceProperties.cpp:244
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::IceProperties::IceProperties
IceProperties(const Ice::PropertiesPtr iceProperties)
Ice property container constructor.
Definition: IceProperties.cpp:67
PropertyInheritanceCycleException.h
armarx::IceProperties::~IceProperties
~IceProperties() override
Ice property container desctructor.
Definition: IceProperties.cpp:85
armarx::IceProperties::getPropertyAsListWithDefault
::Ice::StringSeq getPropertyAsListWithDefault(const ::std::string &name, const ::Ice::StringSeq &defaultValue) ICE_NOEXCEPT override
Definition: IceProperties.cpp:127
armarx::IceProperties::parseIceCommandLineOptions
::Ice::StringSeq parseIceCommandLineOptions(const ::Ice::StringSeq &options) override
Definition: IceProperties.cpp:166
IceInternal::Handle< ::Ice::Properties >
armarx::IceProperties::InheritanceSolver::isInHeritageLine
bool isInHeritageLine(const std::vector< std::string > &heritageLine, const std::string &namespace_)
Checks whether the specified namespace exists already in the heritageLine.
Definition: IceProperties.cpp:288
armarx::IceProperties::InheritanceSolver::resolveNamespaceInheritance
void resolveNamespaceInheritance(const std::string &childNamespace, Ice::PropertiesPtr &properties)
Resolves namespace inheritance for a specific namespace.
Definition: IceProperties.cpp:312
STRINGREPLACEMENT_NAMESPACE
#define STRINGREPLACEMENT_NAMESPACE
Definition: IceProperties.h:34
armarx::IceProperties::getCommandLineOptions
::Ice::StringSeq getCommandLineOptions() ICE_NOEXCEPT override
Definition: IceProperties.cpp:148
armarx::IceProperties::InheritanceSolver::~InheritanceSolver
~InheritanceSolver() override
Definition: IceProperties.cpp:223
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::IceProperties::InheritanceSolver::inherit
void inherit(const std::string &childNamespace, const std::string &parentNamespace, Ice::PropertiesPtr &properties, std::vector< std::string > heritageLine)
Performs the inheritance of properties from parentNamespace to childNamespace.
Definition: IceProperties.cpp:323
Ice::createProperties
Ice::PropertiesPtr createProperties()
armarx::IceProperties::getPropertyAsInt
::Ice::Int getPropertyAsInt(const ::std::string &name) ICE_NOEXCEPT override
Definition: IceProperties.cpp:105
armarx::IceProperties::load
void load(const ::std::string &fileName) override
Definition: IceProperties.cpp:176
armarx::IceProperties::create
static Ice::PropertiesPtr create(const Ice::PropertiesPtr &iceProperties=nullptr)
Definition: IceProperties.cpp:32
armarx::IceProperties::parseCommandLineOptions
::Ice::StringSeq parseCommandLineOptions(const ::std::string &prefix, const ::Ice::StringSeq &options) override
Definition: IceProperties.cpp:155
armarx::IceProperties::setInheritanceSolver
virtual void setInheritanceSolver(const InheritanceSolverPtr &inheritanceSolver)
Sets an inheritance solver in case of testing or using a non-default solver.
Definition: IceProperties.cpp:217
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::IceProperties::setProperty
void setProperty(const ::std::string &name, const ::std::string &value) override
Definition: IceProperties.cpp:142
armarx::IceProperties::getProperty
::std::string getProperty(const ::std::string &name) ICE_NOEXCEPT override
Definition: IceProperties.cpp:90
armarx::IceProperties::InheritanceSolver::resolveInheritance
void resolveInheritance(Ice::PropertiesPtr &properties)
Resolves all inheritance specifications made using the config notation "<namespace>....
Definition: IceProperties.cpp:228
armarx::IceProperties
IceProperties stores ice properties and resolves property inheritance.
Definition: IceProperties.h:120
armarx::IceProperties::getPropertyWithDefault
::std::string getPropertyWithDefault(const ::std::string &name, const ::std::string &defaultValue) ICE_NOEXCEPT override
Definition: IceProperties.cpp:97
armarx::IceProperties::InheritanceSolver
Definition: IceProperties.h:210
armarx::IceProperties::InheritanceSolver::getParent
std::string getParent(const std::string &childNamespace, const Ice::PropertiesPtr &properties)
Returns the parent namespace of a child namespace.
Definition: IceProperties.cpp:280
IceUtil::Handle< InheritanceSolver >
armarx::IceProperties::getInheritanceSolver
virtual InheritanceSolverPtr getInheritanceSolver()
Returns the currently used inheritance solver.
Definition: IceProperties.cpp:212
armarx::IceProperties::getPropertyAsList
::Ice::StringSeq getPropertyAsList(const ::std::string &name) ICE_NOEXCEPT override
Definition: IceProperties.cpp:120
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::IceProperties::getPropertiesForPrefix
::Ice::PropertyDict getPropertiesForPrefix(const ::std::string &prefix) ICE_NOEXCEPT override
Definition: IceProperties.cpp:135
armarx::IceProperties::InheritanceSolver::hasParent
bool hasParent(const std::string &childNamespace, const Ice::PropertiesPtr &properties)
Checks whether the specified namespace has a parent which it inherits from.
Definition: IceProperties.cpp:266
Logging.h
IceProperties.h
INHERITANCE_KEYWORD
#define INHERITANCE_KEYWORD
Definition: IceProperties.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::IceProperties::getPropertyAsIntWithDefault
::Ice::Int getPropertyAsIntWithDefault(const ::std::string &name, ::Ice::Int defaultValue) ICE_NOEXCEPT override
Definition: IceProperties.cpp:112