helper_functions.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 
36 #ifndef ROS_INTROSPECTION_HELPER_H
37 #define ROS_INTROSPECTION_HELPER_H
38 
39 #include <functional>
42 
43 namespace RosIntrospection{
44 
45 template< class T>
46 using Span = nonstd::span<T>;
47 
48 // Brutally faster for numbers below 100
49 inline int print_number(char* buffer, uint16_t value)
50 {
51  const char DIGITS[] =
52  "00010203040506070809"
53  "10111213141516171819"
54  "20212223242526272829"
55  "30313233343536373839"
56  "40414243444546474849"
57  "50515253545556575859"
58  "60616263646566676869"
59  "70717273747576777879"
60  "80818283848586878889"
61  "90919293949596979899";
62  if (value < 10)
63  {
64  buffer[0] = static_cast<char>('0' + value);
65  return 1;
66  }
67  else if (value < 100) {
68  value *= 2;
69  buffer[0] = DIGITS[ value ];
70  buffer[1] = DIGITS[ value+1 ];
71  return 2;
72  }
73  else{
74  return sprintf( buffer,"%d", value );
75  }
76 }
77 
78 
79 // helper function to deserialize raw memory
80 template <typename T> inline void ReadFromBuffer( const Span<uint8_t>& buffer, size_t& offset, T& destination)
81 {
82  if ( offset + sizeof(T) > static_cast<std::size_t>(buffer.size()) )
83  {
84  throw std::runtime_error("Buffer overrun in RosIntrospection::ReadFromBuffer");
85  }
86  destination = (*( reinterpret_cast<const T*>( &(buffer.data()[offset]) ) ) );
87  offset += sizeof(T);
88 }
89 
90 template <> inline void ReadFromBuffer( const Span<uint8_t>& buffer, size_t& offset, std::string& destination)
91 {
92  uint32_t string_size = 0;
93  ReadFromBuffer( buffer, offset, string_size );
94 
95  if( offset + string_size > static_cast<std::size_t>(buffer.size()) )
96  {
97  throw std::runtime_error("Buffer overrun in RosIntrospection::ReadFromBuffer");
98  }
99 
100  const char* buffer_ptr = reinterpret_cast<const char*>( &buffer[offset] );
101  offset += string_size;
102 
103  destination.assign( buffer_ptr, string_size );
104 }
105 
106 template <typename T> inline
107 Variant ReadFromBufferToVariant( const Span<uint8_t>& buffer, size_t& offset)
108 {
109  T destination;
110  ReadFromBuffer(buffer, offset, destination);
111  return Variant(destination);
112 }
113 
114 inline Variant ReadFromBufferToVariant(BuiltinType id, const Span<uint8_t>& buffer, size_t& offset)
115 {
116  switch(id)
117  {
118  case BOOL: return ReadFromBufferToVariant<bool>(buffer,offset);
119  case CHAR: return ReadFromBufferToVariant<char>(buffer,offset);
120  case BYTE:
121  case UINT8: return ReadFromBufferToVariant<uint8_t>(buffer,offset);
122  case UINT16: return ReadFromBufferToVariant<uint16_t>(buffer,offset);
123  case UINT32: return ReadFromBufferToVariant<uint32_t>(buffer,offset);
124  case UINT64: return ReadFromBufferToVariant<uint64_t>(buffer,offset);
125 
126  case INT8: return ReadFromBufferToVariant<int8_t>(buffer,offset);
127  case INT16: return ReadFromBufferToVariant<int16_t>(buffer,offset);
128  case INT32: return ReadFromBufferToVariant<int32_t>(buffer,offset);
129  case INT64: return ReadFromBufferToVariant<int64_t>(buffer,offset);
130 
131  case FLOAT32: return ReadFromBufferToVariant<float>(buffer,offset);
132  case FLOAT64: return ReadFromBufferToVariant<double>(buffer,offset);
133 
134  case TIME: {
135  ros::Time tmp;
136  ReadFromBuffer( buffer, offset, tmp.sec );
137  ReadFromBuffer( buffer, offset, tmp.nsec );
138  return tmp;
139  }
140  case DURATION: {
141  ros::Duration tmp;
142  ReadFromBuffer( buffer, offset, tmp.sec );
143  ReadFromBuffer( buffer, offset, tmp.nsec );
144  return tmp;
145  }
146 
147  case STRING: {
148  uint32_t string_size = 0;
149  ReadFromBuffer( buffer, offset, string_size );
150  if( offset + string_size > static_cast<std::size_t>(buffer.size()) ) {
151  throw std::runtime_error("Buffer overrun");
152  }
153  Variant var_string(reinterpret_cast<const char*>( &buffer[offset] ), string_size );
154  offset += string_size;
155  return var_string;
156  }
157  case OTHER: return -1;
158  default: break;
159  }
160  throw std::runtime_error( "unsupported builtin type value");
161 }
162 
163 
164 } // end namespace
165 
166 
167 #endif
int print_number(char *buffer, uint16_t value)
nonstd::span< T > Span
Variant ReadFromBufferToVariant(const Span< uint8_t > &buffer, size_t &offset)
void ReadFromBuffer(const Span< uint8_t > &buffer, size_t &offset, T &destination)


ros_type_introspection
Author(s): Davide Faconti
autogenerated on Sun Sep 6 2020 03:19:54