PropertyDefinition.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 
26 #pragma once
27 
28 
35 
36 #include <Ice/Handle.h>
37 
38 #include <type_traits>
39 #include <functional>
40 #include <memory>
41 #include <string>
42 #include <map>
43 
44 namespace Ice
45 {
46  class Properties;
47  class LocalObject;
49 }
50 
52 {
53  template<class T, class = void>
54  struct GetPropertyPlugin : std::false_type {};
55 
56  template<class T, class = void>
57  struct DefinePropertyPlugin : std::false_type {};
58 
59  template<class T, class = void>
60  struct MapPropertyValuePlugin : std::false_type {};
61 
62  template<class T, class = void>
63  struct DefaultAsStringPlugin : std::false_type {};
64 
65  template<class T, class = void>
66  struct PDInitHookPlugin : std::false_type {};
67 }
68 
69 namespace armarx
70 {
71  /**
72  * @ingroup properties
73  *
74  * @class PropertyDefinition
75  * @brief PropertyDefinition defines a property that will be available
76  * within the PropertyUser
77  *
78  * PropertyDefinition provides the definition and description of a
79  * property that may be definined in a config file or passed via command
80  * line as an option. Depending which constructor is used a property
81  * is either required or optional. In the latter case a default value has
82  * to be specified.
83  *
84  * The property specific value type (@em PropertyType) can be arbitrary, yet
85  * it is devided into these three groups which are handled internally
86  * differently:
87  * - String values
88  * - Arithmetic values (integral & floating point types)
89  * - Custom objects (Classes, Structs, Enums)
90  *
91  * @section fluentinterface Fluent Interface
92  *
93  * All setters are self referential and therefor can be chained to
94  * simplify the implementation and increase the readability.
95  *
96  * @subsection fluentinterface_example Example
97  * @code
98  * PropertyDefinition<float> frameRateDef("MyFrameRate", "Capture frame rate");
99  *
100  * frameRateDef
101  * .setMatchRegex("\\d+(.\\d*)?")
102  * .setMin(0.0f)
103  * .setMax(60.0f);
104  * @endcode
105  */
106  template<typename PropertyType>
109  {
110  public:
111  using PropertyTypePtr = std::shared_ptr<PropertyType>;
112  using ValueEntry = std::pair<std::string, PropertyType>;
113  using PropertyValuesMap = std::map<std::string, ValueEntry>;
114  typedef std::function<PropertyType(std::string)> PropertyFactoryFunction;
115 
116  /**
117  * Constructs a property definition of a required property
118  *
119  * @param setterRef Setter
120  * @param propertyName Name of the property used in config files
121  * @param description Mandatory property description
122  */
124  PropertyType* setterRef,
125  const std::string& propertyName,
126  const std::string& description,
128 
129 
130  /**
131  * Constructs a property definition of an optional property
132  *
133  * @param setterRef Setter
134  * @param propertyName Name of the property used in config files
135  * @param defaultValue Property default value of the type
136  * @em PropertyType
137  * @param description Mandatory property description
138  */
140  PropertyType* setterRef,
141  const std::string& propertyName,
142  PropertyType defaultValue,
143  const std::string& description,
145 
146  /**
147  * Maps a string value onto a value of the specified template type
148  *
149  * @param valueString Property string value key
150  * @param value Property value
151  *
152  * @return self reference
153  */
154  PropertyDefinition<PropertyType>& map(const std::string& valueString, PropertyType value);
155 
156 
157  PropertyDefinition<PropertyType>& map(const std::map<std::string, PropertyType>& values);
158 
159  template<class T>
160  std::enable_if_t < std::is_same_v<T, PropertyType>&& !std::is_same_v<T, std::string>, PropertyDefinition<T>& >
161  map(const std::map<T, std::string>& values);
162 
163  template<class T>
165  map(const T& values)
166  {
168  }
169 
170  /**
171  * Sets the factory function that creates the specified template type from the actual string value
172  *
173  * @param func the factory function
174  *
175  * @return self reference
176  */
178 
179  /**
180  * Sets whether the property value matching is case insensitive.
181  *
182  * @param isCaseInsensitive Case sensitivity state
183  *
184  * @return self reference
185  */
187 
188  /**
189  * Sets whether for string values environment varbiale expanding should be considered.
190  *
191  * @param expand Expand entries like '${ENV_VAR}' to the according values of the
192  * environment.
193  *
194  * @return self reference
195  */
197 
198  /**
199  * Sets whether for string values leading and trailing quotes should be removed.
200  *
201  * @param removeQuotes The indicator.
202  *
203  * @return self reference
204  */
206 
207  /**
208  * Sets the regular expression which the value has to be matched with.
209  *
210  * @param expr Value regular expression
211  *
212  * @return self reference
213  */
214  PropertyDefinition<PropertyType>& setMatchRegex(const std::string& expr);
215 
216  /**
217  * Sets the min allowed numeric value
218  *
219  * @param min Min. allowed value
220  *
221  * @return self reference
222  */
224  setMin(double min);
225 
226  template<class T = PropertyType>
228  {
229  return setMin(min.at(0));
230  }
231 
232  /**
233  * Sets the max allowed numeric value
234  *
235  * @param max Max. allowed value
236  *
237  * @return self reference
238  */
240  setMax(double max);
241 
242  template<class T = PropertyType>
244  {
245  return setMax(max.at(0));
246  }
247 
248  bool isCaseInsensitive() const;
249  bool expandEnvironmentVariables() const;
250  bool removeQuotes() const;
251  std::string getMatchRegex() const;
252  double getMin() const;
253  double getMax() const;
254  std::string getPropertyName() const;
256  PropertyType getDefaultValue();
258  std::string getDefaultAsString() override;
259 
260  PropertyType getValue(const std::string& prefix, Ice::PropertiesPtr);
261  bool isSet(const std::string& prefix, Ice::PropertiesPtr iceProperties) const;
262 
263  virtual
264  void
266  const std::string& prefix,
267  Ice::PropertiesPtr) override;
268 
269  //! Checks first and last character of input and in case both are quotes, they are removed and the remaining result is stored in output
270  static void removeQuotes(const std::string& input, std::string& output);
271 
272  /**
273  * @see PropertyDefinitionBase::toString()
274  */
275  std::string toString(PropertyDefinitionFormatter& formatter, const std::string& value) override;
276 
277  std::string getDescription() const;
278 
279  protected:
280 
281  /**
282  * Main property map
283  */
285 
286  /**
287  * Property name
288  */
289  std::string propertyName;
290 
291  /**
292  * Property description
293  */
294  std::string description;
295 
296  /**
297  * Fallback/Default property value
298  */
299  PropertyType defaultValue;
300 
301  /**
302  * Reference to a variable to set
303  */
304  PropertyType* setterRef;
305 
306  /**
307  * Builder function
308  */
310 
311  /**
312  * Regular expression to approve a required value pattern
313  */
314  std::string regex;
315 
316  /**
317  * Case sensitivity indicator
318  */
320 
321  /**
322  * Exand environments variables indicator (standard: true)
323  */
325 
326  /**
327  * Remove leading and trailing quotes indicator (standard: true)
328  * First and last character of a string property value are checked for quotes.
329  * E.g.
330  * "test" -> test
331  * 'test' -> test
332  */
334 
335  /**
336  * Upper bound of numeric values (used for numeric value retrieval
337  * without mapping)
338  */
339  double max;
340 
341  /**
342  * Lower bound of numeric values (used for numeric value retrieval
343  * without mapping)
344  */
345  double min;
346 
347  private:
348 
349  void initHook();
350 
351  /**
352  * @brief Get a required value.
353  * @throw armarx::exceptions::local::ValueRangeExceededException
354  * @throw armarx::exceptions::local::InvalidPropertyValueException
355  */
356  PropertyType getValueRequired(const std::string& prefix, Ice::PropertiesPtr iceProperties);
357  /**
358  * @brief Get an optional value.
359  * @throw armarx::exceptions::local::ValueRangeExceededException
360  * @throw armarx::exceptions::local::InvalidPropertyValueException
361  */
362  PropertyType getValueOptional(const std::string& prefix, Ice::PropertiesPtr iceProperties);
363 
364  /**
365  * Returns the property string value. This function adapts the value
366  * case according to the case sensitivity state. If no case sensitivity
367  * is required, the return value will always be transformed to lower
368  * case. This is mainly used to generate the value keys used in mapping.
369  *
370  * @return lower or mixed case eof th raw string value
371  */
372  std::string getPropertyValue(const std::string& prefix, Ice::PropertiesPtr iceProperties) const;
373 
374  /**
375  * Returns the raw string value of this property.
376  *
377  * @return raw string value.
378  */
379  std::string getRawPropertyValue(const std::string& prefix, Ice::PropertiesPtr iceProperties) const;
380 
381  /**
382  * Checks if this property is required and throws an exception if the
383  * property is not specified.
384  *
385  * @throw armarx::exceptions::local::MissingRequiredPropertyException
386  */
387  void checkRequirement(const std::string& prefix, Ice::PropertiesPtr iceProperties);
388 
389  /**
390  * Checks whether the a passed string matches a specified regular
391  * expression.
392  *
393  * @param value The value to check
394  *
395  * @return true if the string matches, otherwise false
396  */
397  bool matchRegex(const std::string& value) const;
398 
399  /// Return `value`.
400  template <typename T>
401  T processMappedValue(const T& value);
402 
403  /// @brief Expand environment variables and removes quotes, if set respectively.
404  std::string processMappedValue(const std::string& value);
405 
406  /**
407  * Verifies that the passed numericValue is within the defined
408  * limits.
409  *
410  * @param numericValue The numeric value of the property
411  *
412  * @throw armarx::exceptions::local::ValueRangeExceededException
413  */
414  void checkLimits(const std::string& prefix, PropertyType numericValue) const;
415 
416  static bool expandEnvironmentVariables(const std::string& input, std::string& output);
417 
418  };
419 
420  // Helper functions to prevent boost include files
421  std::string PropertyDefinition_lexicalCastToString(double input);
425  std::string PropertyDefinition_lexicalCastToString(unsigned long input);
426  std::string PropertyDefinition_lexicalCastToString(unsigned int input);
427  std::string PropertyDefinition_lexicalCastToString(std::string const& input);
428 
429  template <typename T>
430  T PropertyDefinition_lexicalCastTo(std::string const& input);
431 
432  template <>
433  double PropertyDefinition_lexicalCastTo<double>(std::string const& input);
434  template <>
435  float PropertyDefinition_lexicalCastTo<float>(std::string const& input);
436  template <>
437  long PropertyDefinition_lexicalCastTo<long>(std::string const& input);
438  template <>
439  int PropertyDefinition_lexicalCastTo<int>(std::string const& input);
440  template <>
441  unsigned long PropertyDefinition_lexicalCastTo<unsigned long>(std::string const& input);
442  template <>
443  unsigned int PropertyDefinition_lexicalCastTo<unsigned int>(std::string const& input);
444  template <>
445  char PropertyDefinition_lexicalCastTo<char>(std::string const& input);
446  template <>
447  unsigned char PropertyDefinition_lexicalCastTo<unsigned char>(std::string const& input);
448  template <>
449  bool PropertyDefinition_lexicalCastTo<bool>(std::string const& input);
450 
451 
452  extern template class PropertyDefinition<int>;
453  extern template class PropertyDefinition<float>;
454  extern template class PropertyDefinition<double>;
455  extern template class PropertyDefinition<std::string>;
456  extern template class PropertyDefinition<bool>;
457 }
armarx::PropertyDefinition::getValue
PropertyType getValue(const std::string &prefix, Ice::PropertiesPtr)
Definition: PropertyDefinition.hpp:466
armarx::PropertyDefinition::expandEnvVars
bool expandEnvVars
Exand environments variables indicator (standard: true)
Definition: PropertyDefinition.h:324
armarx::PropertyDefinition_lexicalCastTo< float >
float PropertyDefinition_lexicalCastTo< float >(std::string const &input)
Definition: PropertyDefinition.cpp:219
armarx::PropertyDefinition::getMatchRegex
std::string getMatchRegex() const
Definition: PropertyDefinition.hpp:273
armarx::PropertyDefinition_lexicalCastTo< unsigned long >
unsigned long PropertyDefinition_lexicalCastTo< unsigned long >(std::string const &input)
Definition: PropertyDefinition.cpp:238
armarx::PropertyDefinitionBase
Common interface of any property definition.
Definition: PropertyDefinitionInterface.h:51
armarx::PropertyDefinition::getValueMap
PropertyValuesMap & getValueMap()
Definition: PropertyDefinition.hpp:313
armarx::PropertyDefinition_lexicalCastTo
T PropertyDefinition_lexicalCastTo(std::string const &input)
armarx::meta::properties::MapPropertyValuePlugin
Definition: PropertyDefinition.h:60
armarx::PropertyDefinitionBase::PropertyConstness
PropertyConstness
Definition: PropertyDefinitionInterface.h:54
armarx::meta::properties::DefinePropertyPlugin
Definition: PropertyDefinition.h:57
armarx::meta::properties
Definition: PluginCfgStruct.h:42
armarx::PropertyDefinition::getMax
double getMax() const
Definition: PropertyDefinition.hpp:289
armarx::PropertyDefinition::setterRef
PropertyType * setterRef
Reference to a variable to set.
Definition: PropertyDefinition.h:304
armarx::PropertyDefinition::stringRemoveQuotes
bool stringRemoveQuotes
Remove leading and trailing quotes indicator (standard: true) First and last character of a string pr...
Definition: PropertyDefinition.h:333
armarx::PropertyDefinition::setMax
PropertyDefinition< PropertyType > & setMax(std::array< T, 1 > max)
Definition: PropertyDefinition.h:243
armarx::VariantType::Map
const VariantContainerType Map
Definition: StringValueMap.h:247
armarx::PropertyDefinition::getPropertyName
std::string getPropertyName() const
Definition: PropertyDefinition.hpp:297
armarx::PropertyDefinition::PropertyValuesMap
std::map< std::string, ValueEntry > PropertyValuesMap
Definition: PropertyDefinition.h:113
InvalidPropertyValueException.h
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::PropertyDefinition::PropertyDefinition
PropertyDefinition(PropertyType *setterRef, const std::string &propertyName, const std::string &description, PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
Constructs a property definition of a required property.
Definition: PropertyDefinition.hpp:46
PropertyDefinitionFormatter.h
armarx::PropertyDefinition::setExpandEnvironmentVariables
PropertyDefinition< PropertyType > & setExpandEnvironmentVariables(bool expand)
Sets whether for string values environment varbiale expanding should be considered.
Definition: PropertyDefinition.hpp:186
armarx::PropertyDefinition_lexicalCastTo< long >
long PropertyDefinition_lexicalCastTo< long >(std::string const &input)
Definition: PropertyDefinition.cpp:226
armarx::PropertyDefinition::map
std::enable_if_t< meta::properties::MapPropertyValuePlugin< T >::value, PropertyDefinition< PropertyType > & > map(const T &values)
Definition: PropertyDefinition.h:165
armarx::PropertyDefinition::setMin
PropertyDefinition< PropertyType > & setMin(double min)
Sets the min allowed numeric value.
Definition: PropertyDefinition.hpp:215
armarx::PropertyDefinition::isCaseInsensitive
bool isCaseInsensitive() const
Definition: PropertyDefinition.hpp:250
armarx::PropertyDefinition::min
double min
Lower bound of numeric values (used for numeric value retrieval without mapping)
Definition: PropertyDefinition.h:345
IceInternal::Handle< ::Ice::Properties >
armarx::PropertyDefinition::caseInsensitive
bool caseInsensitive
Case sensitivity indicator.
Definition: PropertyDefinition.h:319
armarx::meta::properties::PDInitHookPlugin
Definition: PropertyDefinition.h:66
PropertyDefinitionInterface.h
ValueRangeExceededException.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::PropertyDefinition::ValueEntry
std::pair< std::string, PropertyType > ValueEntry
Definition: PropertyDefinition.h:112
armarx::PropertyDefinition_lexicalCastTo< int >
int PropertyDefinition_lexicalCastTo< int >(std::string const &input)
Definition: PropertyDefinition.cpp:232
armarx::PropertyDefinition::getFactory
PropertyFactoryFunction getFactory() const
Definition: PropertyDefinition.hpp:335
armarx::PropertyDefinition::setRemoveQuotes
PropertyDefinition< PropertyType > & setRemoveQuotes(bool removeQuotes)
Sets whether for string values leading and trailing quotes should be removed.
Definition: PropertyDefinition.hpp:195
armarx::PropertyDefinition::max
double max
Upper bound of numeric values (used for numeric value retrieval without mapping)
Definition: PropertyDefinition.h:339
armarx::PropertyDefinition::factory
PropertyFactoryFunction factory
Builder function.
Definition: PropertyDefinition.h:309
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::PropertyDefinition_lexicalCastToString
std::string PropertyDefinition_lexicalCastToString(float input)
Definition: PropertyDefinition.cpp:174
armarx::PropertyDefinition_lexicalCastTo< bool >
bool PropertyDefinition_lexicalCastTo< bool >(std::string const &input)
Definition: PropertyDefinition.cpp:262
armarx::PropertyDefinition::toString
std::string toString(PropertyDefinitionFormatter &formatter, const std::string &value) override
Definition: PropertyDefinition.hpp:342
armarx::PropertyDefinition_lexicalCastTo< unsigned char >
unsigned char PropertyDefinition_lexicalCastTo< unsigned char >(std::string const &input)
Definition: PropertyDefinition.cpp:256
armarx::PropertyDefinition::expandEnvironmentVariables
bool expandEnvironmentVariables() const
Definition: PropertyDefinition.hpp:258
armarx::PropertyDefinition::getDefaultValue
PropertyType getDefaultValue()
Definition: PropertyDefinition.hpp:321
MissingRequiredPropertyException.h
armarx::PropertyDefinition::getDescription
std::string getDescription() const
Definition: PropertyDefinition.hpp:305
armarx::PropertyDefinition::defaultValue
PropertyType defaultValue
Fallback/Default property value.
Definition: PropertyDefinition.h:299
Ice
Definition: DBTypes.cpp:64
armarx::PropertyDefinition::isSet
bool isSet(const std::string &prefix, Ice::PropertiesPtr iceProperties) const
Definition: PropertyDefinition.hpp:703
armarx::PropertyDefinition::setMin
PropertyDefinition< PropertyType > & setMin(std::array< T, 1 > min)
Definition: PropertyDefinition.h:227
armarx::PropertyDefinition::setMatchRegex
PropertyDefinition< PropertyType > & setMatchRegex(const std::string &expr)
Sets the regular expression which the value has to be matched with.
Definition: PropertyDefinition.hpp:204
UnmappedValueException.h
armarx::PropertyDefinition::removeQuotes
bool removeQuotes() const
Definition: PropertyDefinition.hpp:266
armarx::PropertyDefinition::PropertyTypePtr
std::shared_ptr< PropertyType > PropertyTypePtr
Definition: PropertyDefinition.h:111
armarx::PropertyDefinition::propertyName
std::string propertyName
Property name.
Definition: PropertyDefinition.h:289
armarx::PropertyDefinition::getDefaultAsString
std::string getDefaultAsString() override
Definition: PropertyDefinition.hpp:370
armarx::meta::properties::DefaultAsStringPlugin
Definition: PropertyDefinition.h:63
armarx::PropertyDefinitionBase::constness
PropertyConstness constness
Definition: PropertyDefinitionInterface.h:104
armarx::PropertyDefinition::setCaseInsensitive
PropertyDefinition< PropertyType > & setCaseInsensitive(bool isCaseInsensitive)
Sets whether the property value matching is case insensitive.
Definition: PropertyDefinition.hpp:150
armarx::PropertyDefinition::regex
std::string regex
Regular expression to approve a required value pattern.
Definition: PropertyDefinition.h:314
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::PropertyDefinition::setFactory
PropertyDefinition< PropertyType > & setFactory(const PropertyFactoryFunction &func)
Sets the factory function that creates the specified template type from the actual string value.
Definition: PropertyDefinition.hpp:140
armarx::PropertyDefinition_lexicalCastTo< unsigned int >
unsigned int PropertyDefinition_lexicalCastTo< unsigned int >(std::string const &input)
Definition: PropertyDefinition.cpp:244
armarx::PropertyDefinition_lexicalCastTo< char >
char PropertyDefinition_lexicalCastTo< char >(std::string const &input)
Definition: PropertyDefinition.cpp:250
armarx::PropertyDefinition::setMax
PropertyDefinition< PropertyType > & setMax(double max)
Sets the max allowed numeric value.
Definition: PropertyDefinition.hpp:233
armarx::PropertyDefinitionFormatter
PropertyDefinitionFormatter is the base class for all formatters of PropertyDefinitions.
Definition: PropertyDefinitionFormatter.h:38
armarx::PropertyDefinition::description
std::string description
Property description.
Definition: PropertyDefinition.h:294
armarx::PropertyDefinition::getMin
double getMin() const
Definition: PropertyDefinition.hpp:281
armarx::PropertyDefinition::propertyValuesMap
PropertyValuesMap propertyValuesMap
Main property map.
Definition: PropertyDefinition.h:284
armarx::PropertyDefinition
PropertyDefinition defines a property that will be available within the PropertyUser.
Definition: PropertyDefinition.h:107
armarx::PropertyDefinition::PropertyFactoryFunction
std::function< PropertyType(std::string)> PropertyFactoryFunction
Definition: PropertyDefinition.h:114
armarx::meta::properties::GetPropertyPlugin
Definition: PropertyDefinition.h:54
armarx::PropertyDefinition::writeValueToSetter
virtual void writeValueToSetter(const std::string &prefix, Ice::PropertiesPtr) override
Definition: PropertyDefinition.hpp:474
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::PropertyDefinition::map
PropertyDefinition< PropertyType > & map(const std::string &valueString, PropertyType value)
Maps a string value onto a value of the specified template type.
Definition: PropertyDefinition.hpp:95
armarx::PropertyDefinition_lexicalCastTo< double >
double PropertyDefinition_lexicalCastTo< double >(std::string const &input)
Definition: PropertyDefinition.cpp:212
armarx::PropertyDefinitionBase::eConstant
@ eConstant
Definition: PropertyDefinitionInterface.h:56