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 
25 #include "PropertyDefinition.hpp"
26 
27 #include <Ice/Properties.h>
28 
29 #include <SimoxUtility/algorithm/string/string_tools.h>
30 
31 #include <boost/regex.hpp>
32 #include <boost/lexical_cast.hpp>
33 
34 #include <regex>
35 
36 namespace armarx
37 {
38  template class PropertyDefinition<int>;
39  template class PropertyDefinition<float>;
40  template class PropertyDefinition<double>;
41  template class PropertyDefinition<std::string>;
42  template class PropertyDefinition<bool>;
43 
44  std::string PropertyDefinition_toLowerCopy(const std::string& input)
45  {
46  return simox::alg::to_lower(input);
47  }
48 
49  bool PropertyDefinition_matchRegex(std::string const& regex, std::string const& value)
50  {
51  boost::regex expr(regex);
52  return boost::regex_match(value, expr);
53  }
54 
55  std::map<std::string, std::string> PropertyDefinition_parseMapStringString(const std::string& input)
56  {
57  std::map<std::string, std::string> result;
58  auto splits = armarx::Split(input, ",");
59  for (auto& elem : splits)
60  {
61  simox::alg::trim(elem);
62  auto split = armarx::Split(elem, ":");
63  if (split.size() == 2)
64  {
65  result[simox::alg::trim_copy(split[0])] = simox::alg::trim_copy(split[1]);
66  }
67  }
68  return result;
69  }
70 
71  std::vector<std::string> PropertyDefinition_parseVectorString(std::string const& input)
72  {
73  std::vector<std::string> args;
74 
75  const boost::regex re(",(?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)");
76 
77  boost::match_results<std::string::const_iterator> what;
78  boost::match_flag_type flags = boost::match_default;
79  std::string::const_iterator s = input.begin();
80  std::string::const_iterator e = input.end();
81  size_t begin = 0;
82  while (boost::regex_search(s, e, what, re, flags))
83  {
84  int pos = what.position();
85  auto arg = input.substr(begin, pos);
86  simox::alg::trim(arg);
87  if (!arg.empty())
88  {
89  args.push_back(arg);
90  }
91  std::string::difference_type l = what.length();
92  std::string::difference_type p = what.position();
93  begin += l + p;
94  s += p + l;
95  }
96  std::string lastArg = input.substr(begin);
97  simox::alg::trim(lastArg);
98  if (!lastArg.empty())
99  {
100  args.push_back(lastArg);
101  }
102  return args;
103  }
104 
106  {
107  std::regex re_number{"^[0-9]*\\.?[0-9]+"};
108  std::regex re_unit{"[a-zµ]+$"};
109  std::smatch number_match;
110  std::smatch unit_match;
111 
112  if (not std::regex_search(input.begin(), input.end(), number_match, re_number))
113  {
115  throw InvalidPropertyValueException("unknown", input)
116  << "Could not parse Time (number invalid).";
117  }
118 
119  if (not std::regex_search(input.begin(), input.end(), unit_match, re_unit))
120  {
122  throw InvalidPropertyValueException("unknown", input)
123  << "Could not parse Time (unit invalid).";
124  }
125 
126  const double value = boost::lexical_cast<double>(number_match[0]);
127 
128  static constexpr long h2us = 1000l * 1000 * 60 * 60;
129  static constexpr long m2us = 1000 * 1000 * 60;
130  static constexpr long s2us = 1000 * 1000;
131  static constexpr long ms2us = 1000;
132  static constexpr long us = 1;
133 
134  static std::map<std::string, long> conv_map;
135  // Hours.
136  conv_map["h"] = h2us;
137  conv_map["hrs"] = h2us;
138  conv_map["hour"] = h2us;
139  conv_map["hours"] = h2us;
140  // Minutes.
141  conv_map["m"] = m2us;
142  conv_map["min"] = m2us;
143  conv_map["minute"] = m2us;
144  conv_map["minutes"] = m2us;
145  // Seconds.
146  conv_map["s"] = s2us;
147  conv_map["sec"] = s2us;
148  conv_map["second"] = s2us;
149  conv_map["seconds"] = s2us;
150  // Milliseconds.
151  conv_map["ms"] = ms2us;
152  conv_map["msec"] = ms2us;
153  conv_map["millisecond"] = ms2us;
154  conv_map["milliseconds"] = ms2us;
155  // Microseconds.
156  conv_map["µs"] = us;
157  conv_map["us"] = us;
158  conv_map["µsec"] = us;
159  conv_map["usec"] = us;
160  conv_map["microsecond"] = us;
161  conv_map["microseconds"] = us;
162 
163  if (conv_map.find(unit_match[0]) == conv_map.end())
164  {
166  throw InvalidPropertyValueException("unknown", input)
167  << "Invalid/unknown unit `" << unit_match[0] << "`.";
168  }
169 
170  const long int to_microseconds_mult = conv_map[unit_match[0]];
171  return IceUtil::Time::microSeconds(value * to_microseconds_mult);
172  }
173 
175  {
176  std::locale::global(std::locale::classic());
177  return boost::lexical_cast<std::string>(input);
178  }
179 
181  {
182  std::locale::global(std::locale::classic());
183  return boost::lexical_cast<std::string>(input);
184  }
185 
187  {
188  return boost::lexical_cast<std::string>(input);
189  }
190 
192  {
193  return boost::lexical_cast<std::string>(input);
194  }
195 
197  {
198  return boost::lexical_cast<std::string>(input);
199  }
200 
202  {
203  return boost::lexical_cast<std::string>(input);
204  }
205 
206  std::string PropertyDefinition_lexicalCastToString(std::string const& input)
207  {
208  return input;
209  }
210 
211  template <>
213  {
214  std::locale::global(std::locale::classic());
215  return boost::lexical_cast<double>(input);
216  }
217 
218  template <>
220  {
221  std::locale::global(std::locale::classic());
222  return boost::lexical_cast<float>(input);
223  }
224 
225  template <>
227  {
228  return boost::lexical_cast<long>(input);
229  }
230 
231  template <>
233  {
234  return boost::lexical_cast<int>(input);
235  }
236 
237  template <>
238  unsigned long PropertyDefinition_lexicalCastTo<unsigned long>(std::string const& input)
239  {
240  return boost::lexical_cast<unsigned long>(input);
241  }
242 
243  template <>
244  unsigned int PropertyDefinition_lexicalCastTo<unsigned int>(std::string const& input)
245  {
246  return boost::lexical_cast<unsigned int>(input);
247  }
248 
249  template <>
251  {
252  return boost::lexical_cast<char>(input);
253  }
254 
255  template <>
256  unsigned char PropertyDefinition_lexicalCastTo<unsigned char>(std::string const& input)
257  {
258  return boost::lexical_cast<unsigned char>(input);
259  }
260 
261  template <>
263  {
264  return boost::lexical_cast<bool>(input);
265  }
266 
267  std::string PropertyDefinitionBase::icePropertyGet(const Ice::PropertiesPtr& iceProperties, const std::string& key)
268  {
269  return iceProperties->getProperty(key);
270  }
271 
272  bool PropertyDefinitionBase::isSet(std::string const& prefix, std::string const& propertyName, Ice::PropertiesPtr const& iceProperties) const
273  {
274  if (!iceProperties)
275  {
276  return false;
277  }
278 
279  return iceProperties->getPropertyWithDefault(prefix + propertyName, "::NOT-SET::")
280  .compare("::NOT-SET::") != 0;
281  }
282 
283 }
armarx::PropertyDefinition_parseVectorString
std::vector< std::string > PropertyDefinition_parseVectorString(std::string const &input)
Definition: PropertyDefinition.cpp:71
armarx::PropertyDefinition_lexicalCastTo< float >
float PropertyDefinition_lexicalCastTo< float >(std::string const &input)
Definition: PropertyDefinition.cpp:219
armarx::PropertyDefinition_lexicalCastTo< unsigned long >
unsigned long PropertyDefinition_lexicalCastTo< unsigned long >(std::string const &input)
Definition: PropertyDefinition.cpp:238
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
armarx::PropertyDefinition_lexicalCastTo< long >
long PropertyDefinition_lexicalCastTo< long >(std::string const &input)
Definition: PropertyDefinition.cpp:226
IceInternal::Handle< ::Ice::Properties >
armarx::PropertyDefinition_parseIceUtilTime
IceUtil::Time PropertyDefinition_parseIceUtilTime(std::string const &input)
Definition: PropertyDefinition.cpp:105
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::PropertyDefinition_parseMapStringString
std::map< std::string, std::string > PropertyDefinition_parseMapStringString(const std::string &input)
Definition: PropertyDefinition.cpp:55
armarx::PropertyDefinition_lexicalCastTo< int >
int PropertyDefinition_lexicalCastTo< int >(std::string const &input)
Definition: PropertyDefinition.cpp:232
armarx::PropertyDefinitionBase::icePropertyGet
static std::string icePropertyGet(Ice::PropertiesPtr const &iceProperties, std::string const &key)
Definition: PropertyDefinition.cpp:267
armarx::PropertyDefinitionBase::isSet
bool isSet(std::string const &prefix, std::string const &propertyName, Ice::PropertiesPtr const &iceProperties) const
Definition: PropertyDefinition.cpp:272
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_lexicalCastTo< unsigned char >
unsigned char PropertyDefinition_lexicalCastTo< unsigned char >(std::string const &input)
Definition: PropertyDefinition.cpp:256
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::PropertyDefinition_toLowerCopy
std::string PropertyDefinition_toLowerCopy(const std::string &input)
Definition: PropertyDefinition.cpp:44
armarx::PropertyDefinition_matchRegex
bool PropertyDefinition_matchRegex(std::string const &regex, std::string const &value)
Definition: PropertyDefinition.cpp:49
armarx::exceptions::local::InvalidPropertyValueException
This exception is thrown if an invalid value was specified for a property.
Definition: InvalidPropertyValueException.h:38
PropertyDefinition.hpp
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::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::PropertyDefinition_lexicalCastTo< double >
double PropertyDefinition_lexicalCastTo< double >(std::string const &input)
Definition: PropertyDefinition.cpp:212
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36