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;
48  ICE_API ::Ice::LocalObject* upCast(::Ice::Properties*);
49  typedef ::IceInternal::Handle< ::Ice::Properties> PropertiesPtr;
50 }
51 
53 {
54  template<class T, class = void>
55  struct GetPropertyPlugin : std::false_type {};
56 
57  template<class T, class = void>
58  struct DefinePropertyPlugin : std::false_type {};
59 
60  template<class T, class = void>
61  struct MapPropertyValuePlugin : std::false_type {};
62 
63  template<class T, class = void>
64  struct DefaultAsStringPlugin : std::false_type {};
65 
66  template<class T, class = void>
67  struct PDInitHookPlugin : std::false_type {};
68 }
69 
70 namespace armarx
71 {
107  template<typename PropertyType>
110  {
111  public:
112  using PropertyTypePtr = std::shared_ptr<PropertyType>;
113  using ValueEntry = std::pair<std::string, PropertyType>;
114  using PropertyValuesMap = std::map<std::string, ValueEntry>;
115  typedef std::function<PropertyType(std::string)> PropertyFactoryFunction;
116 
125  PropertyType* setterRef,
126  const std::string& propertyName,
127  const std::string& description,
129 
130 
141  PropertyType* setterRef,
142  const std::string& propertyName,
143  PropertyType defaultValue,
144  const std::string& description,
146 
155  PropertyDefinition<PropertyType>& map(const std::string& valueString, PropertyType value);
156 
157 
158  PropertyDefinition<PropertyType>& map(const std::map<std::string, PropertyType>& values);
159 
160  template<class T>
161  std::enable_if_t < std::is_same_v<T, PropertyType>&& !std::is_same_v<T, std::string>, PropertyDefinition<T>& >
162  map(const std::map<T, std::string>& values);
163 
164  template<class T>
166  map(const T& values)
167  {
169  }
170 
179 
188 
198 
207 
215  PropertyDefinition<PropertyType>& setMatchRegex(const std::string& expr);
216 
225  setMin(double min);
226 
227  template<class T = PropertyType>
229  {
230  return setMin(min.at(0));
231  }
232 
241  setMax(double max);
242 
243  template<class T = PropertyType>
245  {
246  return setMax(max.at(0));
247  }
248 
249  bool isCaseInsensitive() const;
250  bool expandEnvironmentVariables() const;
251  bool removeQuotes() const;
252  std::string getMatchRegex() const;
253  double getMin() const;
254  double getMax() const;
255  std::string getPropertyName() const;
257  PropertyType getDefaultValue();
259  std::string getDefaultAsString() override;
260 
261  PropertyType getValue(const std::string& prefix, Ice::PropertiesPtr);
262  bool isSet(const std::string& prefix, Ice::PropertiesPtr iceProperties) const;
263 
264  virtual
265  void
267  const std::string& prefix,
268  Ice::PropertiesPtr) override;
269 
271  static void removeQuotes(const std::string& input, std::string& output);
272 
276  std::string toString(PropertyDefinitionFormatter& formatter, const std::string& value) override;
277 
278  std::string getDescription() const;
279 
280  protected:
281 
286 
290  std::string propertyName;
291 
295  std::string description;
296 
300  PropertyType defaultValue;
301 
305  PropertyType* setterRef;
306 
311 
315  std::string regex;
316 
321 
326 
335 
340  double max;
341 
346  double min;
347 
348  private:
349 
350  void initHook();
351 
357  PropertyType getValueRequired(const std::string& prefix, Ice::PropertiesPtr iceProperties);
363  PropertyType getValueOptional(const std::string& prefix, Ice::PropertiesPtr iceProperties);
364 
373  std::string getPropertyValue(const std::string& prefix, Ice::PropertiesPtr iceProperties) const;
374 
380  std::string getRawPropertyValue(const std::string& prefix, Ice::PropertiesPtr iceProperties) const;
381 
388  void checkRequirement(const std::string& prefix, Ice::PropertiesPtr iceProperties);
389 
398  bool matchRegex(const std::string& value) const;
399 
401  template <typename T>
402  T processMappedValue(const T& value);
403 
405  std::string processMappedValue(const std::string& value);
406 
415  void checkLimits(const std::string& prefix, PropertyType numericValue) const;
416 
417  static bool expandEnvironmentVariables(const std::string& input, std::string& output);
418 
419  };
420 
421  // Helper functions to prevent boost include files
422  std::string PropertyDefinition_lexicalCastToString(double input);
426  std::string PropertyDefinition_lexicalCastToString(unsigned long input);
427  std::string PropertyDefinition_lexicalCastToString(unsigned int input);
428  std::string PropertyDefinition_lexicalCastToString(std::string const& input);
429 
430  template <typename T>
431  T PropertyDefinition_lexicalCastTo(std::string const& input);
432 
433  template <>
434  double PropertyDefinition_lexicalCastTo<double>(std::string const& input);
435  template <>
436  float PropertyDefinition_lexicalCastTo<float>(std::string const& input);
437  template <>
438  long PropertyDefinition_lexicalCastTo<long>(std::string const& input);
439  template <>
440  int PropertyDefinition_lexicalCastTo<int>(std::string const& input);
441  template <>
442  unsigned long PropertyDefinition_lexicalCastTo<unsigned long>(std::string const& input);
443  template <>
444  unsigned int PropertyDefinition_lexicalCastTo<unsigned int>(std::string const& input);
445  template <>
446  char PropertyDefinition_lexicalCastTo<char>(std::string const& input);
447  template <>
448  unsigned char PropertyDefinition_lexicalCastTo<unsigned char>(std::string const& input);
449  template <>
450  bool PropertyDefinition_lexicalCastTo<bool>(std::string const& input);
451 
452 
453  extern template class PropertyDefinition<int>;
454  extern template class PropertyDefinition<float>;
455  extern template class PropertyDefinition<double>;
456  extern template class PropertyDefinition<std::string>;
457  extern template class PropertyDefinition<bool>;
458 }
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:325
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:52
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:61
armarx::PropertyDefinitionBase::PropertyConstness
PropertyConstness
Definition: PropertyDefinitionInterface.h:55
armarx::meta::properties::DefinePropertyPlugin
Definition: PropertyDefinition.h:58
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:305
armarx::PropertyDefinition::stringRemoveQuotes
bool stringRemoveQuotes
Remove leading and trailing quotes indicator (standard: true) First and last character of a string pr...
Definition: PropertyDefinition.h:334
armarx::PropertyDefinition::setMax
PropertyDefinition< PropertyType > & setMax(std::array< T, 1 > max)
Definition: PropertyDefinition.h:244
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:114
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:166
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:346
IceInternal::Handle< ::Ice::Properties >
armarx::PropertyDefinition::caseInsensitive
bool caseInsensitive
Case sensitivity indicator.
Definition: PropertyDefinition.h:320
armarx::meta::properties::PDInitHookPlugin
Definition: PropertyDefinition.h:67
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:113
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:340
armarx::PropertyDefinition::factory
PropertyFactoryFunction factory
Builder function.
Definition: PropertyDefinition.h:310
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:16
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:300
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:228
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:112
armarx::PropertyDefinition::propertyName
std::string propertyName
Property name.
Definition: PropertyDefinition.h:290
Ice::upCast
ICE_API ::Ice::LocalObject * upCast(::Ice::Properties *)
armarx::PropertyDefinition::getDefaultAsString
std::string getDefaultAsString() override
Definition: PropertyDefinition.hpp:370
armarx::meta::properties::DefaultAsStringPlugin
Definition: PropertyDefinition.h:64
armarx::PropertyDefinitionBase::constness
PropertyConstness constness
Definition: PropertyDefinitionInterface.h:105
Ice::PropertiesPtr
::IceInternal::Handle< ::Ice::Properties > PropertiesPtr
Definition: PropertyDefinition.h:49
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:315
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:295
armarx::PropertyDefinition::getMin
double getMin() const
Definition: PropertyDefinition.hpp:281
armarx::PropertyDefinition::propertyValuesMap
PropertyValuesMap propertyValuesMap
Main property map.
Definition: PropertyDefinition.h:285
armarx::PropertyDefinition
PropertyDefinition defines a property that will be available within the PropertyUser.
Definition: PropertyDefinition.h:108
armarx::PropertyDefinition::PropertyFactoryFunction
std::function< PropertyType(std::string)> PropertyFactoryFunction
Definition: PropertyDefinition.h:115
armarx::meta::properties::GetPropertyPlugin
Definition: PropertyDefinition.h:55
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:57