capstone_finish

Dependencies:   BufferedSerial motor_sn7544

Committer:
Jeonghoon
Date:
Tue Nov 26 15:30:27 2019 +0000
Revision:
10:ca4e4062701a
Parent:
3:2a3664dc6634
capstone finish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeonghoon 3:2a3664dc6634 1 /*
Jeonghoon 3:2a3664dc6634 2 * Copyright (C) 2009, Willow Garage, Inc.
Jeonghoon 3:2a3664dc6634 3 *
Jeonghoon 3:2a3664dc6634 4 * Redistribution and use in source and binary forms, with or without
Jeonghoon 3:2a3664dc6634 5 * modification, are permitted provided that the following conditions are met:
Jeonghoon 3:2a3664dc6634 6 * * Redistributions of source code must retain the above copyright notice,
Jeonghoon 3:2a3664dc6634 7 * this list of conditions and the following disclaimer.
Jeonghoon 3:2a3664dc6634 8 * * Redistributions in binary form must reproduce the above copyright
Jeonghoon 3:2a3664dc6634 9 * notice, this list of conditions and the following disclaimer in the
Jeonghoon 3:2a3664dc6634 10 * documentation and/or other materials provided with the distribution.
Jeonghoon 3:2a3664dc6634 11 * * Neither the names of Willow Garage, Inc. nor the names of its
Jeonghoon 3:2a3664dc6634 12 * contributors may be used to endorse or promote products derived from
Jeonghoon 3:2a3664dc6634 13 * this software without specific prior written permission.
Jeonghoon 3:2a3664dc6634 14 *
Jeonghoon 3:2a3664dc6634 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Jeonghoon 3:2a3664dc6634 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Jeonghoon 3:2a3664dc6634 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Jeonghoon 3:2a3664dc6634 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Jeonghoon 3:2a3664dc6634 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Jeonghoon 3:2a3664dc6634 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Jeonghoon 3:2a3664dc6634 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Jeonghoon 3:2a3664dc6634 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Jeonghoon 3:2a3664dc6634 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Jeonghoon 3:2a3664dc6634 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Jeonghoon 3:2a3664dc6634 25 * POSSIBILITY OF SUCH DAMAGE.
Jeonghoon 3:2a3664dc6634 26 */
Jeonghoon 3:2a3664dc6634 27
Jeonghoon 3:2a3664dc6634 28 #ifndef ROSCPP_SERIALIZATION_H
Jeonghoon 3:2a3664dc6634 29 #define ROSCPP_SERIALIZATION_H
Jeonghoon 3:2a3664dc6634 30
Jeonghoon 3:2a3664dc6634 31 #include "roscpp_serialization_macros.h"
Jeonghoon 3:2a3664dc6634 32
Jeonghoon 3:2a3664dc6634 33 #include <ros/types.h>
Jeonghoon 3:2a3664dc6634 34 #include <ros/time.h>
Jeonghoon 3:2a3664dc6634 35
Jeonghoon 3:2a3664dc6634 36 #include "serialized_message.h"
Jeonghoon 3:2a3664dc6634 37 #include "ros/message_traits.h"
Jeonghoon 3:2a3664dc6634 38 #include "ros/builtin_message_traits.h"
Jeonghoon 3:2a3664dc6634 39 #include "ros/exception.h"
Jeonghoon 3:2a3664dc6634 40 #include "ros/datatypes.h"
Jeonghoon 3:2a3664dc6634 41
Jeonghoon 3:2a3664dc6634 42 #include <vector>
Jeonghoon 3:2a3664dc6634 43 #include <map>
Jeonghoon 3:2a3664dc6634 44
Jeonghoon 3:2a3664dc6634 45 #include <boost/array.hpp>
Jeonghoon 3:2a3664dc6634 46 #include <boost/call_traits.hpp>
Jeonghoon 3:2a3664dc6634 47 #include <boost/utility/enable_if.hpp>
Jeonghoon 3:2a3664dc6634 48 #include <boost/mpl/and.hpp>
Jeonghoon 3:2a3664dc6634 49 #include <boost/mpl/or.hpp>
Jeonghoon 3:2a3664dc6634 50 #include <boost/mpl/not.hpp>
Jeonghoon 3:2a3664dc6634 51
Jeonghoon 3:2a3664dc6634 52 #include <cstring>
Jeonghoon 3:2a3664dc6634 53
Jeonghoon 3:2a3664dc6634 54 #define ROS_NEW_SERIALIZATION_API 1
Jeonghoon 3:2a3664dc6634 55
Jeonghoon 3:2a3664dc6634 56 /**
Jeonghoon 3:2a3664dc6634 57 * \brief Declare your serializer to use an allInOne member instead of requiring 3 different serialization
Jeonghoon 3:2a3664dc6634 58 * functions.
Jeonghoon 3:2a3664dc6634 59 *
Jeonghoon 3:2a3664dc6634 60 * The allinone method has the form:
Jeonghoon 3:2a3664dc6634 61 \verbatim
Jeonghoon 3:2a3664dc6634 62 template<typename Stream, typename T>
Jeonghoon 3:2a3664dc6634 63 inline static void allInOne(Stream& stream, T t)
Jeonghoon 3:2a3664dc6634 64 {
Jeonghoon 3:2a3664dc6634 65 stream.next(t.a);
Jeonghoon 3:2a3664dc6634 66 stream.next(t.b);
Jeonghoon 3:2a3664dc6634 67 ...
Jeonghoon 3:2a3664dc6634 68 }
Jeonghoon 3:2a3664dc6634 69 \endverbatim
Jeonghoon 3:2a3664dc6634 70 *
Jeonghoon 3:2a3664dc6634 71 * The only guarantee given is that Stream::next(T) is defined.
Jeonghoon 3:2a3664dc6634 72 */
Jeonghoon 3:2a3664dc6634 73 #define ROS_DECLARE_ALLINONE_SERIALIZER \
Jeonghoon 3:2a3664dc6634 74 template<typename Stream, typename T> \
Jeonghoon 3:2a3664dc6634 75 inline static void write(Stream& stream, const T& t) \
Jeonghoon 3:2a3664dc6634 76 { \
Jeonghoon 3:2a3664dc6634 77 allInOne<Stream, const T&>(stream, t); \
Jeonghoon 3:2a3664dc6634 78 } \
Jeonghoon 3:2a3664dc6634 79 \
Jeonghoon 3:2a3664dc6634 80 template<typename Stream, typename T> \
Jeonghoon 3:2a3664dc6634 81 inline static void read(Stream& stream, T& t) \
Jeonghoon 3:2a3664dc6634 82 { \
Jeonghoon 3:2a3664dc6634 83 allInOne<Stream, T&>(stream, t); \
Jeonghoon 3:2a3664dc6634 84 } \
Jeonghoon 3:2a3664dc6634 85 \
Jeonghoon 3:2a3664dc6634 86 template<typename T> \
Jeonghoon 3:2a3664dc6634 87 inline static uint32_t serializedLength(const T& t) \
Jeonghoon 3:2a3664dc6634 88 { \
Jeonghoon 3:2a3664dc6634 89 LStream stream; \
Jeonghoon 3:2a3664dc6634 90 allInOne<LStream, const T&>(stream, t); \
Jeonghoon 3:2a3664dc6634 91 return stream.getLength(); \
Jeonghoon 3:2a3664dc6634 92 }
Jeonghoon 3:2a3664dc6634 93
Jeonghoon 3:2a3664dc6634 94 namespace ros
Jeonghoon 3:2a3664dc6634 95 {
Jeonghoon 3:2a3664dc6634 96 namespace serialization
Jeonghoon 3:2a3664dc6634 97 {
Jeonghoon 3:2a3664dc6634 98 namespace mt = message_traits;
Jeonghoon 3:2a3664dc6634 99 namespace mpl = boost::mpl;
Jeonghoon 3:2a3664dc6634 100
Jeonghoon 3:2a3664dc6634 101 class ROSCPP_SERIALIZATION_DECL StreamOverrunException : public ros::Exception
Jeonghoon 3:2a3664dc6634 102 {
Jeonghoon 3:2a3664dc6634 103 public:
Jeonghoon 3:2a3664dc6634 104 StreamOverrunException(const std::string& what)
Jeonghoon 3:2a3664dc6634 105 : Exception(what)
Jeonghoon 3:2a3664dc6634 106 {}
Jeonghoon 3:2a3664dc6634 107 };
Jeonghoon 3:2a3664dc6634 108
Jeonghoon 3:2a3664dc6634 109 ROSCPP_SERIALIZATION_DECL void throwStreamOverrun();
Jeonghoon 3:2a3664dc6634 110
Jeonghoon 3:2a3664dc6634 111 /**
Jeonghoon 3:2a3664dc6634 112 * \brief Templated serialization class. Default implementation provides backwards compatibility with
Jeonghoon 3:2a3664dc6634 113 * old message types.
Jeonghoon 3:2a3664dc6634 114 *
Jeonghoon 3:2a3664dc6634 115 * Specializing the Serializer class is the only thing you need to do to get the ROS serialization system
Jeonghoon 3:2a3664dc6634 116 * to work with a type.
Jeonghoon 3:2a3664dc6634 117 */
Jeonghoon 3:2a3664dc6634 118 template<typename T>
Jeonghoon 3:2a3664dc6634 119 struct Serializer
Jeonghoon 3:2a3664dc6634 120 {
Jeonghoon 3:2a3664dc6634 121 /**
Jeonghoon 3:2a3664dc6634 122 * \brief Write an object to the stream. Normally the stream passed in here will be a ros::serialization::OStream
Jeonghoon 3:2a3664dc6634 123 */
Jeonghoon 3:2a3664dc6634 124 template<typename Stream>
Jeonghoon 3:2a3664dc6634 125 inline static void write(Stream& stream, typename boost::call_traits<T>::param_type t)
Jeonghoon 3:2a3664dc6634 126 {
Jeonghoon 3:2a3664dc6634 127 t.serialize(stream.getData(), 0);
Jeonghoon 3:2a3664dc6634 128 }
Jeonghoon 3:2a3664dc6634 129
Jeonghoon 3:2a3664dc6634 130 /**
Jeonghoon 3:2a3664dc6634 131 * \brief Read an object from the stream. Normally the stream passed in here will be a ros::serialization::IStream
Jeonghoon 3:2a3664dc6634 132 */
Jeonghoon 3:2a3664dc6634 133 template<typename Stream>
Jeonghoon 3:2a3664dc6634 134 inline static void read(Stream& stream, typename boost::call_traits<T>::reference t)
Jeonghoon 3:2a3664dc6634 135 {
Jeonghoon 3:2a3664dc6634 136 t.deserialize(stream.getData());
Jeonghoon 3:2a3664dc6634 137 }
Jeonghoon 3:2a3664dc6634 138
Jeonghoon 3:2a3664dc6634 139 /**
Jeonghoon 3:2a3664dc6634 140 * \brief Determine the serialized length of an object.
Jeonghoon 3:2a3664dc6634 141 */
Jeonghoon 3:2a3664dc6634 142 inline static uint32_t serializedLength(typename boost::call_traits<T>::param_type t)
Jeonghoon 3:2a3664dc6634 143 {
Jeonghoon 3:2a3664dc6634 144 return t.serializationLength();
Jeonghoon 3:2a3664dc6634 145 }
Jeonghoon 3:2a3664dc6634 146 };
Jeonghoon 3:2a3664dc6634 147
Jeonghoon 3:2a3664dc6634 148 /**
Jeonghoon 3:2a3664dc6634 149 * \brief Serialize an object. Stream here should normally be a ros::serialization::OStream
Jeonghoon 3:2a3664dc6634 150 */
Jeonghoon 3:2a3664dc6634 151 template<typename T, typename Stream>
Jeonghoon 3:2a3664dc6634 152 inline void serialize(Stream& stream, const T& t)
Jeonghoon 3:2a3664dc6634 153 {
Jeonghoon 3:2a3664dc6634 154 Serializer<T>::write(stream, t);
Jeonghoon 3:2a3664dc6634 155 }
Jeonghoon 3:2a3664dc6634 156
Jeonghoon 3:2a3664dc6634 157 /**
Jeonghoon 3:2a3664dc6634 158 * \brief Deserialize an object. Stream here should normally be a ros::serialization::IStream
Jeonghoon 3:2a3664dc6634 159 */
Jeonghoon 3:2a3664dc6634 160 template<typename T, typename Stream>
Jeonghoon 3:2a3664dc6634 161 inline void deserialize(Stream& stream, T& t)
Jeonghoon 3:2a3664dc6634 162 {
Jeonghoon 3:2a3664dc6634 163 Serializer<T>::read(stream, t);
Jeonghoon 3:2a3664dc6634 164 }
Jeonghoon 3:2a3664dc6634 165
Jeonghoon 3:2a3664dc6634 166 /**
Jeonghoon 3:2a3664dc6634 167 * \brief Determine the serialized length of an object
Jeonghoon 3:2a3664dc6634 168 */
Jeonghoon 3:2a3664dc6634 169 template<typename T>
Jeonghoon 3:2a3664dc6634 170 inline uint32_t serializationLength(const T& t)
Jeonghoon 3:2a3664dc6634 171 {
Jeonghoon 3:2a3664dc6634 172 return Serializer<T>::serializedLength(t);
Jeonghoon 3:2a3664dc6634 173 }
Jeonghoon 3:2a3664dc6634 174
Jeonghoon 3:2a3664dc6634 175 #define ROS_CREATE_SIMPLE_SERIALIZER(Type) \
Jeonghoon 3:2a3664dc6634 176 template<> struct Serializer<Type> \
Jeonghoon 3:2a3664dc6634 177 { \
Jeonghoon 3:2a3664dc6634 178 template<typename Stream> inline static void write(Stream& stream, const Type v) \
Jeonghoon 3:2a3664dc6634 179 { \
Jeonghoon 3:2a3664dc6634 180 memcpy(stream.advance(sizeof(v)), &v, sizeof(v) ); \
Jeonghoon 3:2a3664dc6634 181 } \
Jeonghoon 3:2a3664dc6634 182 \
Jeonghoon 3:2a3664dc6634 183 template<typename Stream> inline static void read(Stream& stream, Type& v) \
Jeonghoon 3:2a3664dc6634 184 { \
Jeonghoon 3:2a3664dc6634 185 memcpy(&v, stream.advance(sizeof(v)), sizeof(v) ); \
Jeonghoon 3:2a3664dc6634 186 } \
Jeonghoon 3:2a3664dc6634 187 \
Jeonghoon 3:2a3664dc6634 188 inline static uint32_t serializedLength(const Type&) \
Jeonghoon 3:2a3664dc6634 189 { \
Jeonghoon 3:2a3664dc6634 190 return sizeof(Type); \
Jeonghoon 3:2a3664dc6634 191 } \
Jeonghoon 3:2a3664dc6634 192 };
Jeonghoon 3:2a3664dc6634 193
Jeonghoon 3:2a3664dc6634 194 ROS_CREATE_SIMPLE_SERIALIZER(uint8_t)
Jeonghoon 3:2a3664dc6634 195 ROS_CREATE_SIMPLE_SERIALIZER(int8_t)
Jeonghoon 3:2a3664dc6634 196 ROS_CREATE_SIMPLE_SERIALIZER(uint16_t)
Jeonghoon 3:2a3664dc6634 197 ROS_CREATE_SIMPLE_SERIALIZER(int16_t)
Jeonghoon 3:2a3664dc6634 198 ROS_CREATE_SIMPLE_SERIALIZER(uint32_t)
Jeonghoon 3:2a3664dc6634 199 ROS_CREATE_SIMPLE_SERIALIZER(int32_t)
Jeonghoon 3:2a3664dc6634 200 ROS_CREATE_SIMPLE_SERIALIZER(uint64_t)
Jeonghoon 3:2a3664dc6634 201 ROS_CREATE_SIMPLE_SERIALIZER(int64_t)
Jeonghoon 3:2a3664dc6634 202 ROS_CREATE_SIMPLE_SERIALIZER(float)
Jeonghoon 3:2a3664dc6634 203 ROS_CREATE_SIMPLE_SERIALIZER(double)
Jeonghoon 3:2a3664dc6634 204
Jeonghoon 3:2a3664dc6634 205 /**
Jeonghoon 3:2a3664dc6634 206 * \brief Serializer specialized for bool (serialized as uint8)
Jeonghoon 3:2a3664dc6634 207 */
Jeonghoon 3:2a3664dc6634 208 template<> struct Serializer<bool>
Jeonghoon 3:2a3664dc6634 209 {
Jeonghoon 3:2a3664dc6634 210 template<typename Stream> inline static void write(Stream& stream, const bool v)
Jeonghoon 3:2a3664dc6634 211 {
Jeonghoon 3:2a3664dc6634 212 uint8_t b = (uint8_t)v;
Jeonghoon 3:2a3664dc6634 213 memcpy(stream.advance(1), &b, 1 );
Jeonghoon 3:2a3664dc6634 214 }
Jeonghoon 3:2a3664dc6634 215
Jeonghoon 3:2a3664dc6634 216 template<typename Stream> inline static void read(Stream& stream, bool& v)
Jeonghoon 3:2a3664dc6634 217 {
Jeonghoon 3:2a3664dc6634 218 uint8_t b;
Jeonghoon 3:2a3664dc6634 219 memcpy(&b, stream.advance(1), 1 );
Jeonghoon 3:2a3664dc6634 220 v = (bool)b;
Jeonghoon 3:2a3664dc6634 221 }
Jeonghoon 3:2a3664dc6634 222
Jeonghoon 3:2a3664dc6634 223 inline static uint32_t serializedLength(bool)
Jeonghoon 3:2a3664dc6634 224 {
Jeonghoon 3:2a3664dc6634 225 return 1;
Jeonghoon 3:2a3664dc6634 226 }
Jeonghoon 3:2a3664dc6634 227 };
Jeonghoon 3:2a3664dc6634 228
Jeonghoon 3:2a3664dc6634 229 /**
Jeonghoon 3:2a3664dc6634 230 * \brief Serializer specialized for std::string
Jeonghoon 3:2a3664dc6634 231 */
Jeonghoon 3:2a3664dc6634 232 template<class ContainerAllocator>
Jeonghoon 3:2a3664dc6634 233 struct Serializer<std::basic_string<char, std::char_traits<char>, ContainerAllocator> >
Jeonghoon 3:2a3664dc6634 234 {
Jeonghoon 3:2a3664dc6634 235 typedef std::basic_string<char, std::char_traits<char>, ContainerAllocator> StringType;
Jeonghoon 3:2a3664dc6634 236
Jeonghoon 3:2a3664dc6634 237 template<typename Stream>
Jeonghoon 3:2a3664dc6634 238 inline static void write(Stream& stream, const StringType& str)
Jeonghoon 3:2a3664dc6634 239 {
Jeonghoon 3:2a3664dc6634 240 size_t len = str.size();
Jeonghoon 3:2a3664dc6634 241 stream.next((uint32_t)len);
Jeonghoon 3:2a3664dc6634 242
Jeonghoon 3:2a3664dc6634 243 if (len > 0)
Jeonghoon 3:2a3664dc6634 244 {
Jeonghoon 3:2a3664dc6634 245 memcpy(stream.advance((uint32_t)len), str.data(), len);
Jeonghoon 3:2a3664dc6634 246 }
Jeonghoon 3:2a3664dc6634 247 }
Jeonghoon 3:2a3664dc6634 248
Jeonghoon 3:2a3664dc6634 249 template<typename Stream>
Jeonghoon 3:2a3664dc6634 250 inline static void read(Stream& stream, StringType& str)
Jeonghoon 3:2a3664dc6634 251 {
Jeonghoon 3:2a3664dc6634 252 uint32_t len;
Jeonghoon 3:2a3664dc6634 253 stream.next(len);
Jeonghoon 3:2a3664dc6634 254 if (len > 0)
Jeonghoon 3:2a3664dc6634 255 {
Jeonghoon 3:2a3664dc6634 256 str = StringType((char*)stream.advance(len), len);
Jeonghoon 3:2a3664dc6634 257 }
Jeonghoon 3:2a3664dc6634 258 else
Jeonghoon 3:2a3664dc6634 259 {
Jeonghoon 3:2a3664dc6634 260 str.clear();
Jeonghoon 3:2a3664dc6634 261 }
Jeonghoon 3:2a3664dc6634 262 }
Jeonghoon 3:2a3664dc6634 263
Jeonghoon 3:2a3664dc6634 264 inline static uint32_t serializedLength(const StringType& str)
Jeonghoon 3:2a3664dc6634 265 {
Jeonghoon 3:2a3664dc6634 266 return 4 + (uint32_t)str.size();
Jeonghoon 3:2a3664dc6634 267 }
Jeonghoon 3:2a3664dc6634 268 };
Jeonghoon 3:2a3664dc6634 269
Jeonghoon 3:2a3664dc6634 270 /**
Jeonghoon 3:2a3664dc6634 271 * \brief Serializer specialized for ros::Time
Jeonghoon 3:2a3664dc6634 272 */
Jeonghoon 3:2a3664dc6634 273 template<>
Jeonghoon 3:2a3664dc6634 274 struct Serializer<ros::Time>
Jeonghoon 3:2a3664dc6634 275 {
Jeonghoon 3:2a3664dc6634 276 template<typename Stream>
Jeonghoon 3:2a3664dc6634 277 inline static void write(Stream& stream, const ros::Time& v)
Jeonghoon 3:2a3664dc6634 278 {
Jeonghoon 3:2a3664dc6634 279 stream.next(v.sec);
Jeonghoon 3:2a3664dc6634 280 stream.next(v.nsec);
Jeonghoon 3:2a3664dc6634 281 }
Jeonghoon 3:2a3664dc6634 282
Jeonghoon 3:2a3664dc6634 283 template<typename Stream>
Jeonghoon 3:2a3664dc6634 284 inline static void read(Stream& stream, ros::Time& v)
Jeonghoon 3:2a3664dc6634 285 {
Jeonghoon 3:2a3664dc6634 286 stream.next(v.sec);
Jeonghoon 3:2a3664dc6634 287 stream.next(v.nsec);
Jeonghoon 3:2a3664dc6634 288 }
Jeonghoon 3:2a3664dc6634 289
Jeonghoon 3:2a3664dc6634 290 inline static uint32_t serializedLength(const ros::Time&)
Jeonghoon 3:2a3664dc6634 291 {
Jeonghoon 3:2a3664dc6634 292 return 8;
Jeonghoon 3:2a3664dc6634 293 }
Jeonghoon 3:2a3664dc6634 294 };
Jeonghoon 3:2a3664dc6634 295
Jeonghoon 3:2a3664dc6634 296 /**
Jeonghoon 3:2a3664dc6634 297 * \brief Serializer specialized for ros::Duration
Jeonghoon 3:2a3664dc6634 298 */
Jeonghoon 3:2a3664dc6634 299 template<>
Jeonghoon 3:2a3664dc6634 300 struct Serializer<ros::Duration>
Jeonghoon 3:2a3664dc6634 301 {
Jeonghoon 3:2a3664dc6634 302 template<typename Stream>
Jeonghoon 3:2a3664dc6634 303 inline static void write(Stream& stream, const ros::Duration& v)
Jeonghoon 3:2a3664dc6634 304 {
Jeonghoon 3:2a3664dc6634 305 stream.next(v.sec);
Jeonghoon 3:2a3664dc6634 306 stream.next(v.nsec);
Jeonghoon 3:2a3664dc6634 307 }
Jeonghoon 3:2a3664dc6634 308
Jeonghoon 3:2a3664dc6634 309 template<typename Stream>
Jeonghoon 3:2a3664dc6634 310 inline static void read(Stream& stream, ros::Duration& v)
Jeonghoon 3:2a3664dc6634 311 {
Jeonghoon 3:2a3664dc6634 312 stream.next(v.sec);
Jeonghoon 3:2a3664dc6634 313 stream.next(v.nsec);
Jeonghoon 3:2a3664dc6634 314 }
Jeonghoon 3:2a3664dc6634 315
Jeonghoon 3:2a3664dc6634 316 inline static uint32_t serializedLength(const ros::Duration&)
Jeonghoon 3:2a3664dc6634 317 {
Jeonghoon 3:2a3664dc6634 318 return 8;
Jeonghoon 3:2a3664dc6634 319 }
Jeonghoon 3:2a3664dc6634 320 };
Jeonghoon 3:2a3664dc6634 321
Jeonghoon 3:2a3664dc6634 322 /**
Jeonghoon 3:2a3664dc6634 323 * \brief Vector serializer. Default implementation does nothing
Jeonghoon 3:2a3664dc6634 324 */
Jeonghoon 3:2a3664dc6634 325 template<typename T, class ContainerAllocator, class Enabled = void>
Jeonghoon 3:2a3664dc6634 326 struct VectorSerializer
Jeonghoon 3:2a3664dc6634 327 {};
Jeonghoon 3:2a3664dc6634 328
Jeonghoon 3:2a3664dc6634 329 /**
Jeonghoon 3:2a3664dc6634 330 * \brief Vector serializer, specialized for non-fixed-size, non-simple types
Jeonghoon 3:2a3664dc6634 331 */
Jeonghoon 3:2a3664dc6634 332 template<typename T, class ContainerAllocator>
Jeonghoon 3:2a3664dc6634 333 struct VectorSerializer<T, ContainerAllocator, typename boost::disable_if<mt::IsFixedSize<T> >::type >
Jeonghoon 3:2a3664dc6634 334 {
Jeonghoon 3:2a3664dc6634 335 typedef std::vector<T, typename ContainerAllocator::template rebind<T>::other> VecType;
Jeonghoon 3:2a3664dc6634 336 typedef typename VecType::iterator IteratorType;
Jeonghoon 3:2a3664dc6634 337 typedef typename VecType::const_iterator ConstIteratorType;
Jeonghoon 3:2a3664dc6634 338
Jeonghoon 3:2a3664dc6634 339 template<typename Stream>
Jeonghoon 3:2a3664dc6634 340 inline static void write(Stream& stream, const VecType& v)
Jeonghoon 3:2a3664dc6634 341 {
Jeonghoon 3:2a3664dc6634 342 stream.next((uint32_t)v.size());
Jeonghoon 3:2a3664dc6634 343 ConstIteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 344 ConstIteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 345 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 346 {
Jeonghoon 3:2a3664dc6634 347 stream.next(*it);
Jeonghoon 3:2a3664dc6634 348 }
Jeonghoon 3:2a3664dc6634 349 }
Jeonghoon 3:2a3664dc6634 350
Jeonghoon 3:2a3664dc6634 351 template<typename Stream>
Jeonghoon 3:2a3664dc6634 352 inline static void read(Stream& stream, VecType& v)
Jeonghoon 3:2a3664dc6634 353 {
Jeonghoon 3:2a3664dc6634 354 uint32_t len;
Jeonghoon 3:2a3664dc6634 355 stream.next(len);
Jeonghoon 3:2a3664dc6634 356 v.resize(len);
Jeonghoon 3:2a3664dc6634 357 IteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 358 IteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 359 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 360 {
Jeonghoon 3:2a3664dc6634 361 stream.next(*it);
Jeonghoon 3:2a3664dc6634 362 }
Jeonghoon 3:2a3664dc6634 363 }
Jeonghoon 3:2a3664dc6634 364
Jeonghoon 3:2a3664dc6634 365 inline static uint32_t serializedLength(const VecType& v)
Jeonghoon 3:2a3664dc6634 366 {
Jeonghoon 3:2a3664dc6634 367 uint32_t size = 4;
Jeonghoon 3:2a3664dc6634 368 ConstIteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 369 ConstIteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 370 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 371 {
Jeonghoon 3:2a3664dc6634 372 size += serializationLength(*it);
Jeonghoon 3:2a3664dc6634 373 }
Jeonghoon 3:2a3664dc6634 374
Jeonghoon 3:2a3664dc6634 375 return size;
Jeonghoon 3:2a3664dc6634 376 }
Jeonghoon 3:2a3664dc6634 377 };
Jeonghoon 3:2a3664dc6634 378
Jeonghoon 3:2a3664dc6634 379 /**
Jeonghoon 3:2a3664dc6634 380 * \brief Vector serializer, specialized for fixed-size simple types
Jeonghoon 3:2a3664dc6634 381 */
Jeonghoon 3:2a3664dc6634 382 template<typename T, class ContainerAllocator>
Jeonghoon 3:2a3664dc6634 383 struct VectorSerializer<T, ContainerAllocator, typename boost::enable_if<mt::IsSimple<T> >::type >
Jeonghoon 3:2a3664dc6634 384 {
Jeonghoon 3:2a3664dc6634 385 typedef std::vector<T, typename ContainerAllocator::template rebind<T>::other> VecType;
Jeonghoon 3:2a3664dc6634 386 typedef typename VecType::iterator IteratorType;
Jeonghoon 3:2a3664dc6634 387 typedef typename VecType::const_iterator ConstIteratorType;
Jeonghoon 3:2a3664dc6634 388
Jeonghoon 3:2a3664dc6634 389 template<typename Stream>
Jeonghoon 3:2a3664dc6634 390 inline static void write(Stream& stream, const VecType& v)
Jeonghoon 3:2a3664dc6634 391 {
Jeonghoon 3:2a3664dc6634 392 uint32_t len = (uint32_t)v.size();
Jeonghoon 3:2a3664dc6634 393 stream.next(len);
Jeonghoon 3:2a3664dc6634 394 if (!v.empty())
Jeonghoon 3:2a3664dc6634 395 {
Jeonghoon 3:2a3664dc6634 396 const uint32_t data_len = len * (uint32_t)sizeof(T);
Jeonghoon 3:2a3664dc6634 397 memcpy(stream.advance(data_len), &v.front(), data_len);
Jeonghoon 3:2a3664dc6634 398 }
Jeonghoon 3:2a3664dc6634 399 }
Jeonghoon 3:2a3664dc6634 400
Jeonghoon 3:2a3664dc6634 401 template<typename Stream>
Jeonghoon 3:2a3664dc6634 402 inline static void read(Stream& stream, VecType& v)
Jeonghoon 3:2a3664dc6634 403 {
Jeonghoon 3:2a3664dc6634 404 uint32_t len;
Jeonghoon 3:2a3664dc6634 405 stream.next(len);
Jeonghoon 3:2a3664dc6634 406 v.resize(len);
Jeonghoon 3:2a3664dc6634 407
Jeonghoon 3:2a3664dc6634 408 if (len > 0)
Jeonghoon 3:2a3664dc6634 409 {
Jeonghoon 3:2a3664dc6634 410 const uint32_t data_len = (uint32_t)sizeof(T) * len;
Jeonghoon 3:2a3664dc6634 411 memcpy(&v.front(), stream.advance(data_len), data_len);
Jeonghoon 3:2a3664dc6634 412 }
Jeonghoon 3:2a3664dc6634 413 }
Jeonghoon 3:2a3664dc6634 414
Jeonghoon 3:2a3664dc6634 415 inline static uint32_t serializedLength(const VecType& v)
Jeonghoon 3:2a3664dc6634 416 {
Jeonghoon 3:2a3664dc6634 417 return 4 + v.size() * (uint32_t)sizeof(T);
Jeonghoon 3:2a3664dc6634 418 }
Jeonghoon 3:2a3664dc6634 419 };
Jeonghoon 3:2a3664dc6634 420
Jeonghoon 3:2a3664dc6634 421 /**
Jeonghoon 3:2a3664dc6634 422 * \brief Vector serializer, specialized for fixed-size non-simple types
Jeonghoon 3:2a3664dc6634 423 */
Jeonghoon 3:2a3664dc6634 424 template<typename T, class ContainerAllocator>
Jeonghoon 3:2a3664dc6634 425 struct VectorSerializer<T, ContainerAllocator, typename boost::enable_if<mpl::and_<mt::IsFixedSize<T>, mpl::not_<mt::IsSimple<T> > > >::type >
Jeonghoon 3:2a3664dc6634 426 {
Jeonghoon 3:2a3664dc6634 427 typedef std::vector<T, typename ContainerAllocator::template rebind<T>::other> VecType;
Jeonghoon 3:2a3664dc6634 428 typedef typename VecType::iterator IteratorType;
Jeonghoon 3:2a3664dc6634 429 typedef typename VecType::const_iterator ConstIteratorType;
Jeonghoon 3:2a3664dc6634 430
Jeonghoon 3:2a3664dc6634 431 template<typename Stream>
Jeonghoon 3:2a3664dc6634 432 inline static void write(Stream& stream, const VecType& v)
Jeonghoon 3:2a3664dc6634 433 {
Jeonghoon 3:2a3664dc6634 434 stream.next((uint32_t)v.size());
Jeonghoon 3:2a3664dc6634 435 ConstIteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 436 ConstIteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 437 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 438 {
Jeonghoon 3:2a3664dc6634 439 stream.next(*it);
Jeonghoon 3:2a3664dc6634 440 }
Jeonghoon 3:2a3664dc6634 441 }
Jeonghoon 3:2a3664dc6634 442
Jeonghoon 3:2a3664dc6634 443 template<typename Stream>
Jeonghoon 3:2a3664dc6634 444 inline static void read(Stream& stream, VecType& v)
Jeonghoon 3:2a3664dc6634 445 {
Jeonghoon 3:2a3664dc6634 446 uint32_t len;
Jeonghoon 3:2a3664dc6634 447 stream.next(len);
Jeonghoon 3:2a3664dc6634 448 v.resize(len);
Jeonghoon 3:2a3664dc6634 449 IteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 450 IteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 451 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 452 {
Jeonghoon 3:2a3664dc6634 453 stream.next(*it);
Jeonghoon 3:2a3664dc6634 454 }
Jeonghoon 3:2a3664dc6634 455 }
Jeonghoon 3:2a3664dc6634 456
Jeonghoon 3:2a3664dc6634 457 inline static uint32_t serializedLength(const VecType& v)
Jeonghoon 3:2a3664dc6634 458 {
Jeonghoon 3:2a3664dc6634 459 uint32_t size = 4;
Jeonghoon 3:2a3664dc6634 460 if (!v.empty())
Jeonghoon 3:2a3664dc6634 461 {
Jeonghoon 3:2a3664dc6634 462 uint32_t len_each = serializationLength(v.front());
Jeonghoon 3:2a3664dc6634 463 size += len_each * (uint32_t)v.size();
Jeonghoon 3:2a3664dc6634 464 }
Jeonghoon 3:2a3664dc6634 465
Jeonghoon 3:2a3664dc6634 466 return size;
Jeonghoon 3:2a3664dc6634 467 }
Jeonghoon 3:2a3664dc6634 468 };
Jeonghoon 3:2a3664dc6634 469
Jeonghoon 3:2a3664dc6634 470 /**
Jeonghoon 3:2a3664dc6634 471 * \brief serialize version for std::vector
Jeonghoon 3:2a3664dc6634 472 */
Jeonghoon 3:2a3664dc6634 473 template<typename T, class ContainerAllocator, typename Stream>
Jeonghoon 3:2a3664dc6634 474 inline void serialize(Stream& stream, const std::vector<T, ContainerAllocator>& t)
Jeonghoon 3:2a3664dc6634 475 {
Jeonghoon 3:2a3664dc6634 476 VectorSerializer<T, ContainerAllocator>::write(stream, t);
Jeonghoon 3:2a3664dc6634 477 }
Jeonghoon 3:2a3664dc6634 478
Jeonghoon 3:2a3664dc6634 479 /**
Jeonghoon 3:2a3664dc6634 480 * \brief deserialize version for std::vector
Jeonghoon 3:2a3664dc6634 481 */
Jeonghoon 3:2a3664dc6634 482 template<typename T, class ContainerAllocator, typename Stream>
Jeonghoon 3:2a3664dc6634 483 inline void deserialize(Stream& stream, std::vector<T, ContainerAllocator>& t)
Jeonghoon 3:2a3664dc6634 484 {
Jeonghoon 3:2a3664dc6634 485 VectorSerializer<T, ContainerAllocator>::read(stream, t);
Jeonghoon 3:2a3664dc6634 486 }
Jeonghoon 3:2a3664dc6634 487
Jeonghoon 3:2a3664dc6634 488 /**
Jeonghoon 3:2a3664dc6634 489 * \brief serializationLength version for std::vector
Jeonghoon 3:2a3664dc6634 490 */
Jeonghoon 3:2a3664dc6634 491 template<typename T, class ContainerAllocator>
Jeonghoon 3:2a3664dc6634 492 inline uint32_t serializationLength(const std::vector<T, ContainerAllocator>& t)
Jeonghoon 3:2a3664dc6634 493 {
Jeonghoon 3:2a3664dc6634 494 return VectorSerializer<T, ContainerAllocator>::serializedLength(t);
Jeonghoon 3:2a3664dc6634 495 }
Jeonghoon 3:2a3664dc6634 496
Jeonghoon 3:2a3664dc6634 497 /**
Jeonghoon 3:2a3664dc6634 498 * \brief Array serializer, default implementation does nothing
Jeonghoon 3:2a3664dc6634 499 */
Jeonghoon 3:2a3664dc6634 500 template<typename T, size_t N, class Enabled = void>
Jeonghoon 3:2a3664dc6634 501 struct ArraySerializer
Jeonghoon 3:2a3664dc6634 502 {};
Jeonghoon 3:2a3664dc6634 503
Jeonghoon 3:2a3664dc6634 504 /**
Jeonghoon 3:2a3664dc6634 505 * \brief Array serializer, specialized for non-fixed-size, non-simple types
Jeonghoon 3:2a3664dc6634 506 */
Jeonghoon 3:2a3664dc6634 507 template<typename T, size_t N>
Jeonghoon 3:2a3664dc6634 508 struct ArraySerializer<T, N, typename boost::disable_if<mt::IsFixedSize<T> >::type>
Jeonghoon 3:2a3664dc6634 509 {
Jeonghoon 3:2a3664dc6634 510 typedef boost::array<T, N > ArrayType;
Jeonghoon 3:2a3664dc6634 511 typedef typename ArrayType::iterator IteratorType;
Jeonghoon 3:2a3664dc6634 512 typedef typename ArrayType::const_iterator ConstIteratorType;
Jeonghoon 3:2a3664dc6634 513
Jeonghoon 3:2a3664dc6634 514 template<typename Stream>
Jeonghoon 3:2a3664dc6634 515 inline static void write(Stream& stream, const ArrayType& v)
Jeonghoon 3:2a3664dc6634 516 {
Jeonghoon 3:2a3664dc6634 517 ConstIteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 518 ConstIteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 519 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 520 {
Jeonghoon 3:2a3664dc6634 521 stream.next(*it);
Jeonghoon 3:2a3664dc6634 522 }
Jeonghoon 3:2a3664dc6634 523 }
Jeonghoon 3:2a3664dc6634 524
Jeonghoon 3:2a3664dc6634 525 template<typename Stream>
Jeonghoon 3:2a3664dc6634 526 inline static void read(Stream& stream, ArrayType& v)
Jeonghoon 3:2a3664dc6634 527 {
Jeonghoon 3:2a3664dc6634 528 IteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 529 IteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 530 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 531 {
Jeonghoon 3:2a3664dc6634 532 stream.next(*it);
Jeonghoon 3:2a3664dc6634 533 }
Jeonghoon 3:2a3664dc6634 534 }
Jeonghoon 3:2a3664dc6634 535
Jeonghoon 3:2a3664dc6634 536 inline static uint32_t serializedLength(const ArrayType& v)
Jeonghoon 3:2a3664dc6634 537 {
Jeonghoon 3:2a3664dc6634 538 uint32_t size = 0;
Jeonghoon 3:2a3664dc6634 539 ConstIteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 540 ConstIteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 541 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 542 {
Jeonghoon 3:2a3664dc6634 543 size += serializationLength(*it);
Jeonghoon 3:2a3664dc6634 544 }
Jeonghoon 3:2a3664dc6634 545
Jeonghoon 3:2a3664dc6634 546 return size;
Jeonghoon 3:2a3664dc6634 547 }
Jeonghoon 3:2a3664dc6634 548 };
Jeonghoon 3:2a3664dc6634 549
Jeonghoon 3:2a3664dc6634 550 /**
Jeonghoon 3:2a3664dc6634 551 * \brief Array serializer, specialized for fixed-size, simple types
Jeonghoon 3:2a3664dc6634 552 */
Jeonghoon 3:2a3664dc6634 553 template<typename T, size_t N>
Jeonghoon 3:2a3664dc6634 554 struct ArraySerializer<T, N, typename boost::enable_if<mt::IsSimple<T> >::type>
Jeonghoon 3:2a3664dc6634 555 {
Jeonghoon 3:2a3664dc6634 556 typedef boost::array<T, N > ArrayType;
Jeonghoon 3:2a3664dc6634 557 typedef typename ArrayType::iterator IteratorType;
Jeonghoon 3:2a3664dc6634 558 typedef typename ArrayType::const_iterator ConstIteratorType;
Jeonghoon 3:2a3664dc6634 559
Jeonghoon 3:2a3664dc6634 560 template<typename Stream>
Jeonghoon 3:2a3664dc6634 561 inline static void write(Stream& stream, const ArrayType& v)
Jeonghoon 3:2a3664dc6634 562 {
Jeonghoon 3:2a3664dc6634 563 const uint32_t data_len = N * sizeof(T);
Jeonghoon 3:2a3664dc6634 564 memcpy(stream.advance(data_len), &v.front(), data_len);
Jeonghoon 3:2a3664dc6634 565 }
Jeonghoon 3:2a3664dc6634 566
Jeonghoon 3:2a3664dc6634 567 template<typename Stream>
Jeonghoon 3:2a3664dc6634 568 inline static void read(Stream& stream, ArrayType& v)
Jeonghoon 3:2a3664dc6634 569 {
Jeonghoon 3:2a3664dc6634 570 const uint32_t data_len = N * sizeof(T);
Jeonghoon 3:2a3664dc6634 571 memcpy(&v.front(), stream.advance(data_len), data_len);
Jeonghoon 3:2a3664dc6634 572 }
Jeonghoon 3:2a3664dc6634 573
Jeonghoon 3:2a3664dc6634 574 inline static uint32_t serializedLength(const ArrayType&)
Jeonghoon 3:2a3664dc6634 575 {
Jeonghoon 3:2a3664dc6634 576 return N * sizeof(T);
Jeonghoon 3:2a3664dc6634 577 }
Jeonghoon 3:2a3664dc6634 578 };
Jeonghoon 3:2a3664dc6634 579
Jeonghoon 3:2a3664dc6634 580 /**
Jeonghoon 3:2a3664dc6634 581 * \brief Array serializer, specialized for fixed-size, non-simple types
Jeonghoon 3:2a3664dc6634 582 */
Jeonghoon 3:2a3664dc6634 583 template<typename T, size_t N>
Jeonghoon 3:2a3664dc6634 584 struct ArraySerializer<T, N, typename boost::enable_if<mpl::and_<mt::IsFixedSize<T>, mpl::not_<mt::IsSimple<T> > > >::type>
Jeonghoon 3:2a3664dc6634 585 {
Jeonghoon 3:2a3664dc6634 586 typedef boost::array<T, N > ArrayType;
Jeonghoon 3:2a3664dc6634 587 typedef typename ArrayType::iterator IteratorType;
Jeonghoon 3:2a3664dc6634 588 typedef typename ArrayType::const_iterator ConstIteratorType;
Jeonghoon 3:2a3664dc6634 589
Jeonghoon 3:2a3664dc6634 590 template<typename Stream>
Jeonghoon 3:2a3664dc6634 591 inline static void write(Stream& stream, const ArrayType& v)
Jeonghoon 3:2a3664dc6634 592 {
Jeonghoon 3:2a3664dc6634 593 ConstIteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 594 ConstIteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 595 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 596 {
Jeonghoon 3:2a3664dc6634 597 stream.next(*it);
Jeonghoon 3:2a3664dc6634 598 }
Jeonghoon 3:2a3664dc6634 599 }
Jeonghoon 3:2a3664dc6634 600
Jeonghoon 3:2a3664dc6634 601 template<typename Stream>
Jeonghoon 3:2a3664dc6634 602 inline static void read(Stream& stream, ArrayType& v)
Jeonghoon 3:2a3664dc6634 603 {
Jeonghoon 3:2a3664dc6634 604 IteratorType it = v.begin();
Jeonghoon 3:2a3664dc6634 605 IteratorType end = v.end();
Jeonghoon 3:2a3664dc6634 606 for (; it != end; ++it)
Jeonghoon 3:2a3664dc6634 607 {
Jeonghoon 3:2a3664dc6634 608 stream.next(*it);
Jeonghoon 3:2a3664dc6634 609 }
Jeonghoon 3:2a3664dc6634 610 }
Jeonghoon 3:2a3664dc6634 611
Jeonghoon 3:2a3664dc6634 612 inline static uint32_t serializedLength(const ArrayType& v)
Jeonghoon 3:2a3664dc6634 613 {
Jeonghoon 3:2a3664dc6634 614 return serializationLength(v.front()) * N;
Jeonghoon 3:2a3664dc6634 615 }
Jeonghoon 3:2a3664dc6634 616 };
Jeonghoon 3:2a3664dc6634 617
Jeonghoon 3:2a3664dc6634 618 /**
Jeonghoon 3:2a3664dc6634 619 * \brief serialize version for boost::array
Jeonghoon 3:2a3664dc6634 620 */
Jeonghoon 3:2a3664dc6634 621 template<typename T, size_t N, typename Stream>
Jeonghoon 3:2a3664dc6634 622 inline void serialize(Stream& stream, const boost::array<T, N>& t)
Jeonghoon 3:2a3664dc6634 623 {
Jeonghoon 3:2a3664dc6634 624 ArraySerializer<T, N>::write(stream, t);
Jeonghoon 3:2a3664dc6634 625 }
Jeonghoon 3:2a3664dc6634 626
Jeonghoon 3:2a3664dc6634 627 /**
Jeonghoon 3:2a3664dc6634 628 * \brief deserialize version for boost::array
Jeonghoon 3:2a3664dc6634 629 */
Jeonghoon 3:2a3664dc6634 630 template<typename T, size_t N, typename Stream>
Jeonghoon 3:2a3664dc6634 631 inline void deserialize(Stream& stream, boost::array<T, N>& t)
Jeonghoon 3:2a3664dc6634 632 {
Jeonghoon 3:2a3664dc6634 633 ArraySerializer<T, N>::read(stream, t);
Jeonghoon 3:2a3664dc6634 634 }
Jeonghoon 3:2a3664dc6634 635
Jeonghoon 3:2a3664dc6634 636 /**
Jeonghoon 3:2a3664dc6634 637 * \brief serializationLength version for boost::array
Jeonghoon 3:2a3664dc6634 638 */
Jeonghoon 3:2a3664dc6634 639 template<typename T, size_t N>
Jeonghoon 3:2a3664dc6634 640 inline uint32_t serializationLength(const boost::array<T, N>& t)
Jeonghoon 3:2a3664dc6634 641 {
Jeonghoon 3:2a3664dc6634 642 return ArraySerializer<T, N>::serializedLength(t);
Jeonghoon 3:2a3664dc6634 643 }
Jeonghoon 3:2a3664dc6634 644
Jeonghoon 3:2a3664dc6634 645 /**
Jeonghoon 3:2a3664dc6634 646 * \brief Enum
Jeonghoon 3:2a3664dc6634 647 */
Jeonghoon 3:2a3664dc6634 648 namespace stream_types
Jeonghoon 3:2a3664dc6634 649 {
Jeonghoon 3:2a3664dc6634 650 enum StreamType
Jeonghoon 3:2a3664dc6634 651 {
Jeonghoon 3:2a3664dc6634 652 Input,
Jeonghoon 3:2a3664dc6634 653 Output,
Jeonghoon 3:2a3664dc6634 654 Length
Jeonghoon 3:2a3664dc6634 655 };
Jeonghoon 3:2a3664dc6634 656 }
Jeonghoon 3:2a3664dc6634 657 typedef stream_types::StreamType StreamType;
Jeonghoon 3:2a3664dc6634 658
Jeonghoon 3:2a3664dc6634 659 /**
Jeonghoon 3:2a3664dc6634 660 * \brief Stream base-class, provides common functionality for IStream and OStream
Jeonghoon 3:2a3664dc6634 661 */
Jeonghoon 3:2a3664dc6634 662 struct ROSCPP_SERIALIZATION_DECL Stream
Jeonghoon 3:2a3664dc6634 663 {
Jeonghoon 3:2a3664dc6634 664 /*
Jeonghoon 3:2a3664dc6634 665 * \brief Returns a pointer to the current position of the stream
Jeonghoon 3:2a3664dc6634 666 */
Jeonghoon 3:2a3664dc6634 667 inline uint8_t* getData() { return data_; }
Jeonghoon 3:2a3664dc6634 668 /**
Jeonghoon 3:2a3664dc6634 669 * \brief Advances the stream, checking bounds, and returns a pointer to the position before it
Jeonghoon 3:2a3664dc6634 670 * was advanced.
Jeonghoon 3:2a3664dc6634 671 * \throws StreamOverrunException if len would take this stream past the end of its buffer
Jeonghoon 3:2a3664dc6634 672 */
Jeonghoon 3:2a3664dc6634 673 ROS_FORCE_INLINE uint8_t* advance(uint32_t len)
Jeonghoon 3:2a3664dc6634 674 {
Jeonghoon 3:2a3664dc6634 675 uint8_t* old_data = data_;
Jeonghoon 3:2a3664dc6634 676 data_ += len;
Jeonghoon 3:2a3664dc6634 677 if (data_ > end_)
Jeonghoon 3:2a3664dc6634 678 {
Jeonghoon 3:2a3664dc6634 679 // Throwing directly here causes a significant speed hit due to the extra code generated
Jeonghoon 3:2a3664dc6634 680 // for the throw statement
Jeonghoon 3:2a3664dc6634 681 throwStreamOverrun();
Jeonghoon 3:2a3664dc6634 682 }
Jeonghoon 3:2a3664dc6634 683 return old_data;
Jeonghoon 3:2a3664dc6634 684 }
Jeonghoon 3:2a3664dc6634 685
Jeonghoon 3:2a3664dc6634 686 /**
Jeonghoon 3:2a3664dc6634 687 * \brief Returns the amount of space left in the stream
Jeonghoon 3:2a3664dc6634 688 */
Jeonghoon 3:2a3664dc6634 689 inline uint32_t getLength() { return (uint32_t)(end_ - data_); }
Jeonghoon 3:2a3664dc6634 690
Jeonghoon 3:2a3664dc6634 691 protected:
Jeonghoon 3:2a3664dc6634 692 Stream(uint8_t* _data, uint32_t _count)
Jeonghoon 3:2a3664dc6634 693 : data_(_data)
Jeonghoon 3:2a3664dc6634 694 , end_(_data + _count)
Jeonghoon 3:2a3664dc6634 695 {}
Jeonghoon 3:2a3664dc6634 696
Jeonghoon 3:2a3664dc6634 697 private:
Jeonghoon 3:2a3664dc6634 698 uint8_t* data_;
Jeonghoon 3:2a3664dc6634 699 uint8_t* end_;
Jeonghoon 3:2a3664dc6634 700 };
Jeonghoon 3:2a3664dc6634 701
Jeonghoon 3:2a3664dc6634 702 /**
Jeonghoon 3:2a3664dc6634 703 * \brief Input stream
Jeonghoon 3:2a3664dc6634 704 */
Jeonghoon 3:2a3664dc6634 705 struct ROSCPP_SERIALIZATION_DECL IStream : public Stream
Jeonghoon 3:2a3664dc6634 706 {
Jeonghoon 3:2a3664dc6634 707 static const StreamType stream_type = stream_types::Input;
Jeonghoon 3:2a3664dc6634 708
Jeonghoon 3:2a3664dc6634 709 IStream(uint8_t* data, uint32_t count)
Jeonghoon 3:2a3664dc6634 710 : Stream(data, count)
Jeonghoon 3:2a3664dc6634 711 {}
Jeonghoon 3:2a3664dc6634 712
Jeonghoon 3:2a3664dc6634 713 /**
Jeonghoon 3:2a3664dc6634 714 * \brief Deserialize an item from this input stream
Jeonghoon 3:2a3664dc6634 715 */
Jeonghoon 3:2a3664dc6634 716 template<typename T>
Jeonghoon 3:2a3664dc6634 717 ROS_FORCE_INLINE void next(T& t)
Jeonghoon 3:2a3664dc6634 718 {
Jeonghoon 3:2a3664dc6634 719 deserialize(*this, t);
Jeonghoon 3:2a3664dc6634 720 }
Jeonghoon 3:2a3664dc6634 721
Jeonghoon 3:2a3664dc6634 722 template<typename T>
Jeonghoon 3:2a3664dc6634 723 ROS_FORCE_INLINE IStream& operator>>(T& t)
Jeonghoon 3:2a3664dc6634 724 {
Jeonghoon 3:2a3664dc6634 725 deserialize(*this, t);
Jeonghoon 3:2a3664dc6634 726 return *this;
Jeonghoon 3:2a3664dc6634 727 }
Jeonghoon 3:2a3664dc6634 728 };
Jeonghoon 3:2a3664dc6634 729
Jeonghoon 3:2a3664dc6634 730 /**
Jeonghoon 3:2a3664dc6634 731 * \brief Output stream
Jeonghoon 3:2a3664dc6634 732 */
Jeonghoon 3:2a3664dc6634 733 struct ROSCPP_SERIALIZATION_DECL OStream : public Stream
Jeonghoon 3:2a3664dc6634 734 {
Jeonghoon 3:2a3664dc6634 735 static const StreamType stream_type = stream_types::Output;
Jeonghoon 3:2a3664dc6634 736
Jeonghoon 3:2a3664dc6634 737 OStream(uint8_t* data, uint32_t count)
Jeonghoon 3:2a3664dc6634 738 : Stream(data, count)
Jeonghoon 3:2a3664dc6634 739 {}
Jeonghoon 3:2a3664dc6634 740
Jeonghoon 3:2a3664dc6634 741 /**
Jeonghoon 3:2a3664dc6634 742 * \brief Serialize an item to this output stream
Jeonghoon 3:2a3664dc6634 743 */
Jeonghoon 3:2a3664dc6634 744 template<typename T>
Jeonghoon 3:2a3664dc6634 745 ROS_FORCE_INLINE void next(const T& t)
Jeonghoon 3:2a3664dc6634 746 {
Jeonghoon 3:2a3664dc6634 747 serialize(*this, t);
Jeonghoon 3:2a3664dc6634 748 }
Jeonghoon 3:2a3664dc6634 749
Jeonghoon 3:2a3664dc6634 750 template<typename T>
Jeonghoon 3:2a3664dc6634 751 ROS_FORCE_INLINE OStream& operator<<(const T& t)
Jeonghoon 3:2a3664dc6634 752 {
Jeonghoon 3:2a3664dc6634 753 serialize(*this, t);
Jeonghoon 3:2a3664dc6634 754 return *this;
Jeonghoon 3:2a3664dc6634 755 }
Jeonghoon 3:2a3664dc6634 756 };
Jeonghoon 3:2a3664dc6634 757
Jeonghoon 3:2a3664dc6634 758
Jeonghoon 3:2a3664dc6634 759 /**
Jeonghoon 3:2a3664dc6634 760 * \brief Length stream
Jeonghoon 3:2a3664dc6634 761 *
Jeonghoon 3:2a3664dc6634 762 * LStream is not what you would normally think of as a stream, but it is used in order to support
Jeonghoon 3:2a3664dc6634 763 * allinone serializers.
Jeonghoon 3:2a3664dc6634 764 */
Jeonghoon 3:2a3664dc6634 765 struct ROSCPP_SERIALIZATION_DECL LStream
Jeonghoon 3:2a3664dc6634 766 {
Jeonghoon 3:2a3664dc6634 767 static const StreamType stream_type = stream_types::Length;
Jeonghoon 3:2a3664dc6634 768
Jeonghoon 3:2a3664dc6634 769 LStream()
Jeonghoon 3:2a3664dc6634 770 : count_(0)
Jeonghoon 3:2a3664dc6634 771 {}
Jeonghoon 3:2a3664dc6634 772
Jeonghoon 3:2a3664dc6634 773 /**
Jeonghoon 3:2a3664dc6634 774 * \brief Add the length of an item to this length stream
Jeonghoon 3:2a3664dc6634 775 */
Jeonghoon 3:2a3664dc6634 776 template<typename T>
Jeonghoon 3:2a3664dc6634 777 ROS_FORCE_INLINE void next(const T& t)
Jeonghoon 3:2a3664dc6634 778 {
Jeonghoon 3:2a3664dc6634 779 count_ += serializationLength(t);
Jeonghoon 3:2a3664dc6634 780 }
Jeonghoon 3:2a3664dc6634 781
Jeonghoon 3:2a3664dc6634 782 /**
Jeonghoon 3:2a3664dc6634 783 * \brief increment the length by len
Jeonghoon 3:2a3664dc6634 784 */
Jeonghoon 3:2a3664dc6634 785 ROS_FORCE_INLINE uint32_t advance(uint32_t len)
Jeonghoon 3:2a3664dc6634 786 {
Jeonghoon 3:2a3664dc6634 787 uint32_t old = count_;
Jeonghoon 3:2a3664dc6634 788 count_ += len;
Jeonghoon 3:2a3664dc6634 789 return old;
Jeonghoon 3:2a3664dc6634 790 }
Jeonghoon 3:2a3664dc6634 791
Jeonghoon 3:2a3664dc6634 792 /**
Jeonghoon 3:2a3664dc6634 793 * \brief Get the total length of this tream
Jeonghoon 3:2a3664dc6634 794 */
Jeonghoon 3:2a3664dc6634 795 inline uint32_t getLength() { return count_; }
Jeonghoon 3:2a3664dc6634 796
Jeonghoon 3:2a3664dc6634 797 private:
Jeonghoon 3:2a3664dc6634 798 uint32_t count_;
Jeonghoon 3:2a3664dc6634 799 };
Jeonghoon 3:2a3664dc6634 800
Jeonghoon 3:2a3664dc6634 801 /**
Jeonghoon 3:2a3664dc6634 802 * \brief Serialize a message
Jeonghoon 3:2a3664dc6634 803 */
Jeonghoon 3:2a3664dc6634 804 template<typename M>
Jeonghoon 3:2a3664dc6634 805 inline SerializedMessage serializeMessage(const M& message)
Jeonghoon 3:2a3664dc6634 806 {
Jeonghoon 3:2a3664dc6634 807 SerializedMessage m;
Jeonghoon 3:2a3664dc6634 808 uint32_t len = serializationLength(message);
Jeonghoon 3:2a3664dc6634 809 m.num_bytes = len + 4;
Jeonghoon 3:2a3664dc6634 810 m.buf.reset(new uint8_t[m.num_bytes]);
Jeonghoon 3:2a3664dc6634 811
Jeonghoon 3:2a3664dc6634 812 OStream s(m.buf.get(), (uint32_t)m.num_bytes);
Jeonghoon 3:2a3664dc6634 813 serialize(s, (uint32_t)m.num_bytes - 4);
Jeonghoon 3:2a3664dc6634 814 m.message_start = s.getData();
Jeonghoon 3:2a3664dc6634 815 serialize(s, message);
Jeonghoon 3:2a3664dc6634 816
Jeonghoon 3:2a3664dc6634 817 return m;
Jeonghoon 3:2a3664dc6634 818 }
Jeonghoon 3:2a3664dc6634 819
Jeonghoon 3:2a3664dc6634 820 /**
Jeonghoon 3:2a3664dc6634 821 * \brief Serialize a service response
Jeonghoon 3:2a3664dc6634 822 */
Jeonghoon 3:2a3664dc6634 823 template<typename M>
Jeonghoon 3:2a3664dc6634 824 inline SerializedMessage serializeServiceResponse(bool ok, const M& message)
Jeonghoon 3:2a3664dc6634 825 {
Jeonghoon 3:2a3664dc6634 826 SerializedMessage m;
Jeonghoon 3:2a3664dc6634 827
Jeonghoon 3:2a3664dc6634 828 if (ok)
Jeonghoon 3:2a3664dc6634 829 {
Jeonghoon 3:2a3664dc6634 830 uint32_t len = serializationLength(message);
Jeonghoon 3:2a3664dc6634 831 m.num_bytes = len + 5;
Jeonghoon 3:2a3664dc6634 832 m.buf.reset(new uint8_t[m.num_bytes]);
Jeonghoon 3:2a3664dc6634 833
Jeonghoon 3:2a3664dc6634 834 OStream s(m.buf.get(), (uint32_t)m.num_bytes);
Jeonghoon 3:2a3664dc6634 835 serialize(s, (uint8_t)ok);
Jeonghoon 3:2a3664dc6634 836 serialize(s, (uint32_t)m.num_bytes - 5);
Jeonghoon 3:2a3664dc6634 837 serialize(s, message);
Jeonghoon 3:2a3664dc6634 838 }
Jeonghoon 3:2a3664dc6634 839 else
Jeonghoon 3:2a3664dc6634 840 {
Jeonghoon 3:2a3664dc6634 841 uint32_t len = serializationLength(message);
Jeonghoon 3:2a3664dc6634 842 m.num_bytes = len + 1;
Jeonghoon 3:2a3664dc6634 843 m.buf.reset(new uint8_t[m.num_bytes]);
Jeonghoon 3:2a3664dc6634 844
Jeonghoon 3:2a3664dc6634 845 OStream s(m.buf.get(), (uint32_t)m.num_bytes);
Jeonghoon 3:2a3664dc6634 846 serialize(s, (uint8_t)ok);
Jeonghoon 3:2a3664dc6634 847 serialize(s, message);
Jeonghoon 3:2a3664dc6634 848 }
Jeonghoon 3:2a3664dc6634 849
Jeonghoon 3:2a3664dc6634 850 return m;
Jeonghoon 3:2a3664dc6634 851 }
Jeonghoon 3:2a3664dc6634 852
Jeonghoon 3:2a3664dc6634 853 /**
Jeonghoon 3:2a3664dc6634 854 * \brief Deserialize a message. If includes_length is true, skips the first 4 bytes
Jeonghoon 3:2a3664dc6634 855 */
Jeonghoon 3:2a3664dc6634 856 template<typename M>
Jeonghoon 3:2a3664dc6634 857 inline void deserializeMessage(const SerializedMessage& m, M& message)
Jeonghoon 3:2a3664dc6634 858 {
Jeonghoon 3:2a3664dc6634 859 IStream s(m.message_start, m.num_bytes - (m.message_start - m.buf.get()));
Jeonghoon 3:2a3664dc6634 860 deserialize(s, message);
Jeonghoon 3:2a3664dc6634 861 }
Jeonghoon 3:2a3664dc6634 862
Jeonghoon 3:2a3664dc6634 863
Jeonghoon 3:2a3664dc6634 864 // Additional serialization traits
Jeonghoon 3:2a3664dc6634 865
Jeonghoon 3:2a3664dc6634 866 template<typename M>
Jeonghoon 3:2a3664dc6634 867 struct PreDeserializeParams
Jeonghoon 3:2a3664dc6634 868 {
Jeonghoon 3:2a3664dc6634 869 boost::shared_ptr<M> message;
Jeonghoon 3:2a3664dc6634 870 boost::shared_ptr<std::map<std::string, std::string> > connection_header;
Jeonghoon 3:2a3664dc6634 871 };
Jeonghoon 3:2a3664dc6634 872
Jeonghoon 3:2a3664dc6634 873 /**
Jeonghoon 3:2a3664dc6634 874 * \brief called by the SubscriptionCallbackHelper after a message is
Jeonghoon 3:2a3664dc6634 875 * instantiated but before that message is deserialized
Jeonghoon 3:2a3664dc6634 876 */
Jeonghoon 3:2a3664dc6634 877 template<typename M>
Jeonghoon 3:2a3664dc6634 878 struct PreDeserialize
Jeonghoon 3:2a3664dc6634 879 {
Jeonghoon 3:2a3664dc6634 880 static void notify(const PreDeserializeParams<M>&) { }
Jeonghoon 3:2a3664dc6634 881 };
Jeonghoon 3:2a3664dc6634 882
Jeonghoon 3:2a3664dc6634 883 } // namespace serialization
Jeonghoon 3:2a3664dc6634 884
Jeonghoon 3:2a3664dc6634 885 } // namespace ros
Jeonghoon 3:2a3664dc6634 886
Jeonghoon 3:2a3664dc6634 887 #endif // ROSCPP_SERIALIZATION_H