ros_field.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright 2016-2017 Davide Faconti
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 * *******************************************************************/
34 
36 #include <boost/regex.hpp>
37 #include <boost/algorithm/string/regex.hpp>
38 #include <boost/algorithm/string/trim.hpp>
39 
40 namespace RosIntrospection{
41 
42 ROSField::ROSField(const std::string &definition):
43  _array_size(1)
44 {
45  static const boost::regex type_regex("[a-zA-Z][a-zA-Z0-9_]*"
46  "(/[a-zA-Z][a-zA-Z0-9_]*){0,1}"
47  "(\\[[0-9]*\\]){0,1}");
48 
49  static const boost::regex field_regex("[a-zA-Z][a-zA-Z0-9_]*");
50 
51  static const boost::regex array_regex("(.+)(\\[([0-9]*)\\])");
52 
53  using boost::regex;
54  std::string::const_iterator begin = definition.begin();
55  std::string::const_iterator end = definition.end();
56  boost::match_results<std::string::const_iterator> what;
57 
58  // Get type and field
59  std::string type, value;
60 
61  //-------------------------------
62  // Find type, field and array size
63  if( regex_search(begin, end, what, type_regex)) {
64  type = what[0];
65  begin = what[0].second;
66  }
67  else {
68  throw std::runtime_error("Bad type when parsing field: " + definition);
69  }
70 
71  if (regex_search(begin, end, what, field_regex))
72  {
73  _fieldname = what[0];
74  begin = what[0].second;
75  }
76  else {
77  throw std::runtime_error("Bad field when parsing field: " + definition);
78  }
79 
80  std::string temp_type = type;
81  if (regex_search(temp_type, what, array_regex))
82  {
83  type = what[1];
84 
85  if (what.size() == 3) {
86  _array_size = -1;
87  }
88  else if (what.size() == 4) {
89  std::string size(what[3].first, what[3].second);
90  _array_size = size.empty() ? -1 : atoi(size.c_str());
91  }
92  else {
93  throw std::runtime_error("Bad array size when parsing field: " + definition);
94  }
95  }
96 
97  //-------------------------------
98  // Find if Constant or comment
99 
100  // Determine next character
101  // if '=' -> constant, if '#' -> done, if nothing -> done, otherwise error
102  if (regex_search(begin, end, what, boost::regex("\\S")))
103  {
104  if (what[0] == "=")
105  {
106  begin = what[0].second;
107  // Copy constant
108  if (type == "string") {
109  value.assign(begin, end);
110  }
111  else {
112  if (regex_search(begin, end, what, boost::regex("\\s*#")))
113  {
114  value.assign(begin, what[0].first);
115  }
116  else {
117  value.assign(begin, end);
118  }
119  // TODO: Raise error if string is not numeric
120  }
121 
122  boost::algorithm::trim(value);
123  } else if (what[0] == "#") {
124  // Ignore comment
125  } else {
126  // Error
127  throw std::runtime_error("Unexpected character after type and field: " +
128  definition);
129  }
130  }
131  _type = ROSType( type );
132  _value = value;
133 }
134 
135 }
const std::string & value() const
If constant, value of field, else undefined.
Definition: ros_field.hpp:67
char * begin
char * end
ROSField(const std::string &definition)
Definition: ros_field.cpp:42
const ROSType & type() const
Definition: ros_field.hpp:59
uintptr_t size


ros_type_introspection
Author(s): Davide Faconti
autogenerated on Thu May 16 2019 02:39:09