PropertyDefinition.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU Lesser General Public License as
6* published by the Free Software Foundation; either version 2 of
7* the License, or (at your option) any later version.
8*
9* ArmarX is distributed in the hope that it will be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU Lesser General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with this program. If not, see <http://www.gnu.org/licenses/>.
16*
17* @package ArmarX::
18* @author Mirko Waechter ( mirko.waechter at kit dot edu)
19* @date 2015
20* @copyright http://www.gnu.org/licenses/gpl.txt
21* GNU General Public License
22*/
23
24
26
27#include <regex>
28
29#include <boost/lexical_cast.hpp>
30#include <boost/regex.hpp>
31
32#include <Ice/Properties.h>
33
34#include <SimoxUtility/algorithm/string/string_tools.h>
35
36namespace armarx
37{
38 template class PropertyDefinition<int>;
39 template class PropertyDefinition<float>;
40 template class PropertyDefinition<double>;
42 template class PropertyDefinition<bool>;
43
44 std::string
45 PropertyDefinition_toLowerCopy(const std::string& input)
46 {
47 return simox::alg::to_lower(input);
48 }
49
50 bool
51 PropertyDefinition_matchRegex(std::string const& regex, std::string const& value)
52 {
53 boost::regex expr(regex);
54 return boost::regex_match(value, expr);
55 }
56
57 std::map<std::string, std::string>
59 {
60 std::map<std::string, std::string> result;
61 auto splits = armarx::Split(input, ",");
62 for (auto& elem : splits)
63 {
64 simox::alg::trim(elem);
65 auto split = armarx::Split(elem, ":");
66 if (split.size() == 2)
67 {
68 result[simox::alg::trim_copy(split[0])] = simox::alg::trim_copy(split[1]);
69 }
70 }
71 return result;
72 }
73
74 std::vector<std::string>
75 PropertyDefinition_parseVectorString(std::string const& input)
76 {
77 std::vector<std::string> args;
78
79 const boost::regex re(",(?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)");
80
81 boost::match_results<std::string::const_iterator> what;
82 boost::match_flag_type flags = boost::match_default;
83 std::string::const_iterator s = input.begin();
84 std::string::const_iterator e = input.end();
85 size_t begin = 0;
86 while (boost::regex_search(s, e, what, re, flags))
87 {
88 int pos = what.position();
89 auto arg = input.substr(begin, pos);
90 simox::alg::trim(arg);
91 if (!arg.empty())
92 {
93 args.push_back(arg);
94 }
95 std::string::difference_type l = what.length();
96 std::string::difference_type p = what.position();
97 begin += l + p;
98 s += p + l;
99 }
100 std::string lastArg = input.substr(begin);
101 simox::alg::trim(lastArg);
102 if (!lastArg.empty())
103 {
104 args.push_back(lastArg);
105 }
106 return args;
107 }
108
109 IceUtil::Time
110 PropertyDefinition_parseIceUtilTime(std::string const& input)
111 {
112 std::regex re_number{"^[0-9]*\\.?[0-9]+"};
113 std::regex re_unit{"[a-zµ]+$"};
114 std::smatch number_match;
115 std::smatch unit_match;
116
117 if (not std::regex_search(input.begin(), input.end(), number_match, re_number))
118 {
120 throw InvalidPropertyValueException("unknown", input)
121 << "Could not parse Time (number invalid).";
122 }
123
124 if (not std::regex_search(input.begin(), input.end(), unit_match, re_unit))
125 {
127 throw InvalidPropertyValueException("unknown", input)
128 << "Could not parse Time (unit invalid).";
129 }
130
131 const double value = boost::lexical_cast<double>(number_match[0]);
132
133 static constexpr long h2us = 1000l * 1000 * 60 * 60;
134 static constexpr long m2us = 1000 * 1000 * 60;
135 static constexpr long s2us = 1000 * 1000;
136 static constexpr long ms2us = 1000;
137 static constexpr long us = 1;
138
139 static std::map<std::string, long> conv_map;
140 // Hours.
141 conv_map["h"] = h2us;
142 conv_map["hrs"] = h2us;
143 conv_map["hour"] = h2us;
144 conv_map["hours"] = h2us;
145 // Minutes.
146 conv_map["m"] = m2us;
147 conv_map["min"] = m2us;
148 conv_map["minute"] = m2us;
149 conv_map["minutes"] = m2us;
150 // Seconds.
151 conv_map["s"] = s2us;
152 conv_map["sec"] = s2us;
153 conv_map["second"] = s2us;
154 conv_map["seconds"] = s2us;
155 // Milliseconds.
156 conv_map["ms"] = ms2us;
157 conv_map["msec"] = ms2us;
158 conv_map["millisecond"] = ms2us;
159 conv_map["milliseconds"] = ms2us;
160 // Microseconds.
161 conv_map["µs"] = us;
162 conv_map["us"] = us;
163 conv_map["µsec"] = us;
164 conv_map["usec"] = us;
165 conv_map["microsecond"] = us;
166 conv_map["microseconds"] = us;
167
168 if (conv_map.find(unit_match[0]) == conv_map.end())
169 {
171 throw InvalidPropertyValueException("unknown", input)
172 << "Invalid/unknown unit `" << unit_match[0] << "`.";
173 }
174
175 const long int to_microseconds_mult = conv_map[unit_match[0]];
176 return IceUtil::Time::microSeconds(value * to_microseconds_mult);
177 }
178
179 std::string
181 {
182 std::locale::global(std::locale::classic());
183 return boost::lexical_cast<std::string>(input);
184 }
185
186 std::string
188 {
189 std::locale::global(std::locale::classic());
190 return boost::lexical_cast<std::string>(input);
191 }
192
193 std::string
195 {
196 return boost::lexical_cast<std::string>(input);
197 }
198
199 std::string
201 {
202 return boost::lexical_cast<std::string>(input);
203 }
204
205 std::string
207 {
208 return boost::lexical_cast<std::string>(input);
209 }
210
211 std::string
213 {
214 return boost::lexical_cast<std::string>(input);
215 }
216
217 std::string
219 {
220 return input;
221 }
222
223 template <>
224 double
225 PropertyDefinition_lexicalCastTo<double>(std::string const& input)
226 {
227 std::locale::global(std::locale::classic());
228 return boost::lexical_cast<double>(input);
229 }
230
231 template <>
232 float
233 PropertyDefinition_lexicalCastTo<float>(std::string const& input)
234 {
235 std::locale::global(std::locale::classic());
236 return boost::lexical_cast<float>(input);
237 }
238
239 template <>
240 long
241 PropertyDefinition_lexicalCastTo<long>(std::string const& input)
242 {
243 return boost::lexical_cast<long>(input);
244 }
245
246 template <>
247 int
248 PropertyDefinition_lexicalCastTo<int>(std::string const& input)
249 {
250 return boost::lexical_cast<int>(input);
251 }
252
253 template <>
254 unsigned long
256 {
257 return boost::lexical_cast<unsigned long>(input);
258 }
259
260 template <>
261 unsigned int
262 PropertyDefinition_lexicalCastTo<unsigned int>(std::string const& input)
263 {
264 return boost::lexical_cast<unsigned int>(input);
265 }
266
267 template <>
268 char
269 PropertyDefinition_lexicalCastTo<char>(std::string const& input)
270 {
271 return boost::lexical_cast<char>(input);
272 }
273
274 template <>
275 unsigned char
277 {
278 return boost::lexical_cast<unsigned char>(input);
279 }
280
281 template <>
282 bool
283 PropertyDefinition_lexicalCastTo<bool>(std::string const& input)
284 {
285 return boost::lexical_cast<bool>(input);
286 }
287
288 std::string
290 const std::string& key)
291 {
292 return iceProperties->getProperty(key);
293 }
294
295 bool
296 PropertyDefinitionBase::isSet(std::string const& prefix,
297 std::string const& propertyName,
298 Ice::PropertiesPtr const& iceProperties) const
299 {
300 if (!iceProperties)
301 {
302 return false;
303 }
304
305 return iceProperties->getPropertyWithDefault(prefix + propertyName, "::NOT-SET::")
306 .compare("::NOT-SET::") != 0;
307 }
308
309} // namespace armarx
bool isSet(std::string const &prefix, std::string const &propertyName, Ice::PropertiesPtr const &iceProperties) const
static std::string icePropertyGet(Ice::PropertiesPtr const &iceProperties, std::string const &key)
PropertyDefinition defines a property that will be available within the PropertyUser.
This exception is thrown if an invalid value was specified for a property.
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
float PropertyDefinition_lexicalCastTo< float >(std::string const &input)
long PropertyDefinition_lexicalCastTo< long >(std::string const &input)
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
std::string PropertyDefinition_lexicalCastToString(float input)
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceUtil::Time PropertyDefinition_parseIceUtilTime(std::string const &input)
unsigned long PropertyDefinition_lexicalCastTo< unsigned long >(std::string const &input)
int PropertyDefinition_lexicalCastTo< int >(std::string const &input)
std::string PropertyDefinition_toLowerCopy(const std::string &input)
double PropertyDefinition_lexicalCastTo< double >(std::string const &input)
std::vector< std::string > PropertyDefinition_parseVectorString(std::string const &input)
unsigned char PropertyDefinition_lexicalCastTo< unsigned char >(std::string const &input)
char PropertyDefinition_lexicalCastTo< char >(std::string const &input)
unsigned int PropertyDefinition_lexicalCastTo< unsigned int >(std::string const &input)
bool PropertyDefinition_lexicalCastTo< bool >(std::string const &input)
bool PropertyDefinition_matchRegex(std::string const &regex, std::string const &value)
std::map< std::string, std::string > PropertyDefinition_parseMapStringString(const std::string &input)