Skip to content

Commit

Permalink
Support of external members (#170)
Browse files Browse the repository at this point in the history
* Refs #19697. External template and api tests

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #19697. Encode/decode external

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #19697. Apply suggestions

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

---------

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware authored Oct 24, 2023
1 parent 7112895 commit 92021a8
Show file tree
Hide file tree
Showing 8 changed files with 1,831 additions and 0 deletions.
78 changes: 78 additions & 0 deletions include/fastcdr/Cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "exceptions/Exception.h"
#include "exceptions/NotEnoughMemoryException.h"
#include "FastBuffer.h"
#include "xcdr/external.hpp"
#include "xcdr/MemberId.hpp"
#include "xcdr/optional.hpp"

Expand Down Expand Up @@ -2676,6 +2677,27 @@ class Cdr
return *this;
}

/*!
* @brief Encodes an external in the buffer.
* @param[in] value A reference to the external which will be encoded in the buffer.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when external is null.
* @exception exception::NotEnoughMemoryException This exception is thrown when trying to encode into a buffer
* position that exceeds the internal memory size.
*/
template<class _T>
Cdr& serialize(
const external<_T>& value)
{
if (!value)
{
throw exception::BadParamException("External member is null");
}

serialize(*value);
return *this;
}

/*!
* @brief Tells the encoder the member identifier for the next member to be encoded.
* @param[in] member_id Member identifier.
Expand Down Expand Up @@ -2719,6 +2741,62 @@ class Cdr
return *this;
}

/*!
* @brief Decodes an external from the buffer.
* @param[out] value A reference to the variable where the external will be stored.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked.
* @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)
{
if (value.is_locked())
{
throw exception::BadParamException("External member is locked");
}

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

deserialize(*value);
return *this;
}

/*!
* @brief Decodes an optional of an external from the buffer.
* @param[out] value A reference to the variable where the optional will be stored.
* @return Reference to the eprosima::fastcdr::Cdr object.
* @exception exception::BadParamException This exception is thrown when the external is locked.
* @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)
{
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(is_present);
}
value.reset(is_present);
if (is_present)
{
deserialize(*value);
}
return *this;
}

private:

Cdr(
Expand Down
22 changes: 22 additions & 0 deletions include/fastcdr/CdrSizeCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "CdrEncoding.hpp"
#include "cdr/fixed_size_string.hpp"
#include "detail/container_recursive_inspector.hpp"
#include "exceptions/BadParamException.h"
#include "xcdr/external.hpp"
#include "xcdr/MemberId.hpp"
#include "xcdr/optional.hpp"

Expand Down Expand Up @@ -751,6 +753,26 @@ class CdrSizeCalculator
return calculated_size;
}

/*!
* @brief Specific template which calculates the encoded size of an instance of an external type.
* @param[in] data Reference to the instance.
* @param[inout] current_alignment Current alignment in the encoding.
* @exception exception::BadParamException This exception is thrown when the external is null.
* @return Encoded size of the instance.
*/
template<class _T>
size_t calculate_serialized_size(
const external<_T>& data,
size_t& current_alignment)
{
if (!data)
{
throw exception::BadParamException("External member is null");
}

return calculate_serialized_size(*data, current_alignment);
}

/*!
* @brief Specific template which calculates the encoded size of an instance of an array of unknown type.
* @tparam _T Array's type.
Expand Down
83 changes: 83 additions & 0 deletions include/fastcdr/exceptions/LockedExternalAccessException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _FASTCDR_EXCEPTIONS_LOCKEDEXTERNALACCESSEXCEPTION_H_
#define _FASTCDR_EXCEPTIONS_LOCKEDEXTERNALACCESSEXCEPTION_H_

#include "Exception.h"

namespace eprosima {
namespace fastcdr {
namespace exception {
/*!
* @brief This class is thrown as an exception when accessing to set the value of a locked external.
* @ingroup EXCEPTIONMODULE
*/
class LockedExternalAccessException : public Exception
{
public:

/*!
* @brief Default constructor.
*
* @param message An error message. This message pointer is copied.
*/
Cdr_DllAPI LockedExternalAccessException(
const char* const& message) noexcept;

/*!
* @brief Default copy constructor.
*
* @param ex LockedExternalAccessException that will be copied.
*/
Cdr_DllAPI LockedExternalAccessException(
const LockedExternalAccessException& ex) noexcept;

/*!
* @brief Default move constructor.
*
* @param ex LockedExternalAccessException that will be moved.
*/
Cdr_DllAPI LockedExternalAccessException(
LockedExternalAccessException&& ex) noexcept;

/*!
* @brief Assigment operation.
*
* @param ex LockedExternalAccessException that will be copied.
*/
Cdr_DllAPI LockedExternalAccessException& operator =(
const LockedExternalAccessException& ex) noexcept;

/*!
* @brief Assigment operation.
*
* @param ex LockedExternalAccessException that will be moved.
*/
LockedExternalAccessException& operator =(
LockedExternalAccessException&& ex) noexcept;

//! @brief Default destructor
virtual Cdr_DllAPI ~LockedExternalAccessException() noexcept;

//! @brief This function throws the object as exception.
Cdr_DllAPI void raise() const override;

//! @brief Default message used in the library.
static Cdr_DllAPI const char* const LOCKED_EXTERNAL_ACCESS_MESSAGE_DEFAULT;
};
} //namespace exception
} //namespace fastcdr
} //namespace eprosima
#endif // _FASTCDR_EXCEPTIONS_LOCKEDEXTERNALACCESSEXCEPTION_H_
Loading

0 comments on commit 92021a8

Please sign in to comment.