IceProperties.h
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 #pragma once
26 
27 #include <Ice/Properties.h>
28 
29 #include <vector>
30 #include <map>
31 #include <string>
32 
33 #define INHERITANCE_KEYWORD "inheritFrom"
34 #define STRINGREPLACEMENT_NAMESPACE "Variables"
35 
36 namespace armarx
37 {
38  class IceProperties;
39 
40  /**
41  * @ingroup properties
42  *
43  * @class IceProperties
44  * @brief IceProperties stores ice properties and resolves property inheritance.
45  *
46  * This Ice property container supports inheritance. That is, a namespace can inherit all
47  * properties defined within another namespace. Inherited properties can also be overridden
48  * if redefined.
49  *
50  * Additionally, there exists a stringreplacement namespace "Variables", which can be used to define variables that
51  * will be searched in the property values of namespaces that inherit from the "Variables" namespace. In case a
52  * property has a matching string, this string is replaced with the value of the global variable.
53  *
54  * @code <namespace>.inheritFrom = <namespace>@endcode
55  *
56  * Example config:
57  *
58  * @code
59  *
60  * ArmarX.CommonProperties.FrameRate = 30
61  * ArmarX.CommonProperties.ColorMode = HSV
62  * ArmarX.CommonProperties.ShutterSpeed = 0.003
63  * ArmarX.CommonProperties.Aperture = 1.2
64  * ArmarX.CommonProperties.WhiteBalance = Auto
65  * ArmarX.CommonProperties.Metering = CenteredMean
66  * ArmarX.CommonProperties.Resolution = 640x480
67  * ArmarX.CommonProperties.CropFactor = 1.5
68  *
69  * Variables.Prefix = Local
70  *
71  * VisionX.MyCapturer1.inheritFrom = ArmarX.CommonProperties
72  * VisionX.MyCapturer1.inheritFrom = Variables
73  * VisionX.MyCapturer1.ColorMode = RGB
74  * VisionX.MyCapturer1.ObjectName = {Prefix}_MyCapturer1
75  *
76  * VisionX.MyCapturer2.inheritFrom = ArmarX.CommonProperties
77  * VisionX.MyCapturer2.ShutterSpeed = 0.001
78  *
79  * VisionX.MyCapturer3.FrameRate = 60
80  * VisionX.MyCapturer3.inheritFrom = ArmarX.CommonProperties
81  * @endcode
82  *
83  * The result is:
84  *
85  * @code
86  * ArmarX.CommonProperties.FrameRate = 30
87  * ...
88  * ...
89  * ArmarX.CommonProperties.CropFactor = 1.5
90  *
91  * VisionX.MyCapturer1.FrameRate = 30
92  * VisionX.MyCapturer1.ColorMode = RGB
93  * VisionX.MyCapturer1.ShutterSpeed = 0.003
94  * VisionX.MyCapturer1.Aperture = 1.2
95  * VisionX.MyCapturer1.WhiteBalance = Auto
96  * VisionX.MyCapturer1.Metering = CenteredMean
97  * VisionX.MyCapturer1.Resolution = 640x480
98  * VisionX.MyCapturer1.CropFactor = 1.5
99  * VisionX.MyCapturer1.ObjectName = Local_MyCapturer1
100  *
101  * VisionX.MyCapturer2.FrameRate = 30
102  * VisionX.MyCapturer2.ColorMode = HSV
103  * VisionX.MyCapturer2.ShutterSpeed = 0.001
104  * VisionX.MyCapturer2.Aperture = 1.2
105  * VisionX.MyCapturer2.WhiteBalance = Auto
106  * VisionX.MyCapturer2.Metering = CenteredMean
107  * VisionX.MyCapturer2.Resolution = 640x480
108  * VisionX.MyCapturer2.CropFactor = 1.5
109  *
110  * VisionX.MyCapturer3.FrameRate = 60
111  * VisionX.MyCapturer3.ColorMode = HSV
112  * VisionX.MyCapturer3.ShutterSpeed = 0.003
113  * VisionX.MyCapturer3.Aperture = 1.2
114  * VisionX.MyCapturer3.WhiteBalance = Auto
115  * VisionX.MyCapturer3.Metering = CenteredMean
116  * VisionX.MyCapturer3.Resolution = 640x480
117  * VisionX.MyCapturer3.CropFactor = 1.5
118  * @endcode
119  */
121  public Ice::Properties
122  {
123  public:
124  static Ice::PropertiesPtr create(const Ice::PropertiesPtr& iceProperties = nullptr);
125 
126  static Ice::PropertiesPtr create(Ice::StringSeq& propertySeq,
127  const Ice::PropertiesPtr& iceProperties = nullptr);
128 
129  static Ice::PropertiesPtr create(int& argn,
130  char* argv[],
131  const Ice::PropertiesPtr& iceProperties = nullptr);
132  public:
133  /**
134  * Ice property container constructor
135  */
136  explicit IceProperties(const Ice::PropertiesPtr iceProperties);
138 
139  /**
140  * Ice property container desctructor
141  */
142  ~IceProperties() override;
143 
144  /**
145  * @see Ice::Properties::getProperty()
146  */
147  ::std::string getProperty(const ::std::string& name) ICE_NOEXCEPT override;
148 
149  /**
150  * @see Ice::Properties::getPropertyWithDefault()
151  */
152  ::std::string getPropertyWithDefault(const ::std::string& name,
153  const ::std::string& defaultValue) ICE_NOEXCEPT override;
154  /**
155  * @see Ice::Properties::getPropertyAsInt()
156  */
157  ::Ice::Int getPropertyAsInt(const ::std::string& name) ICE_NOEXCEPT override;
158 
159  /**
160  * @see Ice::Properties::getPropertyAsIntWithDefault()
161  */
162  ::Ice::Int getPropertyAsIntWithDefault(const ::std::string& name,
163  ::Ice::Int defaultValue) ICE_NOEXCEPT override;
164  /**
165  * @see Ice::Properties::getPropertyAsList()
166  */
167  ::Ice::StringSeq getPropertyAsList(const ::std::string& name) ICE_NOEXCEPT override;
168 
169  /**
170  * @see Ice::Properties::getPropertyAsListWithDefault()
171  */
172  ::Ice::StringSeq getPropertyAsListWithDefault(const ::std::string& name,
173  const ::Ice::StringSeq& defaultValue) ICE_NOEXCEPT override;
174  /**
175  * @see Ice::Properties::getPropertiesForPrefix()
176  */
177  ::Ice::PropertyDict getPropertiesForPrefix(const ::std::string& prefix) ICE_NOEXCEPT override;
178 
179  /**
180  * @see Ice::Properties::setProperty()
181  */
182  void setProperty(const ::std::string& name, const ::std::string& value) override;
183 
184  /**
185  * @see Ice::Properties::getCommandLineOptions()
186  */
187  ::Ice::StringSeq getCommandLineOptions() ICE_NOEXCEPT override;
188 
189  /**
190  * @see Ice::Properties::parseCommandLineOptions()
191  */
192  ::Ice::StringSeq parseCommandLineOptions(const ::std::string& prefix,
193  const ::Ice::StringSeq& options) override;
194  /**
195  * @see Ice::Properties::parseIceCommandLineOptions()
196  */
197  ::Ice::StringSeq parseIceCommandLineOptions(const ::Ice::StringSeq& options) override;
198 
199  /**
200  * @see Ice::Properties::load()
201  */
202  void load(const ::std::string& fileName) override;
203 
204  /**
205  * @see Ice::Properties::clone()
206  */
207  ::Ice::PropertiesPtr clone() ICE_NOEXCEPT override;
208 
209  public:
211  public IceUtil::Shared
212  {
213  public:
214  using NamespaceMap = std::map<std::string, bool>;
215 
216  public:
217  ~InheritanceSolver() override;
218 
219  /**
220  * Resolves all inheritance specifications made using the config notation
221  * "<namespace>.inheritFrom = <namespace>" while <namespace> is <domain>.<object-name>.
222  *
223  * @param properties The properties with inheritance specifications. The resolved
224  * inheritance is done within this property contain as well.
225  */
226  void resolveInheritance(Ice::PropertiesPtr& properties);
227 
228  /**
229  * Extracts all existing namespace within the passed property container. More simply
230  * it extracts all existing prefixes.
231  *
232  * @param [in] properties Input property container
233  * @param [out] namespaces Extracted properties
234  */
235  void extractNamespaces(const Ice::PropertiesPtr& properties,
236  NamespaceMap& namespaces);
237 
238  /**
239  * Resolves namespace inheritance for a specific namespace. This will resolve all
240  * inheritance specifications of it's parent namespace.
241  *
242  * @param [in] childNamespace Namespace requiring inheritance
243  * @param properties Input and output properties
244  */
245  void resolveNamespaceInheritance(const std::string& childNamespace,
246  Ice::PropertiesPtr& properties);
247 
248  /**
249  * Performs the inheritance of properties from parentNamespace to childNamespace.
250  *
251  * @param [in] childNamespace Namespace requiring inheritance
252  * @param [in] parentNamespace Parent namespace to inherit from.
253  * @param properties Input and output properties
254  * @param heritageLine Heritage line vector. This is used to detect
255  * inheritance cycles.
256  */
257  void inherit(const std::string& childNamespace,
258  const std::string& parentNamespace,
259  Ice::PropertiesPtr& properties,
260  std::vector<std::string> heritageLine);
261 
262  /**
263  * Checks whether the specified namespace has a parent which it inherits from.
264  * The inheritance is specified via a property as follows:
265  * <childNamespace>.inheritFrom = <parentNamespace>.
266  *
267  * <childNamespace> can be anything. Usually it consists of the <domain> and
268  * <object-name>.
269  *
270  * @param [in] childNamespace Child namespace.
271  * @param [in] properties Input properties containing inheritance specifications.
272  */
273  bool hasParent(const std::string& childNamespace, const Ice::PropertiesPtr& properties);
274 
275  /**
276  * Returns the parent namespace of a child namespace
277  *
278  * @param [in] childNamespace Child namespace.
279  * @param [in] properties Input properties containing inheritance specifications.
280  *
281  * @return parent namespace, otherwise an empty string
282  */
283  std::string getParent(const std::string& childNamespace,
284  const Ice::PropertiesPtr& properties);
285 
286  /**
287  * Checks whether the specified namespace exists already in the heritageLine
288  *
289  * @param [in] heritageLine Heritage line vector
290  * @param [in] namespace The namespace to look for
291  *
292  * @return true if namespace is in heritageLine, otherwise false
293  */
294  bool isInHeritageLine(const std::vector<std::string>& heritageLine,
295  const std::string& namespace_);
296 
297  /**
298  * Removes the namespace of the property name.
299  *
300  * "<namespace>.<property-name> = <propertyValue>" becomes "<property-name> =
301  * <property-value>".
302  *
303  * @return property name without the namespace.
304  */
305  std::string stripNamespace(const std::string& propertyName,
306  const std::string& namespace_);
307  };
308 
309  public:
311 
312  public:
313  /**
314  * Returns the currently used inheritance solver
315  *
316  * @return inheritance solver pointer
317  */
319 
320  /**
321  * Sets an inheritance solver in case of testing or using a non-default solver.
322  */
323  virtual void setInheritanceSolver(const InheritanceSolverPtr& inheritanceSolver);
324 
325  private:
326  /**
327  * Flags the property contain that it has been changed. This is required to resolve
328  * possible inheritance.
329  */
330  void setModified(bool modified);
331 
332  /**
333  * Checks whether the container has been modified.
334  *
335  * @return true if at least one property has been set ot altered, otherwise false
336  */
337  bool isModified();
338 
339  /**
340  * Updates the container by resolving potential inheritance specifications.
341  */
342  void update();
343 
344  private:
345  /**
346  * Use internal composite to manage and store properties instead of calling directly
347  * the parent functions. This is due to provide the create factory methods in the
348  * same fashion as provided by Ice::createProperties.
349  */
350  Ice::PropertiesPtr internalProperties;
351 
352  /**
353  * Modification flag
354  */
355  bool modified;
356 
357  /**
358  * Property inheritance solver tool.
359  */
360  InheritanceSolverPtr inheritanceSolver;
361  };
362 }
363 
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::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
armarx::IceProperties::IceProperties
IceProperties(const Ice::PropertiesPtr iceProperties)
Ice property container constructor.
Definition: IceProperties.cpp:67
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
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
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::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
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