Property.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 gmx dot de)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include "PropertyDefinition.hpp"
28 
30 
31 #include <Ice/LocalObject.h>
32 
33 #include <algorithm> // for min, fill
34 #include <iostream> // for basic_ostream, operator<<, etc
35 #include <limits>
36 #include <map>
37 #include <string> // for basic_string, string, etc
38 #include <typeinfo> // for bad_cast
39 #include <vector> // for vector
40 
41 namespace armarx
42 {
43  // Helper functions to reduce includes
45 
46 
47  /* ====================================================================== */
48  /* === Property Declaration ============================================= */
49  /* ====================================================================== */
50 
51  /**
52  * @ingroup properties
53  *
54  * @class Property
55  * @brief Provides access to Ice properties with extended capabilities.
56  *
57  * The Property template class provides access to Ice config properties
58  * and command line options. Its main capability is to map a string value on
59  * any type that is required and put constrains on the value as well as
60  * on the property itself at the same time. For instance you can specify
61  * that a certain property is required for the component to proceed or a
62  * property may require a specific value syntax or even numeric bounds which
63  * may not be exceeded. These features and few more are supported by the
64  * Property combined with the PropertyDefinition.
65  *
66  * \section properties-outline Outline
67  *
68  * @code
69  * template<typename PropertyType> class Property
70  * {
71  * bool iseSet();
72  * bool isRequired();
73  * PropertyType getValue();
74  * }
75  * @endcode
76  */
77  template <typename PropertyType>
78  class Property
79  {
80  public:
81  /**
82  * Property value map type definition
83  */
86 
87  /**
88  * Property constructor
89  *
90  * @param definition Property specific definition
91  * @param prefix Property prefix such as domain or component name
92  * @param properties Ice properties set which contains all property
93  * strings.
94  *
95  */
97  std::string prefix,
98  Ice::PropertiesPtr properties);
99 
100  /**
101  * Checks whether the property is set
102  *
103  * @return true if property is set, otheriwse false
104  */
105  bool isSet() const;
106 
107  /**
108  * Checks if this property is required
109  *
110  * @return true if set required, otherwise false
111  */
112  bool isRequired() const;
113 
114  /**
115  * Checks if this property is constant or if it can be changed at runtime.
116  *
117  * @return true if property is constant, otherwise false
118  */
119  bool isConstant() const;
120 
121  /**
122  * Returns the property value set in a config file or passed as a
123  * command-line option. If property is not set, the default value is
124  * returned unless the property is required. In the latter case an
125  * exception is thrown indicating that the required property is not set.
126  *
127  * @return Property value of the type <b>PropertyType</b>
128  *
129  * @throw armarx::exceptions::local::ValueRangeExceededException
130  * @throw armarx::exceptions::local::InvalidPropertyValueException
131  */
132  PropertyType getValue();
133 
134  /**
135  * @brief Convenience overload of getValue().
136  * Usage:
137  * \code
138  * std::string val = getProperty<std::string>("myProp");
139  * \endcode
140  */
141  operator PropertyType()
142  {
143  ARMARX_TRACE;
144  return getValue();
145  }
146 
147  template<class T = PropertyType>
148  std::enable_if_t<std::is_same_v<T, std::string>, T>
150  {
151  ARMARX_TRACE;
153  }
154 
155  template<class T = PropertyType>
156  std::enable_if_t<std::is_same_v<T, std::vector<std::string>>, T>
158  {
159  auto vals = getValue();
160  for (auto& val : vals)
161  {
163  }
164  return vals;
165  }
166 
167  private:
168  /* = Implementation details ========================================= */
169 
170  template <int> struct Qualifier
171  {
172  Qualifier(int) {}
173  };
174 
175  private:
176 
177  /// Property definition.
178  PropertyDefinition<PropertyType> definition;
179 
180  /// Property prefix such as domain name.
181  std::string prefix;
182 
183  /// Ice property container.
184  Ice::PropertiesPtr properties;
185  };
186 
187 
188  /* ====================================================================== */
189  /* === Property Implementation ========================================== */
190  /* ====================================================================== */
191 
192  template <typename PropertyType>
195  std::string prefix,
196  Ice::PropertiesPtr properties) :
197  definition(definition),
198  prefix(prefix),
199  properties(properties)
200  {
201  ARMARX_TRACE;
202  }
203 
204 
205  template <typename PropertyType>
206  PropertyType
208  {
209  ARMARX_TRACE;
210  return definition.getValue(prefix, properties);
211  }
212 
213 
214  template <typename PropertyType>
215  bool
217  {
218  ARMARX_TRACE;
219  return definition.isRequired();
220  }
221 
222 
223  template <typename PropertyType>
224  bool
226  {
227  ARMARX_TRACE;
228  return definition.isConstant();
229  }
230 
231 
232  template <typename PropertyType>
233  bool
235  {
236  ARMARX_TRACE;
237  return definition.isSet(prefix, properties);
238  }
239 
240  extern template class Property <int>;
241  extern template class Property <float>;
242  extern template class Property <double>;
243  extern template class Property <std::string>;
244  extern template class Property <bool>;
245 
246 }
247 
248 
armarx::Property::PropertyValuesMap
PropertyDefinition< PropertyType >::PropertyValuesMap PropertyValuesMap
Property value map type definition.
Definition: Property.h:85
armarx::Property::getValue
PropertyType getValue()
Returns the property value set in a config file or passed as a command-line option.
Definition: Property.h:207
armarx::PropertyDefinition::PropertyValuesMap
std::map< std::string, ValueEntry > PropertyValuesMap
Definition: PropertyDefinition.h:113
armarx::Property::getValueAndReplaceAllVars
std::enable_if_t< std::is_same_v< T, std::string >, T > getValueAndReplaceAllVars()
Definition: Property.h:149
IceInternal::Handle< ::Ice::Properties >
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::Property::isSet
bool isSet() const
Checks whether the property is set.
Definition: Property.h:234
armarx::Property
Provides access to Ice properties with extended capabilities.
Definition: Property.h:78
armarx::Property::Property
Property(PropertyDefinition< PropertyType > definition, std::string prefix, Ice::PropertiesPtr properties)
Property constructor.
Definition: Property.h:193
armarx::Property::getValueAndReplaceAllVars
std::enable_if_t< std::is_same_v< T, std::vector< std::string > >, T > getValueAndReplaceAllVars()
Definition: Property.h:157
PropertyDefinition.hpp
armarx::Property::isConstant
bool isConstant() const
Checks if this property is constant or if it can be changed at runtime.
Definition: Property.h:225
armarx::FileSystemPathBuilder_ApplyFormattingAndResolveEnvAndCMakeVars
std::string FileSystemPathBuilder_ApplyFormattingAndResolveEnvAndCMakeVars(std::string const &value)
Definition: Property.cpp:39
armarx::Property::isRequired
bool isRequired() const
Checks if this property is required.
Definition: Property.h:216
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::PropertyDefinition
PropertyDefinition defines a property that will be available within the PropertyUser.
Definition: PropertyDefinition.h:107
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
PropertyDefinition.h