ros_message.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 
35 #include <boost/algorithm/string.hpp>
36 #include <boost/utility/string_ref.hpp>
37 #include <boost/utility/string_ref.hpp>
39 #include <boost/regex.hpp>
40 #include <boost/algorithm/string/regex.hpp>
41 
42 namespace RosIntrospection
43 {
44 ROSMessage::ROSMessage(const std::string& msg_def)
45 {
46  std::istringstream messageDescriptor(msg_def);
47  boost::match_results<std::string::const_iterator> what;
48 
49  for (std::string line; std::getline(messageDescriptor, line, '\n');)
50  {
51  std::string::const_iterator begin = line.begin(), end = line.end();
52 
53  static const boost::regex regex("(^\\s*$|^\\s*#)");
54 
55  // Skip empty line or one that is a comment
56  if (boost::regex_search(begin, end, what, regex))
57  {
58  continue;
59  }
60 
61  // Trim start of line
62  line.erase(line.begin(), std::find_if(line.begin(), line.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
63 
64  if (line.compare(0, 5, "MSG: ") == 0)
65  {
66  line.erase(0, 5);
67  _type = ROSType(line);
68  }
69  else
70  {
71  auto new_field = ROSField(line);
72  _fields.push_back(new_field);
73  }
74  }
75 }
76 
77 void ROSMessage::updateMissingPkgNames(const std::vector<const ROSType*>& all_types)
78 {
79  for (ROSField& field : _fields)
80  {
81  // if package name is missing, try to find msgName in the list of known_type
82  if (field.type().pkgName().size() == 0)
83  {
84  for (const ROSType* known_type : all_types)
85  {
86  if (field.type().msgName().compare(known_type->msgName()) == 0)
87  {
88  field._type.setPkgName(known_type->pkgName());
89  break;
90  }
91  }
92  }
93  }
94 }
95 
96 } // namespace RosIntrospection
const ROSField & field(size_t index) const
Get field by index.
Definition: ros_message.hpp:54
const boost::string_ref & pkgName() const
ex.: geometry_msgs/Pose -> "geometry_msgs"
Definition: ros_type.hpp:122
ROSMessage(const std::string &msg_def)
Definition: ros_message.cpp:44
void setPkgName(boost::string_ref new_pkg)
Definition: ros_type.cpp:93
const ROSType & type() const
Definition: ros_field.hpp:59
std::vector< ROSField > _fields
Definition: ros_message.hpp:71
const boost::string_ref & msgName() const
ex.: geometry_msgs/Pose -> "Pose"
Definition: ros_type.hpp:117
A ROSMessage will contain one or more ROSField(s). Each field is little more than a name / type pair...
Definition: ros_field.hpp:52
void updateMissingPkgNames(const std::vector< const ROSType *> &all_types)
Definition: ros_message.cpp:77


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