builtin_types.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_BUILTIN_TYPES_HPP
25 #define ROS_BUILTIN_TYPES_HPP
26 
27 #include <stdint.h>
28 #include <string>
29 #include <iostream>
30 
33 
34 namespace RosMsgParser
35 {
36 
37 template <class T>
39 
40 template <class T, size_t N>
42 
44 {
62 };
63 
64 //---------------------------------------------------------
65 
66 inline int builtinSize(const BuiltinType c)
67 {
68  switch (c)
69  {
70  case BOOL:
71  case BYTE:
72  case INT8:
73  case CHAR:
74  case UINT8:
75  return 1;
76  case UINT16:
77  case INT16:
78  return 2;
79  case UINT32:
80  case INT32:
81  case FLOAT32:
82  return 4;
83  case UINT64:
84  case INT64:
85  case FLOAT64:
86  case TIME:
87  case DURATION:
88  return 8;
89  case STRING:
90  case OTHER:
91  return -1;
92  }
93  throw std::runtime_error("unsupported builtin type value");
94 }
95 
96 inline const char* toStr(const BuiltinType& c)
97 {
98  switch (c)
99  {
100  case BOOL:
101  return "BOOL";
102  case BYTE:
103  return "BYTE";
104  case INT8:
105  return "INT8";
106  case CHAR:
107  return "CHAR";
108  case UINT8:
109  return "UINT8";
110  case UINT16:
111  return "UINT16";
112  case UINT32:
113  return "UINT32";
114  case UINT64:
115  return "UINT64";
116  case INT16:
117  return "INT16";
118  case INT32:
119  return "INT32";
120  case INT64:
121  return "INT64";
122  case FLOAT32:
123  return "FLOAT32";
124  case FLOAT64:
125  return "FLOAT64";
126  case TIME:
127  return "TIME";
128  case DURATION:
129  return "DURATION";
130  case STRING:
131  return "STRING";
132  case OTHER:
133  return "OTHER";
134  }
135  throw std::runtime_error("unsupported builtin type value");
136 }
137 
138 inline std::ostream& operator<<(std::ostream& os, const BuiltinType& c)
139 {
140  os << toStr(c);
141  return os;
142 }
143 
144 template <typename T>
146 {
147  return OTHER;
148 }
149 
150 struct Time
151 {
152  uint32_t sec;
153  uint32_t nsec;
154 
155  double toSec()
156  {
157  return double(sec) + double(nsec) * 1e-9;
158  }
159 };
160 
161 template <>
163 {
164  return BOOL;
165 }
166 
167 template <>
169 {
170  return CHAR;
171 }
172 
173 template <>
175 {
176  return INT8;
177 }
178 template <>
180 {
181  return INT16;
182 }
183 template <>
185 {
186  return INT32;
187 }
188 template <>
190 {
191  return INT64;
192 }
193 
194 template <>
196 {
197  return UINT8;
198 }
199 template <>
201 {
202  return UINT16;
203 }
204 template <>
206 {
207  return UINT32;
208 }
209 template <>
211 {
212  return UINT64;
213 }
214 
215 template <>
217 {
218  return FLOAT32;
219 }
220 template <>
222 {
223  return FLOAT64;
224 }
225 
226 template <>
227 inline BuiltinType getType<std::string>()
228 {
229  return STRING;
230 }
231 
232 template <>
233 inline BuiltinType getType<RosMsgParser::Time>()
234 {
235  return TIME;
236 }
237 
238 } // namespace RosMsgParser
239 
240 #endif // ROS_BUILTIN_TYPES_HPP
RosMsgParser::FLOAT32
@ FLOAT32
Definition: builtin_types.hpp:56
RosMsgParser::getType
BuiltinType getType()
Definition: builtin_types.hpp:145
RosMsgParser::Time::nsec
uint32_t nsec
Definition: builtin_types.hpp:153
RosMsgParser::getType< uint16_t >
BuiltinType getType< uint16_t >()
Definition: builtin_types.hpp:200
RosMsgParser::getType< int32_t >
BuiltinType getType< int32_t >()
Definition: builtin_types.hpp:184
RosMsgParser::INT8
@ INT8
Definition: builtin_types.hpp:52
RosMsgParser::Time::sec
uint32_t sec
Definition: builtin_types.hpp:152
RosMsgParser::BYTE
@ BYTE
Definition: builtin_types.hpp:46
RosMsgParser::Time::toSec
double toSec()
Definition: builtin_types.hpp:155
RosMsgParser::getType< uint32_t >
BuiltinType getType< uint32_t >()
Definition: builtin_types.hpp:205
RosMsgParser::getType< uint64_t >
BuiltinType getType< uint64_t >()
Definition: builtin_types.hpp:210
RosMsgParser::TIME
@ TIME
Definition: builtin_types.hpp:58
RosMsgParser::getType< int64_t >
BuiltinType getType< int64_t >()
Definition: builtin_types.hpp:189
RosMsgParser::UINT8
@ UINT8
Definition: builtin_types.hpp:48
RosMsgParser::FLOAT64
@ FLOAT64
Definition: builtin_types.hpp:57
RosMsgParser
Definition: builtin_types.hpp:34
RosMsgParser::UINT64
@ UINT64
Definition: builtin_types.hpp:51
RosMsgParser::toStr
const char * toStr(const BuiltinType &c)
Definition: builtin_types.hpp:96
RosMsgParser::DURATION
@ DURATION
Definition: builtin_types.hpp:59
RosMsgParser::UINT32
@ UINT32
Definition: builtin_types.hpp:50
RosMsgParser::UINT16
@ UINT16
Definition: builtin_types.hpp:49
RosMsgParser::OTHER
@ OTHER
Definition: builtin_types.hpp:61
RosMsgParser::getType< int8_t >
BuiltinType getType< int8_t >()
Definition: builtin_types.hpp:174
RosMsgParser::getType< float >
BuiltinType getType< float >()
Definition: builtin_types.hpp:216
RosMsgParser::getType< double >
BuiltinType getType< double >()
Definition: builtin_types.hpp:221
nonstd::span_lite::span
Definition: span.hpp:581
RosMsgParser::STRING
@ STRING
Definition: builtin_types.hpp:60
llvm_vecsmall::SmallVector
Definition: SmallVector.h:1029
RosMsgParser::getType< uint8_t >
BuiltinType getType< uint8_t >()
Definition: builtin_types.hpp:195
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::BOOL
@ BOOL
Definition: builtin_types.hpp:45
SmallVector.h
RosMsgParser::getType< int16_t >
BuiltinType getType< int16_t >()
Definition: builtin_types.hpp:179
RosMsgParser::Time
Definition: builtin_types.hpp:150
RosMsgParser::builtinSize
int builtinSize(const BuiltinType c)
Definition: builtin_types.hpp:66
span.hpp
RosMsgParser::getType< char >
BuiltinType getType< char >()
Definition: builtin_types.hpp:168
RosMsgParser::INT16
@ INT16
Definition: builtin_types.hpp:53
RosMsgParser::getType< bool >
BuiltinType getType< bool >()
Definition: builtin_types.hpp:162
RosMsgParser::BuiltinType
BuiltinType
Definition: builtin_types.hpp:43
RosMsgParser::INT32
@ INT32
Definition: builtin_types.hpp:54
RosMsgParser::INT64
@ INT64
Definition: builtin_types.hpp:55


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