class mrpt::serialization::CMessage

A class that contain generic messages, that can be sent and received from a “CClientTCPSocket” object.

A message consists of a “header” (or type), and a “body” (or content). Apart from arbitrary data, specific methods are provided for easing the serialization of MRPT’s “CSerializable” objects. This class is also used for passing data to hardware interfaces (see mrpt::comms::CSerialPort)

See also:

CClientTCPSocket

#include <mrpt/serialization/CMessage.h>

class CMessage
{
public:
    //
fields

    uint32_t type;
    std::vector<uint8_t> content;

    //
methods

    void serializeObject(const CSerializable* obj);
    void deserializeIntoExistingObject(CSerializable* obj);
    void deserializeIntoNewObject(CSerializable::Ptr& obj);
    void setContentFromString(const std::string& str);
    void getContentAsString(std::string& str);

    template <class T>
    void setContentFromStruct(const T& data);

    template <class T>
    void getContentAsStruct(T& data) const;
};

Fields

uint32_t type

An identifier of the message type (only the least-sig byte is typically sent)

std::vector<uint8_t> content

The contents of the message (memory is automatically handled by the std::vector object)

Methods

void serializeObject(const CSerializable* obj)

A method for serializing a MRPT’s object into the content.

Any modification to data in “content” after this will corrupt the object serialization. Member “type” is unmodified in this method.

void deserializeIntoExistingObject(CSerializable* obj)

A method that parse the data in the message into an existing object.

Note that the class of the object must be known and must match the one of the serialized object. std::exception On corrupt data, unknown serialized objects, unknown serialized object version, non-matching classes,…

void deserializeIntoNewObject(CSerializable::Ptr& obj)

A method that parse the data in the message into a new object of (a priori) unknown class.

The pointer will contain on return a copy of the reconstructed object. Deleting this object when no longer required is the responsability of the user. Note that previous contents of the pointer will be ignored (it should be nullptr). std::exception On corrupt data, unknown serialized objects, unknown serialized object version,…

void setContentFromString(const std::string& str)

Sets the contents of the message from a string.

See also:

getContentAsString

void getContentAsString(std::string& str)

Gets the contents of the message as a string.

See also:

setContentFromString

template <class T>
void setContentFromStruct(const T& data)

Sets the contents of the message from an arbitary structure - This is intended for inter-thread comms only, the message will be not cross-platform.

See also:

getContentAsStruct

template <class T>
void getContentAsStruct(T& data) const

Gets the contents of the message as an arbitary structure - This is intended for inter-thread comms only, the message will be not cross-platform.

See also:

setContentFromStruct