ros_type.hpp
Go to the documentation of this file.
1 /***** MIT License ****
2  *
3  * Copyright (c) 2016-2022 Davide Faconti
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef ROS_INTROSPECTION_ROSTYPE_H
25 #define ROS_INTROSPECTION_ROSTYPE_H
26 
27 #include <vector>
28 #include <map>
29 #include <iostream>
30 #include <functional>
32 
33 namespace RosMsgParser
34 {
35 
39 class ROSType
40 {
41 public:
43  {
44  }
45 
46  ROSType(const std::string& name);
47 
48  ROSType(const ROSType& other)
49  {
50  *this = other;
51  }
52 
53  ROSType(ROSType&& other)
54  {
55  *this = other;
56  }
57 
58  ROSType& operator=(const ROSType& other);
59 
60  ROSType& operator=(ROSType&& other);
61 
64  const std::string& baseName() const;
65 
67  const std::string_view& msgName() const;
68 
70  const std::string_view& pkgName() const;
71 
72  void setPkgName(std::string_view new_pkg);
73 
75  bool isBuiltin() const;
76 
78  int typeSize() const;
79 
81  BuiltinType typeID() const;
82 
83  bool operator==(const ROSType& other) const
84  {
85  return _hash == other._hash;
86  }
87 
88  bool operator!=(const ROSType& other) const
89  {
90  return (_hash != other._hash);
91  }
92 
93  bool operator<(const ROSType& other) const
94  {
95  return this->baseName() < other.baseName();
96  }
97 
98  size_t hash() const
99  {
100  return _hash;
101  }
102 
103 protected:
105  std::string _base_name;
108  size_t _hash = 0;
109 };
110 
111 //----------- definitions -------------
112 
113 inline const std::string& ROSType::baseName() const
114 {
115  return _base_name;
116 }
117 
118 inline const std::string_view& ROSType::msgName() const
119 {
120  return _msg_name;
121 }
122 
123 inline const std::string_view& ROSType::pkgName() const
124 {
125  return _pkg_name;
126 }
127 
128 inline bool ROSType::isBuiltin() const
129 {
130  return _id != RosMsgParser::OTHER;
131 }
132 
133 inline int ROSType::typeSize() const
134 {
135  return builtinSize(_id);
136 }
137 
139 {
140  return _id;
141 }
142 
143 //--------- helper functions --------------
144 
145 inline std::ostream& operator<<(std::ostream& os, const ROSType& t)
146 {
147  os << t.baseName();
148  return os;
149 }
150 
152 {
153  static std::map<std::string_view, BuiltinType> string_to_builtin_map{
154  { "bool", BOOL }, { "byte", BYTE }, { "char", CHAR },
155  { "uint8", UINT8 }, { "uint16", UINT16 }, { "uint32", UINT32 },
156  { "uint64", UINT64 }, { "int8", INT8 }, { "int16", INT16 },
157  { "int32", INT32 }, { "int64", INT64 }, { "float32", FLOAT32 },
158  { "float64", FLOAT64 }, { "time", TIME }, { "duration", DURATION },
159  { "string", STRING },
160  };
161  const auto it = string_to_builtin_map.find(s);
162  return (it != string_to_builtin_map.cend()) ? it->second : OTHER;
163 }
164 
165 } // namespace RosMsgParser
166 
167 namespace std
168 {
169 template <>
170 struct hash<RosMsgParser::ROSType>
171 {
174 
176  {
177  return type.hash();
178  }
179 };
180 } // namespace std
181 
182 #endif // ROSTYPE_H
RosMsgParser::FLOAT32
@ FLOAT32
Definition: builtin_types.hpp:56
RosMsgParser::ROSType::pkgName
const std::string_view & pkgName() const
ex.: geometry_msgs/Pose -> "geometry_msgs"
Definition: ros_type.hpp:123
nlohmann::detail::hash
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:5097
std::hash< RosMsgParser::ROSType >::result_type
std::size_t result_type
Definition: ros_type.hpp:173
RosMsgParser::ROSType
ROSType.
Definition: ros_type.hpp:39
RosMsgParser::ROSType::typeID
BuiltinType typeID() const
If type is builtin, returns the id. BuiltinType::OTHER otherwise.
Definition: ros_type.hpp:138
nonstd::span_lite::size_t
span_CONFIG_SIZE_TYPE size_t
Definition: span.hpp:576
RosMsgParser::ROSType::msgName
const std::string_view & msgName() const
ex.: geometry_msgs/Pose -> "Pose"
Definition: ros_type.hpp:118
RosMsgParser::ROSType::operator<
bool operator<(const ROSType &other) const
Definition: ros_type.hpp:93
s
XmlRpcServer s
RosMsgParser::INT8
@ INT8
Definition: builtin_types.hpp:52
RosMsgParser::ROSType::operator==
bool operator==(const ROSType &other) const
Definition: ros_type.hpp:83
RosMsgParser::ROSType::operator=
ROSType & operator=(const ROSType &other)
Definition: ros_type.cpp:57
RosMsgParser::TIME
@ TIME
Definition: builtin_types.hpp:58
RosMsgParser::ROSType::_hash
size_t _hash
Definition: ros_type.hpp:108
RosMsgParser::ROSType::ROSType
ROSType()
Definition: ros_type.hpp:42
RosMsgParser::toBuiltinType
BuiltinType toBuiltinType(const std::string_view &s)
Definition: ros_type.hpp:151
std::hash< RosMsgParser::ROSType >::operator()
result_type operator()(RosMsgParser::ROSType const &type) const
Definition: ros_type.hpp:175
RosMsgParser::UINT8
@ UINT8
Definition: builtin_types.hpp:48
RosMsgParser::FLOAT64
@ FLOAT64
Definition: builtin_types.hpp:57
RosMsgParser
Definition: builtin_types.hpp:34
RosMsgParser::ROSType::setPkgName
void setPkgName(std::string_view new_pkg)
Definition: ros_type.cpp:83
RosMsgParser::ROSType::_base_name
std::string _base_name
Definition: ros_type.hpp:105
BYTE
unsigned char BYTE
Definition: lz4.c:312
std::hash< RosMsgParser::ROSType >::argument_type
RosMsgParser::ROSType argument_type
Definition: ros_type.hpp:172
RosMsgParser::UINT64
@ UINT64
Definition: builtin_types.hpp:51
RosMsgParser::ROSType::hash
size_t hash() const
Definition: ros_type.hpp:98
RosMsgParser::DURATION
@ DURATION
Definition: builtin_types.hpp:59
RosMsgParser::ROSType::ROSType
ROSType(const ROSType &other)
Definition: ros_type.hpp:48
RosMsgParser::UINT32
@ UINT32
Definition: builtin_types.hpp:50
RosMsgParser::UINT16
@ UINT16
Definition: builtin_types.hpp:49
RosMsgParser::ROSType::isBuiltin
bool isBuiltin() const
True if the type is ROS builtin.
Definition: ros_type.hpp:128
string_view
basic_string_view< char > string_view
Definition: core.h:518
RosMsgParser::OTHER
@ OTHER
Definition: builtin_types.hpp:61
RosMsgParser::ROSType::ROSType
ROSType(ROSType &&other)
Definition: ros_type.hpp:53
std
RosMsgParser::STRING
@ STRING
Definition: builtin_types.hpp:60
RosMsgParser::ROSType::_id
BuiltinType _id
Definition: ros_type.hpp:104
RosMsgParser::operator<<
std::ostream & operator<<(std::ostream &os, const BuiltinType &c)
Definition: builtin_types.hpp:138
RosMsgParser::CHAR
@ CHAR
Definition: builtin_types.hpp:47
RosMsgParser::ROSType::baseName
const std::string & baseName() const
Definition: ros_type.hpp:113
variant.hpp
RosMsgParser::ROSType::typeSize
int typeSize() const
If builtin, size of builtin, -1 means variable or undefined.
Definition: ros_type.hpp:133
RosMsgParser::BOOL
@ BOOL
Definition: builtin_types.hpp:45
RosMsgParser::ROSType::operator!=
bool operator!=(const ROSType &other) const
Definition: ros_type.hpp:88
RosMsgParser::builtinSize
int builtinSize(const BuiltinType c)
Definition: builtin_types.hpp:66
RosMsgParser::INT16
@ INT16
Definition: builtin_types.hpp:53
RosMsgParser::ROSType::_msg_name
std::string_view _msg_name
Definition: ros_type.hpp:106
RosMsgParser::BuiltinType
BuiltinType
Definition: builtin_types.hpp:43
RosMsgParser::INT32
@ INT32
Definition: builtin_types.hpp:54
RosMsgParser::INT64
@ INT64
Definition: builtin_types.hpp:55
RosMsgParser::ROSType::_pkg_name
std::string_view _pkg_name
Definition: ros_type.hpp:107


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46