Go to the documentation of this file.
28 #ifndef ROSCPP_SERIALIZATION_H
29 #define ROSCPP_SERIALIZATION_H
46 #include <boost/array.hpp>
47 #include <boost/call_traits.hpp>
48 #include <boost/utility/enable_if.hpp>
49 #include <boost/mpl/and.hpp>
50 #include <boost/mpl/or.hpp>
51 #include <boost/mpl/not.hpp>
55 #define ROS_NEW_SERIALIZATION_API 1
74 #define ROS_DECLARE_ALLINONE_SERIALIZER \
75 template<typename Stream, typename T> \
76 inline static void write(Stream& stream, const T& t) \
78 allInOne<Stream, const T&>(stream, t); \
81 template<typename Stream, typename T> \
82 inline static void read(Stream& stream, T& t) \
84 allInOne<Stream, T&>(stream, t); \
87 template<typename T> \
88 inline static uint32_t serializedLength(const T& t) \
91 allInOne<LStream, const T&>(stream, t); \
92 return stream.getLength(); \
97 namespace serialization
99 namespace mt = message_traits;
100 namespace mpl = boost::mpl;
125 template<
typename Stream>
126 inline static void write(
Stream& stream,
typename boost::call_traits<T>::param_type t)
128 t.serialize(stream.
getData(), 0);
134 template<
typename Stream>
135 inline static void read(
Stream& stream,
typename boost::call_traits<T>::reference t)
137 t.deserialize(stream.
getData());
145 return t.serializationLength();
152 template<
typename T,
typename Stream>
161 template<
typename T,
typename Stream>
168 #if defined(__aarch64__) && __GNUC__ == 9 && __GNUC_MINOR__ == 3
169 #define ROS_SERIALIZATION_GCC_9_3_DISABLE_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
171 #define ROS_SERIALIZATION_GCC_9_3_DISABLE_VECTORIZE
183 #define ROS_CREATE_SIMPLE_SERIALIZER(Type) \
184 template<> struct Serializer<Type> \
186 template<typename Stream> inline static void write(Stream& stream, const Type v) \
188 memcpy(stream.advance(sizeof(v)), &v, sizeof(v) ); \
191 template<typename Stream> inline static void read(Stream& stream, Type& v) \
193 memcpy(&v, stream.advance(sizeof(v)), sizeof(v) ); \
196 inline static uint32_t serializedLength(const Type&) \
198 return sizeof(Type); \
218 template<
typename Stream>
inline static void write(
Stream& stream,
const bool v)
220 uint8_t b =
static_cast<uint8_t
>(v);
221 memcpy(stream.
advance(1), &b, 1 );
224 template<
typename Stream>
inline static void read(
Stream& stream,
bool& v)
227 memcpy(&b, stream.
advance(1), 1 );
228 v =
static_cast<bool>(b);
240 template<
class ContainerAllocator>
241 struct Serializer<
std::basic_string<char, std::char_traits<char>, ContainerAllocator> >
243 typedef std::basic_string<char, std::char_traits<char>, ContainerAllocator>
StringType;
245 template<
typename Stream>
248 size_t len = str.size();
249 stream.next(
static_cast<uint32_t
>(len));
253 memcpy(stream.
advance(
static_cast<uint32_t
>(len)), str.data(), len);
257 template<
typename Stream>
274 return 4 +
static_cast<uint32_t
>(str.size());
284 template<
typename Stream>
291 template<
typename Stream>
310 template<
typename Stream>
317 template<
typename Stream>
333 template<
typename T,
class ContainerAllocator,
class Enabled =
void>
340 template<
typename T,
class ContainerAllocator>
343 typedef std::vector<T, typename std::allocator_traits<ContainerAllocator>::template rebind_alloc<T>>
VecType;
347 template<
typename Stream>
350 stream.next(
static_cast<uint32_t
>(v.size()));
353 for (; it != end; ++it)
359 template<
typename Stream>
367 for (; it != end; ++it)
378 for (; it != end; ++it)
390 template<
typename T,
class ContainerAllocator>
393 typedef std::vector<T, typename std::allocator_traits<ContainerAllocator>::template rebind_alloc<T>>
VecType;
397 template<
typename Stream>
400 uint32_t len =
static_cast<uint32_t
>(v.size());
404 const uint32_t data_len = len *
static_cast<uint32_t
>(
sizeof(T));
405 memcpy(stream.
advance(data_len), &v.front(), data_len);
409 template<
typename Stream>
418 const uint32_t data_len =
static_cast<uint32_t
>(
sizeof(T)) * len;
419 memcpy(
static_cast<void*
>(&v.front()), stream.
advance(data_len), data_len);
425 return 4 + v.size() *
static_cast<uint32_t
>(
sizeof(T));
432 template<
typename T,
class ContainerAllocator>
433 struct VectorSerializer<T, ContainerAllocator, typename
boost::enable_if<mpl::and_<mt::IsFixedSize<T>, mpl::not_<mt::IsSimple<T> > > >::type >
435 typedef std::vector<T, typename std::allocator_traits<ContainerAllocator>::template rebind_alloc<T>>
VecType;
439 template<
typename Stream>
442 stream.next(
static_cast<uint32_t
>(v.size()));
445 for (; it != end; ++it)
451 template<
typename Stream>
459 for (; it != end; ++it)
471 size += len_each *
static_cast<uint32_t
>(v.size());
481 template<
typename T,
class ContainerAllocator,
typename Stream>
490 template<
typename T,
class ContainerAllocator,
typename Stream>
499 template<
typename T,
class ContainerAllocator>
508 template<
typename T,
size_t N,
class Enabled =
void>
515 template<
typename T,
size_t N>
522 template<
typename Stream>
527 for (; it != end; ++it)
533 template<
typename Stream>
538 for (; it != end; ++it)
549 for (; it != end; ++it)
561 template<
typename T,
size_t N>
568 template<
typename Stream>
571 const uint32_t data_len = N *
sizeof(T);
572 memcpy(stream.
advance(data_len), &v.front(), data_len);
575 template<
typename Stream>
578 const uint32_t data_len = N *
sizeof(T);
579 memcpy(&v.front(), stream.
advance(data_len), data_len);
584 return N *
sizeof(T);
591 template<
typename T,
size_t N>
592 struct ArraySerializer<T, N, typename
boost::enable_if<mpl::and_<mt::IsFixedSize<T>, mpl::not_<mt::IsSimple<T> > > >::type>
598 template<
typename Stream>
603 for (; it != end; ++it)
609 template<
typename Stream>
614 for (; it != end; ++it)
629 template<
typename T,
size_t N,
typename Stream>
638 template<
typename T,
size_t N,
typename Stream>
647 template<
typename T,
size_t N>
656 namespace stream_types
683 uint8_t* old_data = data_;
697 inline uint32_t
getLength() {
return static_cast<uint32_t
>(end_ - data_); }
702 , end_(_data + _count)
794 uint32_t old = count_;
897 #endif // ROSCPP_SERIALIZATION_H
std::basic_string< char, std::char_traits< char >, ContainerAllocator > StringType
static uint32_t serializedLength(const ArrayType &v)
std::vector< T, typename std::allocator_traits< ContainerAllocator >::template rebind_alloc< T > > VecType
static void read(Stream &stream, StringType &str)
static uint32_t serializedLength(typename boost::call_traits< T >::param_type t)
Determine the serialized length of an object.
#define ROS_SERIALIZATION_GCC_9_3_DISABLE_VECTORIZE
static void write(Stream &stream, typename boost::call_traits< T >::param_type t)
Write an object to the stream. Normally the stream passed in here will be a ros::serialization::OStre...
VecType::iterator IteratorType
static void write(Stream &stream, const VecType &v)
ROS_FORCE_INLINE uint32_t advance(uint32_t len)
increment the length by len
SerializedMessage serializeServiceResponse(bool ok, const M &message)
Serialize a service response.
static void notify(const PreDeserializeParams< M > &)
static void write(Stream &stream, const ArrayType &v)
Vector serializer. Default implementation does nothing.
std::vector< T, typename std::allocator_traits< ContainerAllocator >::template rebind_alloc< T > > VecType
ArrayType::const_iterator ConstIteratorType
Stream base-class, provides common functionality for IStream and OStream.
uint32_t getLength()
Get the total length of this tream.
static uint32_t serializedLength(const ros::Duration &)
ROS_FORCE_INLINE uint8_t * advance(uint32_t len)
Advances the stream, checking bounds, and returns a pointer to the position before it was advanced.
ArrayType::iterator IteratorType
static uint32_t serializedLength(const VecType &v)
std::vector< T, typename std::allocator_traits< ContainerAllocator >::template rebind_alloc< T > > VecType
static void read(Stream &stream, VecType &v)
boost::array< T, N > ArrayType
static uint32_t serializedLength(const StringType &str)
#define ROSCPP_SERIALIZATION_DECL
static void write(Stream &stream, const ArrayType &v)
VecType::const_iterator ConstIteratorType
called by the SubscriptionCallbackHelper after a message is instantiated but before that message is d...
Stream(uint8_t *_data, uint32_t _count)
ArrayType::iterator IteratorType
static void read(Stream &stream, VecType &v)
VecType::const_iterator ConstIteratorType
VecType::const_iterator ConstIteratorType
static void write(Stream &stream, const StringType &str)
static void write(Stream &stream, const bool v)
stream_types::StreamType StreamType
Array serializer, default implementation does nothing.
ROS_FORCE_INLINE IStream & operator>>(T &t)
static void write(Stream &stream, const VecType &v)
VecType::iterator IteratorType
static uint32_t serializedLength(const VecType &v)
StreamOverrunException(const std::string &what)
boost::shared_ptr< M > message
static void read(Stream &stream, ros::Duration &v)
ROS_FORCE_INLINE void next(T &t)
Deserialize an item from this input stream.
boost::array< T, N > ArrayType
static void read(Stream &stream, bool &v)
OStream(uint8_t *data, uint32_t count)
ArrayType::const_iterator ConstIteratorType
static uint32_t serializedLength(const ArrayType &)
static void read(Stream &stream, typename boost::call_traits< T >::reference t)
Read an object from the stream. Normally the stream passed in here will be a ros::serialization::IStr...
void deserialize(Stream &stream, T &t)
Deserialize an object. Stream here should normally be a ros::serialization::IStream.
void serialize(Stream &stream, const T &t)
Serialize an object. Stream here should normally be a ros::serialization::OStream.
ROS_FORCE_INLINE void next(const T &t)
Serialize an item to this output stream.
static uint32_t serializedLength(const ros::Time &)
boost::shared_ptr< std::map< std::string, std::string > > connection_header
ROS_FORCE_INLINE void next(const T &t)
Add the length of an item to this length stream.
uint32_t getLength()
Returns the amount of space left in the stream.
static uint32_t serializedLength(bool)
static void write(Stream &stream, const ros::Duration &v)
uint32_t ROS_SERIALIZATION_GCC_9_3_DISABLE_VECTORIZE serializationLength(const T &t)
Determine the serialized length of an object.
ArrayType::const_iterator ConstIteratorType
static void read(Stream &stream, ArrayType &v)
boost::array< T, N > ArrayType
ArrayType::iterator IteratorType
#define ROS_CREATE_SIMPLE_SERIALIZER(Type)
SerializedMessage serializeMessage(const M &message)
Serialize a message.
static void write(Stream &stream, const VecType &v)
static void write(Stream &stream, const ros::Time &v)
static void read(Stream &stream, ArrayType &v)
IStream(uint8_t *data, uint32_t count)
static void read(Stream &stream, ros::Time &v)
ROSCPP_SERIALIZATION_DECL void throwStreamOverrun()
static void read(Stream &stream, ArrayType &v)
static uint32_t serializedLength(const VecType &v)
static uint32_t serializedLength(const ArrayType &v)
VecType::iterator IteratorType
ROS_FORCE_INLINE OStream & operator<<(const T &t)
void deserializeMessage(const SerializedMessage &m, M &message)
Deserialize a message. If includes_length is true, skips the first 4 bytes.
static void read(Stream &stream, VecType &v)
boost::shared_array< uint8_t > buf
static void write(Stream &stream, const ArrayType &v)
Templated serialization class. Default implementation provides backwards compatibility with old messa...