ros_type.hpp
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 #ifndef ROS_INTROSPECTION_ROSTYPE_H
36 #define ROS_INTROSPECTION_ROSTYPE_H
37 
38 #include <vector>
39 #include <map>
40 #include <iostream>
41 #include <functional>
43 
44 namespace RosIntrospection{
45 
46 
50 class ROSType {
51 public:
52 
53  ROSType(){}
54 
56 
57  ROSType(const ROSType& other) { *this = other; }
58 
59  ROSType(ROSType&& other) { *this = other; }
60 
61  ROSType& operator= (const ROSType& other);
62 
63  ROSType& operator= (ROSType&& other);
64 
67  const std::string& baseName() const;
68 
70  const absl::string_view& msgName() const;
71 
73  const absl::string_view& pkgName() const;
74 
75  void setPkgName(absl::string_view new_pkg);
76 
78  bool isBuiltin() const;
79 
81  int typeSize() const;
82 
84  BuiltinType typeID() const;
85 
86  bool operator==(const ROSType& other) const {
87  return _hash == other._hash;
88  }
89 
90  bool operator!=(const ROSType& other) const {
91  return (_hash != other._hash);
92  }
93 
94  bool operator<(const ROSType& other) const {
95  return this->baseName() < other.baseName();
96  }
97 
98  size_t hash() const { return _hash; }
99 
100 protected:
101 
103  std::string _base_name;
106  size_t _hash;
107 
108 };
109 
110 //----------- definitions -------------
111 
112 inline const std::string &ROSType::baseName() const
113 {
114  return _base_name;
115 }
116 
117 inline const absl::string_view& ROSType::msgName() const
118 {
119  return _msg_name;
120 }
121 
122 inline const absl::string_view &ROSType::pkgName() const
123 {
124  return _pkg_name;
125 }
126 
127 inline bool ROSType::isBuiltin() const
128 {
129  return _id != RosIntrospection::OTHER;
130 }
131 
132 inline int ROSType::typeSize() const
133 {
134  return builtinSize( _id );
135 }
136 
138 {
139  return _id;
140 }
141 
142 //--------- helper functions --------------
143 
144 inline std::ostream& operator<<(std::ostream &os, const ROSType& t )
145 {
146  os << t.baseName();
147  return os;
148 }
149 
151  static std::map<absl::string_view, BuiltinType> string_to_builtin_map {
152  { "bool", BOOL },
153  { "byte", BYTE },
154  { "char", CHAR },
155  { "uint8", UINT8 },
156  { "uint16", UINT16 },
157  { "uint32", UINT32 },
158  { "uint64", UINT64 },
159  { "int8", INT8 },
160  { "int16", INT16 },
161  { "int32", INT32 },
162  { "int64", INT64 },
163  { "float32", FLOAT32 },
164  { "float64", FLOAT64 },
165  { "time", TIME },
166  { "duration", DURATION },
167  { "string", STRING },
168  };
169  const auto it = string_to_builtin_map.find(s);
170  return (it != string_to_builtin_map.cend()) ? it->second : OTHER;
171 }
172 
173 }
174 
175 namespace std {
176  template <> struct hash<RosIntrospection::ROSType>
177  {
178 
180  typedef std::size_t result_type;
181 
182  result_type operator()(RosIntrospection::ROSType const& type) const
183  {
184  return type.hash();
185  }
186  };
187 }
188 
189 
190 #endif // ROSTYPE_H
ROSType(ROSType &&other)
Definition: ros_type.hpp:59
const absl::string_view & pkgName() const
ex.: geometry_msgs/Pose -> "geometry_msgs"
Definition: ros_type.hpp:122
result_type operator()(RosIntrospection::ROSType const &type) const
Definition: ros_type.hpp:182
BuiltinType toBuiltinType(const absl::string_view &s)
Definition: ros_type.hpp:150
absl::string_view _msg_name
Definition: ros_type.hpp:104
ROSType & operator=(const ROSType &other)
Definition: ros_type.cpp:68
absl::string_view _pkg_name
Definition: ros_type.hpp:105
std::ostream & operator<<(std::ostream &os, const BuiltinType &c)
int typeSize() const
If builtin, size of builtin, -1 means variable or undefined.
Definition: ros_type.hpp:132
bool operator!=(const ROSType &other) const
Definition: ros_type.hpp:90
const std::string & baseName() const
Definition: ros_type.hpp:112
int builtinSize(const BuiltinType c)
bool operator<(const ROSType &other) const
Definition: ros_type.hpp:94
const absl::string_view & msgName() const
ex.: geometry_msgs/Pose -> "Pose"
Definition: ros_type.hpp:117
RosIntrospection::ROSType argument_type
Definition: ros_type.hpp:179
char name[1]
bool operator==(const ROSType &other) const
Definition: ros_type.hpp:86
ROSType(const ROSType &other)
Definition: ros_type.hpp:57
void setPkgName(absl::string_view new_pkg)
Definition: ros_type.cpp:93
size_t hash() const
Definition: ros_type.hpp:98
BuiltinType typeID() const
If type is builtin, returns the id. BuiltinType::OTHER otherwise.
Definition: ros_type.hpp:137
bool isBuiltin() const
True if the type is ROS builtin.
Definition: ros_type.hpp:127


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