Skip to content
Merged
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
356 changes: 356 additions & 0 deletions include/fastcdr/Cdr.h
Comment thread
Danipiza marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,86 @@ class Cdr
return *this;
}

/*!
* @brief Decodes an optional member of a type according to the encoding algorithm used, applying the
* try-construct policy to its value.
* @param[out] member_value A reference of the variable where the optional member value will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when @p consistency_flag is
* @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize_member(
optional<_T>& member_value,
CdrTryConstructFlag consistency_flag)
{
return deserialize_member_try_construct(member_value, consistency_flag);
}

/*!
* @brief Decodes an optional member of a type according to the encoding algorithm used, applying the
* try-construct policy to its value.
* @param[out] member_value A reference of the variable where the optional member value will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @param[in] max_length Maximum number of characters or elements of the value.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when @p consistency_flag is
* @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize_member(
optional<_T>& member_value,
CdrTryConstructFlag consistency_flag,
uint32_t max_length)
{
return deserialize_member_try_construct(member_value, consistency_flag, max_length);
}

/*!
* @brief Decodes an optional member of an external according to the encoding algorithm used, applying
* the try-construct policy to its value.
* @param[out] member_value A reference of the variable where the optional member value will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked or when
* @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize_member(
optional<external<_T>>& member_value,
CdrTryConstructFlag consistency_flag)
{
return deserialize_member_try_construct(member_value, consistency_flag);
}

/*!
* @brief Decodes an optional member of an external according to the encoding algorithm used, applying
* the try-construct policy to its value.
* @param[out] member_value A reference of the variable where the optional member value will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @param[in] max_length Maximum number of characters or elements of the value.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked or when
* @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize_member(
optional<external<_T>>& member_value,
CdrTryConstructFlag consistency_flag,
uint32_t max_length)
{
return deserialize_member_try_construct(member_value, consistency_flag, max_length);
}

/*!
* @brief Tells to the encoder a new type and its members starts to be encoded.
* @param[in,out] current_state State of the encoder previous of calling this function.
Expand Down Expand Up @@ -3818,6 +3898,122 @@ class Cdr
return *this;
}

/*!
* @brief Decodes an optional from the buffer, applying the try-construct policy to its value.
* @param[out] value A reference to the variable where the optional will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when @p consistency_flag is
* @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize(
optional<_T>& value,
CdrTryConstructFlag consistency_flag)
{
return deserialize_try_construct(value, consistency_flag);
}

/*!
* @brief Decodes an optional from the buffer, applying the try-construct policy to its value.
* @param[out] value A reference to the variable where the optional will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @param[in] max_length Maximum number of characters or elements of the value.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when @p consistency_flag is
* @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize(
optional<_T>& value,
CdrTryConstructFlag consistency_flag,
uint32_t max_length)
{
return deserialize_try_construct(value, consistency_flag, max_length);
}

/*!
* @brief Decodes an external from the buffer, applying the try-construct policy to its value.
* @param[out] value A reference to the variable where the external will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked or when
* @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize(
external<_T>& value,
CdrTryConstructFlag consistency_flag)
{
return deserialize_try_construct(value, consistency_flag);
}

/*!
* @brief Decodes an external from the buffer, applying the try-construct policy to its value.
* @param[out] value A reference to the variable where the external will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @param[in] max_length Maximum number of characters or elements of the value.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked or when
* @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize(
external<_T>& value,
CdrTryConstructFlag consistency_flag,
uint32_t max_length)
{
return deserialize_try_construct(value, consistency_flag, max_length);
}

/*!
* @brief Decodes an optional of an external from the buffer, applying the try-construct policy to its
* value.
* @param[out] value A reference to the variable where the optional will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked or when
* @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize(
optional<external<_T>>& value,
CdrTryConstructFlag consistency_flag)
{
return deserialize_try_construct(value, consistency_flag);
}

/*!
* @brief Decodes an optional of an external from the buffer, applying the try-construct policy to its
* value.
* @param[out] value A reference to the variable where the optional will be stored.
* @param[in] consistency_flag Try-construct policy applied when the value does not fit.
* @param[in] max_length Maximum number of characters or elements of the value.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked or when
* @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& deserialize(
optional<external<_T>>& value,
CdrTryConstructFlag consistency_flag,
uint32_t max_length)
{
return deserialize_try_construct(value, consistency_flag, max_length);
}

/*!
* @brief Encodes an empty DHEADER if the encoding version is XCDRv2.
* After serializing the members's type, @ref set_xcdrv2_dheader must be called to set the correct DHEADER value
Expand All @@ -3836,6 +4032,166 @@ class Cdr

private:

/*!
* @brief Decodes an optional member of a type according to the encoding algorithm used, applying the
* try-construct policy to its value.
* Implementation shared by the public overloads taking a @ref CdrTryConstructFlag .
*/
template<class _T, class ... _Args>
Cdr& deserialize_member_try_construct(
optional<_T>& member_value,
CdrTryConstructFlag consistency_flag,
_Args&&... args)
{
if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)
{
Cdr::state current_state(*this);
MemberId member_id;
xcdr1_deserialize_member_header(member_id, current_state);
auto prev_offset = offset_;
member_value.reset(0 < current_state.member_size_);
if (0 < current_state.member_size_)
{
deserialize(member_value, consistency_flag, std::forward<_Args>(args)...);
}
size_t member_size {current_state.member_size_};
size_t diff {offset_ - prev_offset};
if (member_size < diff)
{
throw exception::BadParamException(
"Member size provided by member header is lower than real decoded member size");
}

// Skip unused bytes
offset_ += (member_size - diff);
}
else
{
deserialize(member_value, consistency_flag, std::forward<_Args>(args)...);
}
return *this;
}

