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


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