Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions include/fastcdr/Cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cstring>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -69,6 +70,20 @@ class Cdr
{
public:

/**
* @brief This structure represents the context of a CDR serialization.
*
* Can be used by external (de)serialization functions to customize the serialization of a type.
* The Cdr object can be instantiated with a shared pointer to a Context object, and a getter for the context is
* provided in the Cdr class so it can be accessed from the external (de)serialization functions.
*/
struct Cdr_DllAPI Context
{
// Default virtual destructor to allow proper cleanup of derived classes, and to make sure the class
// is polymorphic so it can be used with dynamic_pointer_cast.
virtual ~Context() = default;
};

/*!
* @brief This enumeration represents endianness types.
*/
Expand Down Expand Up @@ -167,6 +182,21 @@ class Cdr
const Endianness endianness = DEFAULT_ENDIAN,
const CdrVersion cdr_version = XCDRv2);

/*!
* @brief This constructor creates an eprosima::fastcdr::Cdr object that can serialize/deserialize
* the assigned buffer with a specific context.
* @param cdr_buffer A reference to the buffer that contains (or will contain) the CDR representation.
* @param context A shared pointer to the context that will be used for serialization/deserialization.
* @param endianness The initial endianness that will be used. The default value is the endianness of the system.
* @param cdr_version Represents the type of encoding algorithm that will be used for the encoding.
* The default value is CdrVersion::XCDRv2.
*/
Cdr_DllAPI Cdr(
FastBuffer& cdr_buffer,
const std::shared_ptr<Context>& context,
const Endianness endianness = DEFAULT_ENDIAN,
const CdrVersion cdr_version = XCDRv2);

/*!
* @brief This function reads the encapsulation of the CDR stream.
* If the CDR stream contains an encapsulation, then this function should be called before starting to deserialize.
Expand Down Expand Up @@ -235,6 +265,12 @@ class Cdr
*/
Cdr_DllAPI Endianness endianness() const;

/*!
* @brief This function returns the context used by the CDR type.
* @return The context.
*/
Cdr_DllAPI std::shared_ptr<Context> get_context() const;

/*!
* @brief This function skips a number of bytes in the CDR stream buffer.
* @param num_bytes The number of bytes that will be jumped.
Expand Down Expand Up @@ -3588,6 +3624,9 @@ class Cdr
//! Whether the encapsulation was serialized.
bool encapsulation_serialized_ {false};

//! Custom serialization context.
std::shared_ptr<Context> context_;


uint32_t get_long_lc(
SerializedMemberSizeForNextInt serialized_member_size);
Expand Down
19 changes: 18 additions & 1 deletion src/cpp/Cdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <cstring>
#include <limits>
#include <memory>

#include <fastcdr/Cdr.h>

Expand Down Expand Up @@ -134,6 +135,15 @@ Cdr::Cdr(
FastBuffer& cdr_buffer,
const Endianness endianness,
const CdrVersion cdr_version)
: Cdr(cdr_buffer, nullptr, endianness, cdr_version)
{
}

Cdr::Cdr(
FastBuffer& cdr_buffer,
const std::shared_ptr<Context>& context,
const Endianness endianness,
const CdrVersion cdr_version)
: cdr_buffer_(cdr_buffer)
, cdr_version_(cdr_version)
, endianness_(endianness)
Expand All @@ -142,6 +152,7 @@ Cdr::Cdr(
, origin_(cdr_buffer.begin())
, end_(cdr_buffer.end())
, initial_state_(*this)
, context_(context)
{
switch (cdr_version_)
{
Expand Down Expand Up @@ -263,7 +274,8 @@ Cdr& Cdr::read_encapsulation()
}
break;
default:
throw BadParamException("Unexpected encoding algorithm received in Cdr::read_encapsulation for DDS CDR");
throw BadParamException(
"Unexpected encoding algorithm received in Cdr::read_encapsulation for DDS CDR");
}
reset_callbacks();

Expand Down Expand Up @@ -417,6 +429,11 @@ Cdr::Endianness Cdr::endianness() const
return static_cast<Endianness>(endianness_);
}

std::shared_ptr<Cdr::Context> Cdr::get_context() const
{
return context_;
}

bool Cdr::jump(
size_t num_bytes)
{
Expand Down
3 changes: 3 additions & 0 deletions versions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# v2.4.0

* Add context to `Cdr` class to allow for customization of external (de)serialization methods.

# v2.3.0

* Fix symbol visibility for exception classes

`Exception` changed to be a base class without inheriting from `std::exception`.
Expand Down
Loading