Class MessageBuffer

Class Documentation

class MessageBuffer

A class for storing and manipulating message data.

The MessageBuffer class provides operations to store and manipulate message data. It allows appending data to the buffer, extracting values from the buffer, and providing access to the underlying buffer data. It automatically handles converting between network byte order and native byte order.

Public Functions

MessageBuffer() = default

Creates a MessageBuffer object with an empty data buffer.

inline explicit MessageBuffer(std::vector<std::byte> data)

Creates a MessageBuffer object using the specified data.

Parameters:

data – The vector containing the data to be stored in the MessageBuffer.

inline std::size_t size() const

Returns the size of the MessageBuffer.

Returns:

The size of the MessageBuffer in bytes.

inline std::vector<std::byte> const &data() const &

Returns a reference to the underlying data vector.

Returns:

A const reference to the underlying data vector.

inline std::vector<std::byte> data() &&

Returns the data vector by moving it.

Returns:

The data vector of the MessageBuffer object.

template<std::integral... Ts>
inline auto try_extract()

Tries to extract values from the MessageBuffer.

This function tries to extract values from the MessageBuffer based on the specified types. Converting between network byte order and native byte order is handled automatically. If the size of the MessageBuffer is not sufficient to extract all the specified types, an empty optional is returned.

Template Parameters:

Ts – The types of the values to extract.

Returns:

An optional containing the extracted value (if only one), an optional containing an std::tuple or of the requested values, or an empty optional if extraction fails.

Friends

template<std::integral T>
inline friend MessageBuffer &operator<<(MessageBuffer &message_buffer, T const value)

Overloaded insertion operator for appending a value to a MessageBuffer.

This operator allows appending a value of type T to a MessageBuffer object. The value is first converted to network byte order, and then appended to the MessageBuffer’s internal data storage.

Parameters:
Returns:

A reference to the modified MessageBuffer object.

template<std::integral T>
inline friend MessageBuffer &&operator<<(MessageBuffer &&message_buffer, T const value)

Overloaded insertion operator for appending a value to a MessageBuffer.

This operator allows appending a value of type T to a MessageBuffer object. The value is first converted to network byte order, and then appended to the MessageBuffer’s internal data storage.

Parameters:
Returns:

A reference to the modified MessageBuffer object.

template<std::integral Integral>
inline friend MessageBuffer &operator>>(MessageBuffer &message_buffer, Integral &target)

Overloads the >> operator for extracting data from a MessageBuffer into an integral type.

This function extracts a value from a MessageBuffer and stores it in the provided integral type, after converting it from network byte order to native byte order. If the MessageBuffer does not contain enough data to extract the value, a std::runtime_error is thrown.

Template Parameters:

Integral – The integral type of the value to extract.

Parameters:
  • message_buffer – The MessageBuffer from which to extract the value.

  • target – The integral type variable in which to store the extracted value.

Throws:

std::runtime_error – if not enough data is available in the MessageBuffer.

Returns:

A reference to the original MessageBuffer object after extraction.

inline friend MessageBuffer &operator<<(MessageBuffer &message_buffer, std::span<std::byte const> const data)

Appends the provided data to the message buffer.

The operator<< function allows appending data to the message_buffer.

Parameters:
  • message_buffer – The message buffer to append data to.

  • data – The data to append to the message buffer.

Returns:

A reference to the message_buffer after the data has been appended.