File serialization.hpp

FileList > astutedds > rmw > serialization.hpp

Go to the source code of this file

CDR (XCDR1 little-endian) codec primitives, the mutually-recursive message encode/decode entry points, and the public serialize/deserialize wrappers. More...

  • #include <cstddef>
  • #include <cstdint>
  • #include <cstring>
  • #include <type_traits>
  • #include <vector>
  • #include <astutedds/rmw/type_support.hpp>

Classes

Type Name
struct CdrReader

Public Functions

Type Name
void cdr_byteswap_inplace (T & v)
bool cdr_decode_c_message (const MembersC * members, CdrReader & rdr, void * msg)
Decode a C-backend ROS 2 message from rdr intomsg .
bool cdr_decode_cpp_message (const MembersCpp * members, CdrReader & rdr, void * msg)
Decode a C++-backend ROS 2 message. See cdr_decode_c_message .
bool cdr_encode_c_message (const MembersC * members, const void * msg, std::vector< uint8_t > & buf)
bool cdr_encode_cpp_message (const MembersCpp * members, const void * msg, std::vector< uint8_t > & buf)
Encode a C++-backend ROS 2 message. See cdr_encode_c_message .
void cdr_pad (std::vector< uint8_t > & buf, std::size_t align)
void cdr_write_le (std::vector< uint8_t > & buf, T val)
bool deserialize_message (const TypeSupportHandle & ts, const uint8_t * data, size_t len, void * ros_message)
bool serialize_message (const TypeSupportHandle & ts, const void * ros_message, std::vector< uint8_t > & out)

Detailed Description

The trivial primitives (cdr_pad, cdr_write_le, CdrReader) are inline so any caller can use them cheaply. The four message-level entry points are defined in serialization.cpp; they are mutually recursive because a message field may itself be a nested message.

CDR alignment is measured from byte 0 of the encapsulation (four bytes before the payload begins). The writer helpers operate on a buffer that already contains the 4-byte header, so buf.size() % align yields the correct modulus directly. CdrReader::align mirrors this by adding 4 before taking the modulus.

Writing always emits little-endian XCDR1 (matching the ROS 2 CDR default). Reading accepts both little- and big-endian classic-CDR encapsulations (detected from the wire header in deserialize_message); PL_CDR (mutable/appendable XTypes extensibility) is not implemented.

Public Functions Documentation

function cdr_byteswap_inplace

template<typename T>
inline void cdr_byteswap_inplace (
    T & v
) 

Reverse the byte order of a trivially-copyable integer in place.

Portable (no compiler-specific builtins), since this header is shared by every distro/toolchain this RMW targets.


function cdr_decode_c_message

Decode a C-backend ROS 2 message from rdr intomsg .

bool cdr_decode_c_message (
    const MembersC * members,
    CdrReader & rdr,
    void * msg
) 


function cdr_decode_cpp_message

Decode a C++-backend ROS 2 message. See cdr_decode_c_message .

bool cdr_decode_cpp_message (
    const MembersCpp * members,
    CdrReader & rdr,
    void * msg
) 


function cdr_encode_c_message

bool cdr_encode_c_message (
    const MembersC * members,
    const void * msg,
    std::vector< uint8_t > & buf
) 

Encode a C-backend ROS 2 message into buf (excluding the encapsulation header, which the caller writes first).


function cdr_encode_cpp_message

Encode a C++-backend ROS 2 message. See cdr_encode_c_message .

bool cdr_encode_cpp_message (
    const MembersCpp * members,
    const void * msg,
    std::vector< uint8_t > & buf
) 


function cdr_pad

inline void cdr_pad (
    std::vector< uint8_t > & buf,
    std::size_t align
) 

Pad buf with zeros until its size is a multiple of align bytes.

Parameters:

  • buf Growing CDR buffer to pad in-place.
  • align Required alignment boundary (1, 2, 4, or 8 in practice).

function cdr_write_le

template<typename T>
inline void cdr_write_le (
    std::vector< uint8_t > & buf,
    T val
) 

Append a trivially-copyable value to buf in little-endian byte order.

Assumes a little-endian host (Linux x86-64 / aarch64 by default).

Template parameters:

  • T Trivially-copyable integer or enum type.

Parameters:

  • buf Destination buffer.
  • val Value to append.

function deserialize_message

bool deserialize_message (
    const TypeSupportHandle & ts,
    const uint8_t * data,
    size_t len,
    void * ros_message
) 

CDR-deserialize data/len bytes into a ROS 2 message struct.

The buffer must begin with a CDR encapsulation header (XTypes Table 22). Both little- and big-endian classic-CDR encapsulations are accepted; PL_CDR (mutable/appendable XTypes extensibility) is not.

Parameters:

  • ts Valid handle describing ros_message's layout.
  • data CDR-encoded bytes (including the 4-byte header).
  • len Byte length of data.
  • ros_message Zero-initialised ROS 2 message struct; filled in place.

Returns:

true on success; false on any decoder error.


function serialize_message

bool serialize_message (
    const TypeSupportHandle & ts,
    const void * ros_message,
    std::vector< uint8_t > & out
) 

CDR-serialize a ROS 2 message into out.

Prepends the 4-byte little-endian encapsulation header, then walks the introspection table to emit each field with correct CDR alignment. The result is suitable for astutedds_write().

Parameters:

  • ts Valid handle describing ros_message's layout.
  • ros_message Pointer to a filled-in ROS 2 message struct.
  • out Destination buffer; cleared before use.

Returns:

true on success; false on any encoder error.



The documentation for this class was generated from the following file include/astutedds/rmw/serialization.hpp