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 <algorithm> // for min, fill
28#include <iostream> // for basic_ostream, operator<<, etc
29#include <limits>
30#include <map>
31#include <string> // for basic_string, string, etc
32#include <typeinfo> // for bad_cast
33#include <vector> // for vector
34
35#include <Ice/LocalObject.h>
36
38
40
41namespace armarx
42{
43 // Helper functions to reduce includes
44 std::string
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>
79 {
80 public:
81 /**
82 * Property value map type definition
83 */
85
86 /**
87 * Property constructor
88 *
89 * @param definition Property specific definition
90 * @param prefix Property prefix such as domain or component name
91 * @param properties Ice properties set which contains all property
92 * strings.
93 *
94 */
96 std::string prefix,
97 Ice::PropertiesPtr properties);
98
99 /**
100 * Checks whether the property is set
101 *
102 * @return true if property is set, otheriwse false
103 */
104 bool isSet() const;
105
106 /**
107 * Checks if this property is required
108 *
109 * @return true if set required, otherwise false
110 */
111 bool isRequired() const;
112
113 /**
114 * Checks if this property is constant or if it can be changed at runtime.
115 *
116 * @return true if property is constant, otherwise false
117 */
118 bool isConstant() const;
119
120 /**
121 * Returns the property value set in a config file or passed as a
122 * command-line option. If property is not set, the default value is
123 * returned unless the property is required. In the latter case an
124 * exception is thrown indicating that the required property is not set.
125 *
126 * @return Property value of the type <b>PropertyType</b>
127 *
128 * @throw armarx::exceptions::local::ValueRangeExceededException
129 * @throw armarx::exceptions::local::InvalidPropertyValueException
130 */
131 PropertyType getValue();
132
133 /**
134 * @brief Convenience overload of getValue().
135 * Usage:
136 * \code
137 * std::string val = getProperty<std::string>("myProp");
138 * \endcode
139 */
140 operator PropertyType()
141 {
143 return getValue();
144 }
145
146 template <class T = PropertyType>
147 std::enable_if_t<std::is_same_v<T, std::string>, T>
153
154 template <class T = PropertyType>
155 std::enable_if_t<std::is_same_v<T, std::vector<std::string>>, T>
157 {
158 auto vals = getValue();
159 for (auto& val : vals)
160 {
162 }
163 return vals;
164 }
165
166 private:
167 /* = Implementation details ========================================= */
168
169 template <int>
170 struct Qualifier
171 {
172 Qualifier(int)
173 {
174 }
175 };
176
177 private:
178 /// Property definition.
179 PropertyDefinition<PropertyType> definition;
180
181 /// Property prefix such as domain name.
182 std::string prefix;
183
184 /// Ice property container.
185 Ice::PropertiesPtr properties;
186 };
187
188 /* ====================================================================== */
189 /* === Property Implementation ========================================== */
190 /* ====================================================================== */
191
192 template <typename PropertyType>
194 std::string prefix,
195 Ice::PropertiesPtr properties) :
196 definition(definition), prefix(prefix), properties(properties)
197 {
199 }
200
201 template <typename PropertyType>
202 PropertyType
204 {
206 return definition.getValue(prefix, properties);
207 }
208
209 template <typename PropertyType>
210 bool
212 {
214 return definition.isRequired();
215 }
216
217 template <typename PropertyType>
218 bool
220 {
222 return definition.isConstant();
223 }
224
225 template <typename PropertyType>
226 bool
228 {
230 return definition.isSet(prefix, properties);
231 }
232
233 extern template class Property<int>;
234 extern template class Property<float>;
235 extern template class Property<double>;
236 extern template class Property<std::string>;
237 extern template class Property<bool>;
238
239} // namespace armarx
PropertyDefinition defines a property that will be available within the PropertyUser.
std::map< std::string, ValueEntry > PropertyValuesMap
Provides access to Ice properties with extended capabilities.
Definition Property.h:79
PropertyType getValue()
Returns the property value set in a config file or passed as a command-line option.
Definition Property.h:203
std::enable_if_t< std::is_same_v< T, std::string >, T > getValueAndReplaceAllVars()
Definition Property.h:148
bool isSet() const
Checks whether the property is set.
Definition Property.h:227
bool isConstant() const
Checks if this property is constant or if it can be changed at runtime.
Definition Property.h:219
std::enable_if_t< std::is_same_v< T, std::vector< std::string > >, T > getValueAndReplaceAllVars()
Definition Property.h:156
Property(PropertyDefinition< PropertyType > definition, std::string prefix, Ice::PropertiesPtr properties)
Property constructor.
Definition Property.h:193
bool isRequired() const
Checks if this property is required.
Definition Property.h:211
PropertyDefinition< PropertyType >::PropertyValuesMap PropertyValuesMap
Property value map type definition.
Definition Property.h:84
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::string FileSystemPathBuilder_ApplyFormattingAndResolveEnvAndCMakeVars(std::string const &value)
Definition Property.cpp:39
#define ARMARX_TRACE
Definition trace.h:77