/*!
* @brief Decodes an optional member of an external according to the encoding algorithm used, applying the
* try-construct policy to its value.
* Implementation shared by the public overloads taking a @ref CdrTryConstructFlag .
*/
template<class _T, class ... _Args>
Cdr& deserialize_member_try_construct(
optional<external<_T>>& member_value,
CdrTryConstructFlag consistency_flag,
_Args&&... args)
{
if (member_value.has_value() && member_value.value().is_locked())
{
throw exception::BadParamException("External member is locked");
}

if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_)
{
Cdr::state current_state(*this);
MemberId member_id;
xcdr1_deserialize_member_header(member_id, current_state);
auto prev_offset = offset_;
member_value.reset(0 < current_state.member_size_);
if (0 < current_state.member_size_)
{
deserialize(member_value, consistency_flag, std::forward<_Args>(args)...);
}
size_t member_size {current_state.member_size_};
size_t diff {offset_ - prev_offset};
if (member_size < diff)
{
throw exception::BadParamException(
"Member size provided by member header is lower than real decoded member size");
}

// Skip unused bytes
offset_ += (member_size - diff);
}
else
{
deserialize(member_value, consistency_flag, std::forward<_Args>(args)...);
}
return *this;
}

/*!
* @brief Decodes an optional from the buffer, applying the try-construct policy to its value.
* Implementation shared by the public overloads taking a @ref CdrTryConstructFlag .
*/
template<class _T, class ... _Args>
Cdr& deserialize_try_construct(
optional<_T>& value,
CdrTryConstructFlag consistency_flag,
_Args&&... args)
{
bool is_present = true;
if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
{
deserialize_framing(is_present);
}
value.reset(is_present);
if (is_present)
{
deserialize(*value, consistency_flag, std::forward<_Args>(args)...);
}
return *this;
}

/*!
* @brief Decodes an external from the buffer, applying the try-construct policy to its value.
* Implementation shared by the public overloads taking a @ref CdrTryConstructFlag .
*/
template<class _T, class ... _Args>
Cdr& deserialize_try_construct(
external<_T>& value,
CdrTryConstructFlag consistency_flag,
_Args&&... args)
{
if (value.is_locked())
{
throw exception::BadParamException("External member is locked");
}

if (!value)
{
value = external<_T>{new typename external<_T>::type()};
}

deserialize(*value, consistency_flag, std::forward<_Args>(args)...);
return *this;
}

/*!
* @brief Decodes an optional of an external from the buffer, applying the try-construct policy to its value.
* Implementation shared by the public overloads taking a @ref CdrTryConstructFlag .
*/
template<class _T, class ... _Args>
Cdr& deserialize_try_construct(
optional<external<_T>>& value,
CdrTryConstructFlag consistency_flag,
_Args&&... args)
{
if (value.has_value() && value.value().is_locked())
{
throw exception::BadParamException("External member is locked");
}

bool is_present = true;
if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_)
{
deserialize_framing(is_present);
}
value.reset(is_present);
if (is_present)
{
deserialize(*value, consistency_flag, std::forward<_Args>(args)...);
}
return *this;
}

Cdr(
const Cdr&) = delete;

Expand Down
Loading
Loading