third-party/realsense-file/rosbag/roscpp_serialization/include/ros/serialization.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ROSCPP_SERIALIZATION_H
29 #define ROSCPP_SERIALIZATION_H
30 
32 
33 #include <ros/types.h>
34 #include <ros/time.h>
35 
36 #include "serialized_message.h"
37 #include "ros/message_traits.h"
39 #include "ros/exception.h"
40 #include "ros/datatypes.h"
41 
42 #include <vector>
43 #include <map>
44 
45 #include <boost/call_traits.hpp>
47 #include <boost/mpl/and.hpp>
48 #include <boost/mpl/or.hpp>
49 #include <boost/mpl/not.hpp>
50 
51 #include <cstring>
52 
53 #define ROS_NEW_SERIALIZATION_API 1
54 
72 #define ROS_DECLARE_ALLINONE_SERIALIZER \
73  template<typename Stream, typename T> \
74  inline static void write(Stream& stream, const T& t) \
75  { \
76  allInOne<Stream, const T&>(stream, t); \
77  } \
78  \
79  template<typename Stream, typename T> \
80  inline static void read(Stream& stream, T& t) \
81  { \
82  allInOne<Stream, T&>(stream, t); \
83  } \
84  \
85  template<typename T> \
86  inline static uint32_t serializedLength(const T& t) \
87  { \
88  LStream stream; \
89  allInOne<LStream, const T&>(stream, t); \
90  return stream.getLength(); \
91  }
92 
93 namespace rs2rosinternal
94 {
95 namespace serialization
96 {
97 namespace mt = message_traits;
98 namespace mpl = boost::mpl;
99 
101 {
102 public:
104  : Exception(what)
105  {}
106 };
107 
109 
117 template<typename T>
119 {
123  template<typename Stream>
124  inline static void write(Stream& stream, typename boost::call_traits<T>::param_type t)
125  {
126  t.serialize(stream.getData(), 0);
127  }
128 
132  template<typename Stream>
133  inline static void read(Stream& stream, typename boost::call_traits<T>::reference t)
134  {
135  t.deserialize(stream.getData());
136  }
137 
142  {
143  return t.serializationLength();
144  }
145 };
146 
150 template<typename T, typename Stream>
151 inline void serialize(Stream& stream, const T& t)
152 {
153  Serializer<T>::write(stream, t);
154 }
155 
159 template<typename T, typename Stream>
160 inline void deserialize(Stream& stream, T& t)
161 {
162  Serializer<T>::read(stream, t);
163 }
164 
168 template<typename T>
170 {
172 }
173 
174 #define ROS_CREATE_SIMPLE_SERIALIZER(Type) \
175  template<> struct Serializer<Type> \
176  { \
177  template<typename Stream> inline static void write(Stream& stream, const Type v) \
178  { \
179  *reinterpret_cast<Type*>(stream.advance(sizeof(v))) = v; \
180  } \
181  \
182  template<typename Stream> inline static void read(Stream& stream, Type& v) \
183  { \
184  v = *reinterpret_cast<Type*>(stream.advance(sizeof(v))); \
185  } \
186  \
187  inline static uint32_t serializedLength(const Type&) \
188  { \
189  return sizeof(Type); \
190  } \
191  };
192 
193 #define ROS_CREATE_SIMPLE_SERIALIZER_ARM(Type) \
194  template<> struct Serializer<Type> \
195  { \
196  template<typename Stream> inline static void write(Stream& stream, const Type v) \
197  { \
198  memcpy(stream.advance(sizeof(v)), &v, sizeof(v) ); \
199  } \
200  \
201  template<typename Stream> inline static void read(Stream& stream, Type& v) \
202  { \
203  memcpy(&v, stream.advance(sizeof(v)), sizeof(v) ); \
204  } \
205  \
206  inline static uint32_t serializedLength(const Type&) \
207  { \
208  return sizeof(Type); \
209  } \
210 };
211 
212 #if defined(__arm__) || defined(__arm)
223 #else
234 #endif
235 
239 template<> struct Serializer<bool>
240 {
241  template<typename Stream> inline static void write(Stream& stream, const bool v)
242  {
243  uint8_t b = (uint8_t)v;
244 #if defined(__arm__) || defined(__arm)
245  memcpy(stream.advance(1), &b, 1 );
246 #else
247  *reinterpret_cast<uint8_t*>(stream.advance(1)) = b;
248 #endif
249  }
250 
251  template<typename Stream> inline static void read(Stream& stream, bool& v)
252  {
253  uint8_t b;
254 #if defined(__arm__) || defined(__arm)
255  memcpy(&b, stream.advance(1), 1 );
256 #else
257  b = *reinterpret_cast<uint8_t*>(stream.advance(1));
258 #endif
259  v = (bool)b;
260  }
261 
262  inline static uint32_t serializedLength(bool)
263  {
264  return 1;
265  }
266 };
267 
271 template<class ContainerAllocator>
272 struct Serializer<std::basic_string<char, std::char_traits<char>, ContainerAllocator> >
273 {
274  typedef std::basic_string<char, std::char_traits<char>, ContainerAllocator> StringType;
275 
276  template<typename Stream>
277  inline static void write(Stream& stream, const StringType& str)
278  {
279  size_t len = str.size();
280  stream.next((uint32_t)len);
281 
282  if (len > 0)
283  {
284  memcpy(stream.advance((uint32_t)len), str.data(), len);
285  }
286  }
287 
288  template<typename Stream>
289  inline static void read(Stream& stream, StringType& str)
290  {
291  uint32_t len;
292  stream.next(len);
293  if (len > 0)
294  {
295  str = StringType((char*)stream.advance(len), len);
296  }
297  else
298  {
299  str.clear();
300  }
301  }
302 
303  inline static uint32_t serializedLength(const StringType& str)
304  {
305  return 4 + (uint32_t)str.size();
306  }
307 };
308 
312 template<>
314 {
315  template<typename Stream>
316  inline static void write(Stream& stream, const rs2rosinternal::Time& v)
317  {
318  stream.next(v.sec);
319  stream.next(v.nsec);
320  }
321 
322  template<typename Stream>
323  inline static void read(Stream& stream, rs2rosinternal::Time& v)
324  {
325  stream.next(v.sec);
326  stream.next(v.nsec);
327  }
328 
330  {
331  return 8;
332  }
333 };
334 
338 template<>
340 {
341  template<typename Stream>
342  inline static void write(Stream& stream, const rs2rosinternal::Duration& v)
343  {
344  stream.next(v.sec);
345  stream.next(v.nsec);
346  }
347 
348  template<typename Stream>
349  inline static void read(Stream& stream, rs2rosinternal::Duration& v)
350  {
351  stream.next(v.sec);
352  stream.next(v.nsec);
353  }
354 
356  {
357  return 8;
358  }
359 };
360 
364 template<typename T, class ContainerAllocator, class Enabled = void>
366 {};
367 
371 template<typename T, class ContainerAllocator>
372 struct VectorSerializer<T, ContainerAllocator, typename boost::disable_if<mt::IsFixedSize<T> >::type >
373 {
374  typedef std::vector<T, typename ContainerAllocator::template rebind<T>::other> VecType;
375  typedef typename VecType::iterator IteratorType;
376  typedef typename VecType::const_iterator ConstIteratorType;
377 
378  template<typename Stream>
379  inline static void write(Stream& stream, const VecType& v)
380  {
381  stream.next((uint32_t)v.size());
382  ConstIteratorType it = v.begin();
383  ConstIteratorType end = v.end();
384  for (; it != end; ++it)
385  {
386  stream.next(*it);
387  }
388  }
389 
390  template<typename Stream>
391  inline static void read(Stream& stream, VecType& v)
392  {
393  uint32_t len;
394  stream.next(len);
395  v.resize(len);
396  IteratorType it = v.begin();
397  IteratorType end = v.end();
398  for (; it != end; ++it)
399  {
400  stream.next(*it);
401  }
402  }
403 
404  inline static uint32_t serializedLength(const VecType& v)
405  {
406  uint32_t size = 4;
407  ConstIteratorType it = v.begin();
408  ConstIteratorType end = v.end();
409  for (; it != end; ++it)
410  {
411  size += serializationLength(*it);
412  }
413 
414  return size;
415  }
416 };
417 
421 template<typename T, class ContainerAllocator>
422 struct VectorSerializer<T, ContainerAllocator, typename boost::enable_if<mt::IsSimple<T> >::type >
423 {
424  typedef std::vector<T, typename ContainerAllocator::template rebind<T>::other> VecType;
425  typedef typename VecType::iterator IteratorType;
426  typedef typename VecType::const_iterator ConstIteratorType;
427 
428  template<typename Stream>
429  inline static void write(Stream& stream, const VecType& v)
430  {
431  uint32_t len = (uint32_t)v.size();
432  stream.next(len);
433  if (!v.empty())
434  {
435  const uint32_t data_len = len * (uint32_t)sizeof(T);
436  memcpy(stream.advance(data_len), &v.front(), data_len);
437  }
438  }
439 
440  template<typename Stream>
441  inline static void read(Stream& stream, VecType& v)
442  {
443  uint32_t len;
444  stream.next(len);
445  v.resize(len);
446 
447  if (len > 0)
448  {
449  const uint32_t data_len = (uint32_t)sizeof(T) * len;
450  memcpy(&v.front(), stream.advance(data_len), data_len);
451  }
452  }
453 
454  inline static uint32_t serializedLength(const VecType& v)
455  {
456  return 4 + (uint32_t)v.size() * (uint32_t)sizeof(T);
457  }
458 };
459 
463 template<typename T, class ContainerAllocator>
464 struct VectorSerializer<T, ContainerAllocator, typename boost::enable_if<mpl::and_<mt::IsFixedSize<T>, mpl::not_<mt::IsSimple<T> > > >::type >
465 {
466  typedef std::vector<T, typename ContainerAllocator::template rebind<T>::other> VecType;
467  typedef typename VecType::iterator IteratorType;
468  typedef typename VecType::const_iterator ConstIteratorType;
469 
470  template<typename Stream>
471  inline static void write(Stream& stream, const VecType& v)
472  {
473  stream.next((uint32_t)v.size());
474  ConstIteratorType it = v.begin();
475  ConstIteratorType end = v.end();
476  for (; it != end; ++it)
477  {
478  stream.next(*it);
479  }
480  }
481 
482  template<typename Stream>
483  inline static void read(Stream& stream, VecType& v)
484  {
485  uint32_t len;
486  stream.next(len);
487  v.resize(len);
488  IteratorType it = v.begin();
489  IteratorType end = v.end();
490  for (; it != end; ++it)
491  {
492  stream.next(*it);
493  }
494  }
495 
496  inline static uint32_t serializedLength(const VecType& v)
497  {
498  uint32_t size = 4;
499  if (!v.empty())
500  {
501  uint32_t len_each = serializationLength(v.front());
502  size += len_each * (uint32_t)v.size();
503  }
504 
505  return size;
506  }
507 };
508 
512 template<typename T, class ContainerAllocator, typename Stream>
513 inline void serialize(Stream& stream, const std::vector<T, ContainerAllocator>& t)
514 {
516 }
517 
521 template<typename T, class ContainerAllocator, typename Stream>
522 inline void deserialize(Stream& stream, std::vector<T, ContainerAllocator>& t)
523 {
525 }
526 
530 template<typename T, class ContainerAllocator>
531 inline uint32_t serializationLength(const std::vector<T, ContainerAllocator>& t)
532 {
534 }
535 
539 template<typename T, size_t N, class Enabled = void>
541 {};
542 
546 template<typename T, size_t N>
547 struct ArraySerializer<T, N, typename boost::disable_if<mt::IsFixedSize<T> >::type>
548 {
549  typedef std::array<T, N > ArrayType;
550  typedef typename ArrayType::iterator IteratorType;
551  typedef typename ArrayType::const_iterator ConstIteratorType;
552 
553  template<typename Stream>
554  inline static void write(Stream& stream, const ArrayType& v)
555  {
556  ConstIteratorType it = v.begin();
557  ConstIteratorType end = v.end();
558  for (; it != end; ++it)
559  {
560  stream.next(*it);
561  }
562  }
563 
564  template<typename Stream>
565  inline static void read(Stream& stream, ArrayType& v)
566  {
567  IteratorType it = v.begin();
568  IteratorType end = v.end();
569  for (; it != end; ++it)
570  {
571  stream.next(*it);
572  }
573  }
574 
575  inline static uint32_t serializedLength(const ArrayType& v)
576  {
577  uint32_t size = 0;
578  ConstIteratorType it = v.begin();
579  ConstIteratorType end = v.end();
580  for (; it != end; ++it)
581  {
582  size += serializationLength(*it);
583  }
584 
585  return size;
586  }
587 };
588 
592 template<typename T, size_t N>
593 struct ArraySerializer<T, N, typename boost::enable_if<mt::IsSimple<T> >::type>
594 {
595  typedef std::array<T, N > ArrayType;
596  typedef typename ArrayType::iterator IteratorType;
597  typedef typename ArrayType::const_iterator ConstIteratorType;
598 
599  template<typename Stream>
600  inline static void write(Stream& stream, const ArrayType& v)
601  {
602  const uint32_t data_len = N * sizeof(T);
603  memcpy(stream.advance(data_len), &v.front(), data_len);
604  }
605 
606  template<typename Stream>
607  inline static void read(Stream& stream, ArrayType& v)
608  {
609  const uint32_t data_len = N * sizeof(T);
610  memcpy(&v.front(), stream.advance(data_len), data_len);
611  }
612 
613  inline static uint32_t serializedLength(const ArrayType&)
614  {
615  return N * sizeof(T);
616  }
617 };
618 
622 template<typename T, size_t N>
623 struct ArraySerializer<T, N, typename boost::enable_if<mpl::and_<mt::IsFixedSize<T>, mpl::not_<mt::IsSimple<T> > > >::type>
624 {
625  typedef std::array<T, N > ArrayType;
626  typedef typename ArrayType::iterator IteratorType;
627  typedef typename ArrayType::const_iterator ConstIteratorType;
628 
629  template<typename Stream>
630  inline static void write(Stream& stream, const ArrayType& v)
631  {
632  ConstIteratorType it = v.begin();
633  ConstIteratorType end = v.end();
634  for (; it != end; ++it)
635  {
636  stream.next(*it);
637  }
638  }
639 
640  template<typename Stream>
641  inline static void read(Stream& stream, ArrayType& v)
642  {
643  IteratorType it = v.begin();
644  IteratorType end = v.end();
645  for (; it != end; ++it)
646  {
647  stream.next(*it);
648  }
649  }
650 
651  inline static uint32_t serializedLength(const ArrayType& v)
652  {
653  return serializationLength(v.front()) * N;
654  }
655 };
656 
660 template<typename T, size_t N, typename Stream>
661 inline void serialize(Stream& stream, const std::array<T, N>& t)
662 {
663  ArraySerializer<T, N>::write(stream, t);
664 }
665 
669 template<typename T, size_t N, typename Stream>
670 inline void deserialize(Stream& stream, std::array<T, N>& t)
671 {
672  ArraySerializer<T, N>::read(stream, t);
673 }
674 
678 template<typename T, size_t N>
679 inline uint32_t serializationLength(const std::array<T, N>& t)
680 {
682 }
683 
687 namespace stream_types
688 {
690 {
694 };
695 }
697 
702 {
703  /*
704  * \brief Returns a pointer to the current position of the stream
705  */
706  inline uint8_t* getData() { return data_; }
713  {
714  uint8_t* old_data = data_;
715  data_ += len;
716  if (data_ > end_)
717  {
718  // Throwing directly here causes a significant speed hit due to the extra code generated
719  // for the throw statement
721  }
722  return old_data;
723  }
724 
728  inline uint32_t getLength() { return (uint32_t)(end_ - data_); }
729 
730 protected:
731  Stream(uint8_t* _data, uint32_t _count)
732  : data_(_data)
733  , end_(_data + _count)
734  {}
735 
736 private:
739 };
740 
745 {
746  static const StreamType stream_type = stream_types::Input;
747 
749  : Stream(data, count)
750  {}
751 
755  template<typename T>
757  {
758  deserialize(*this, t);
759  }
760 
761  template<typename T>
763  {
764  deserialize(*this, t);
765  return *this;
766  }
767 };
768 
773 {
774  static const StreamType stream_type = stream_types::Output;
775 
777  : Stream(data, count)
778  {}
779 
783  template<typename T>
784  ROS_FORCE_INLINE void next(const T& t)
785  {
786  serialize(*this, t);
787  }
788 
789  template<typename T>
791  {
792  serialize(*this, t);
793  return *this;
794  }
795 };
796 
797 
805 {
806  static const StreamType stream_type = stream_types::Length;
807 
809  : count_(0)
810  {}
811 
815  template<typename T>
816  ROS_FORCE_INLINE void next(const T& t)
817  {
818  count_ += serializationLength(t);
819  }
820 
825  {
826  uint32_t old = count_;
827  count_ += len;
828  return old;
829  }
830 
834  inline uint32_t getLength() { return count_; }
835 
836 private:
838 };
839 
843 template<typename M>
845 {
847  uint32_t len = serializationLength(message);
848  m.num_bytes = len + 4;
849  m.buf.resize(m.num_bytes);
850 
851  OStream s(m.buf.data(), (uint32_t)m.num_bytes);
852  serialize(s, (uint32_t)m.num_bytes - 4);
853  m.message_start = s.getData();
854  serialize(s, message);
855 
856  return m;
857 }
858 
862 template<typename M>
864 {
866 
867  if (ok)
868  {
869  uint32_t len = serializationLength(message);
870  m.num_bytes = len + 5;
871  m.buf.resize(m.num_bytes);
872 
873  OStream s(m.buf.data(), (uint32_t)m.num_bytes);
874  serialize(s, (uint8_t)ok);
875  serialize(s, (uint32_t)m.num_bytes - 5);
876  serialize(s, message);
877  }
878  else
879  {
880  uint32_t len = serializationLength(message);
881  m.num_bytes = len + 1;
882  m.buf.resize(m.num_bytes);
883 
884  OStream s(m.buf.data(), (uint32_t)m.num_bytes);
885  serialize(s, (uint8_t)ok);
886  serialize(s, message);
887  }
888 
889  return m;
890 }
891 
895 template<typename M>
897 {
898  IStream s(m.message_start, m.num_bytes - (m.message_start - m.buf.data()));
899  deserialize(s, message);
900 }
901 
902 
903 // Additional serialization traits
904 
905 template<typename M>
907 {
908  std::shared_ptr<M> message;
909  std::shared_ptr<std::map<std::string, std::string> > connection_header;
910 };
911 
916 template<typename M>
918 {
919  static void notify(const PreDeserializeParams<M>&) { }
920 };
921 
922 } // namespace serialization
923 
924 } // namespace rs2rosinternal
925 
926 #endif // ROSCPP_SERIALIZATION_H
GLenum GLuint GLenum GLsizei const GLchar * message
ROS_FORCE_INLINE void next(T &t)
Deserialize an item from this input stream.
GLuint GLuint end
GLboolean GLboolean GLboolean b
void serialize(Stream &stream, const T &t)
Serialize an object. Stream here should normally be a rs2rosinternal::serialization::OStream.
called by the SubscriptionCallbackHelper after a message is instantiated but before that message is d...
BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE.
GLdouble s
SerializedMessage serializeServiceResponse(bool ok, const M &message)
Serialize a service response.
const GLfloat * m
Definition: glext.h:6814
boost::detail::ct_imp< T,::std::is_pointer< T >::value,::std::is_arithmetic< T >::value,::std::is_enum< T >::value >::param_type param_type
unsigned short uint16_t
Definition: stdint.h:79
GLsizei const GLchar *const * string
GLuint GLuint stream
Definition: glext.h:1790
unsigned char uint8_t
Definition: stdint.h:78
GLdouble t
GLenum GLsizei len
Definition: glext.h:3285
ROSCPP_SERIALIZATION_DECL void throwStreamOverrun()
Duration representation for use with the Time class.
Definition: duration.h:108
Stream base-class, provides common functionality for IStream and OStream.
void deserialize(Stream &stream, T &t)
Deserialize an object. Stream here should normally be a rs2rosinternal::serialization::IStream.
GLsizeiptr size
#define ROS_FORCE_INLINE
static uint32_t serializedLength(typename boost::call_traits< T >::param_type t)
Determine the serialized length of an object.
Base class for all exceptions thrown by ROS.
Definition: exception.h:39
unsigned int uint32_t
Definition: stdint.h:80
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 rs2rosinternal::serializa...
signed short int16_t
Definition: stdint.h:76
#define ROSCPP_SERIALIZATION_DECL
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:177
unsigned __int64 uint64_t
Definition: stdint.h:90
uint32_t serializationLength(const T &t)
Determine the serialized length of an object.
ROS_FORCE_INLINE void next(const T &t)
Add the length of an item to this length stream.
signed char int8_t
Definition: stdint.h:75
ROS_FORCE_INLINE void next(const T &t)
Serialize an item to this output stream.
static auto it
GLenum type
signed __int64 int64_t
Definition: stdint.h:89
GLint GLsizei count
M
Definition: rmse.py:42
Templated serialization class. Default implementation provides backwards compatibility with old messa...
signed int int32_t
Definition: stdint.h:77
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...
void deserializeMessage(const SerializedMessage &m, M &message)
Deserialize a message. If includes_length is true, skips the first 4 bytes.
GLdouble v
Definition: parser.hpp:150
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 rs2rosinternal::serializ...


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41