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) : _array_size(1)
43 {
44  static const boost::regex type_regex("[a-zA-Z][a-zA-Z0-9_]*"
45  "(/[a-zA-Z][a-zA-Z0-9_]*){0,1}"
46  "(\\[[0-9]*\\]){0,1}");
47 
48  static const boost::regex field_regex("[a-zA-Z][a-zA-Z0-9_]*");
49 
50  static const boost::regex array_regex("(.+)(\\[([0-9]*)\\])");
51 
52  static const boost::regex regex_A("\\S");
53 
54  static const boost::regex regex_B("\\s*#");
55 
56  using boost::regex;
57  std::string::const_iterator begin = definition.begin();
58  std::string::const_iterator end = definition.end();
59  boost::match_results<std::string::const_iterator> what;
60 
61  // Get type and field
62  std::string type, value;
63 
64  //-------------------------------
65  // Find type, field and array size
66  if (regex_search(begin, end, what, type_regex))
67  {
68  type = what[0];
69  begin = what[0].second;
70  }
71  else
72  {
73  throw std::runtime_error("Bad type when parsing field: " + definition);
74  }
75 
76  if (regex_search(begin, end, what, field_regex))
77  {
78  _fieldname = what[0];
79  begin = what[0].second;
80  }
81  else
82  {
83  throw std::runtime_error("Bad field when parsing field: " + definition);
84  }
85 
86  std::string temp_type = type;
87  if (regex_search(temp_type, what, array_regex))
88  {
89  type = what[1];
90 
91  if (what.size() == 3)
92  {
93  _array_size = -1;
94  }
95  else if (what.size() == 4)
96  {
97  std::string size(what[3].first, what[3].second);
98  _array_size = size.empty() ? -1 : atoi(size.c_str());
99  }
100  else
101  {
102  throw std::runtime_error("Bad array size when parsing field: " + definition);
103  }
104  }
105 
106  //-------------------------------
107  // Find if Constant or comment
108 
109  // Determine next character
110  // if '=' -> constant, if '#' -> done, if nothing -> done, otherwise error
111 
112  if (regex_search(begin, end, what, regex_A))
113  {
114  if (what[0] == "=")
115  {
116  begin = what[0].second;
117  // Copy constant
118  if (type == "string")
119  {
120  value.assign(begin, end);
121  }
122  else
123  {
124  if (regex_search(begin, end, what, regex_B))
125  {
126  value.assign(begin, what[0].first);
127  }
128  else
129  {
130  value.assign(begin, end);
131  }
132  // TODO: Raise error if string is not numeric
133  }
134 
135  boost::algorithm::trim(value);
136  }
137  else if (what[0] == "#")
138  {
139  // Ignore comment
140  }
141  else
142  {
143  // Error
144  throw std::runtime_error("Unexpected character after type and field: " + definition);
145  }
146  }
147  _type = ROSType(type);
148  _value = value;
149 }
150 
151 } // namespace RosIntrospection
const std::string & value() const
If constant, value of field, else undefined.
Definition: ros_field.hpp:67
ROSField(const std::string &definition)
Definition: ros_field.cpp:42
const T & first(const T &value, const Tail &...)
const ROSType & type() const
Definition: ros_field.hpp:59
span_constexpr std::size_t size(span< T, Extent > const &spn)


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Fri Jun 23 2023 02:28:03