Files
externals/linux/include/ActivMiddleware/Misc/Detail/SerializableContainerHelperDetail.h

488 lines
17 KiB
C++

/**
* @file include/ActivMiddleware/Misc/Detail/SerializableContainerHelperDetail.h
*
* @brief Classes to help serialize STL containers.
*/
#if (!defined ACTIV_MISC_DETAIL_SERIALIZABLE_CONTAINER_HELPER_DETAIL_H)
#define ACTIV_MISC_DETAIL_SERIALIZABLE_CONTAINER_HELPER_DETAIL_H
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
#include "ActivMiddleware/ActivBase/MessageValidator.h"
#include ACTIV_BOOST_INCLUDE(integer_traits.hpp)
#include ACTIV_BOOST_INCLUDE(type_traits/remove_pointer.hpp)
#include ACTIV_BOOST_INCLUDE(static_assert.hpp)
#include <vector>
#include <list>
#include <set>
#include <map>
//! Activ namespace.
namespace Activ
{
/**
* @brief Serialize object helper function.
*
* This calls Serialize() on the provided object, unless it is a basic signed or unsigned type,
* for which overloads are provided that use the MessageBuilder methods.
*
* @param messageBuilder
* @param object
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
template <typename OBJECT_TYPE>
inline StatusCode SerializeObject(MessageBuilder &messageBuilder, const OBJECT_TYPE &object)
{
return object.Serialize(messageBuilder);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename FIRST, typename SECOND>
inline StatusCode SerializeObject(MessageBuilder &messageBuilder, const std::pair<FIRST, SECOND> &object)
{
// Specialization for std::pairs.
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, SerializeObject(messageBuilder, object.first));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, SerializeObject(messageBuilder, object.second));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Deserialize object helper function.
*
* This calls Deserialize() on the provided object, unless it is a basic signed or unsigned type,
* for which overloads are provided that use the MessageValidator methods.
*
* @param messageValidator
* @param object
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
template <typename OBJECT_TYPE>
inline StatusCode DeserializeObject(MessageValidator &messageValidator, OBJECT_TYPE &object)
{
return object.Deserialize(messageValidator);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename FIRST, typename SECOND>
inline StatusCode DeserializeObject(MessageValidator &messageValidator, std::pair<FIRST, SECOND> &object)
{
// Specialization for std::pairs.
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, DeserializeObject(messageValidator, object.first));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, DeserializeObject(messageValidator, object.second));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Macro to declare SerializeObject() for a basic type.
*/
#define ACTIV_SERIALIZE_OBJECT(type) \
inline StatusCode SerializeObject(MessageBuilder &messageBuilder, const type value) \
{ \
ACTIV_BOOST_STATIC_ASSERT(Boost::is_scalar<type>::value); \
return messageBuilder.Append<MessageBuilder::ENDIAN_LITTLE>(value); \
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Macro to declare DeserializeObject() for a basic type.
*/
#define ACTIV_DESERIALIZE_OBJECT(type) \
inline StatusCode DeserializeObject(MessageValidator &messageValidator, type &value) \
{ \
return messageValidator.ValidateBinaryIntegral<sizeof(value), MessageValidator::ENDIAN_LITTLE>(value); \
}
// ---------------------------------------------------------------------------------------------------------------------------------
// Specializations for basic unsigned types.
ACTIV_SERIALIZE_OBJECT(uint8_t)
ACTIV_DESERIALIZE_OBJECT(uint8_t)
ACTIV_SERIALIZE_OBJECT(uint16_t)
ACTIV_DESERIALIZE_OBJECT(uint16_t)
ACTIV_SERIALIZE_OBJECT(uint32_t)
ACTIV_DESERIALIZE_OBJECT(uint32_t)
ACTIV_SERIALIZE_OBJECT(uint64_t)
ACTIV_DESERIALIZE_OBJECT(uint64_t)
// ---------------------------------------------------------------------------------------------------------------------------------
// Specializations for basic signed types.
ACTIV_SERIALIZE_OBJECT(int8_t)
ACTIV_DESERIALIZE_OBJECT(int8_t)
ACTIV_SERIALIZE_OBJECT(int16_t)
ACTIV_DESERIALIZE_OBJECT(int16_t)
ACTIV_SERIALIZE_OBJECT(int32_t)
ACTIV_DESERIALIZE_OBJECT(int32_t)
ACTIV_SERIALIZE_OBJECT(int64_t)
ACTIV_DESERIALIZE_OBJECT(int64_t)
// ---------------------------------------------------------------------------------------------------------------------------------
// Specializations for boolean type.
inline StatusCode SerializeObject(MessageBuilder &messageBuilder, const bool &booleanValue)
{
return messageBuilder.AppendBoolean(booleanValue);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode DeserializeObject(MessageValidator &messageValidator, bool &booleanValue)
{
return messageValidator.ValidateBoolean(booleanValue);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// Specializations for string type.
inline StatusCode SerializeObject(MessageBuilder &messageBuilder, const String &str)
{
return messageBuilder.Append(str);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode DeserializeObject(MessageValidator &messageValidator, String &str)
{
return messageValidator.ValidateString(str);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper class to (de)serialize objects.
*
* Provides static member functions which wrap up the global SerializeObject() and DeserializeObject().
* This class can then be used as a parameter to SerializableContainerHelper.
*/
template<typename OBJECT_TYPE>
class SerializableObjectHelper
{
public:
/**
* @brief Serialize an object.
*
* @param messageBuilder
* @param object object to serialize.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
static StatusCode Serialize(MessageBuilder &messageBuilder, const OBJECT_TYPE &object)
{
return SerializeObject(messageBuilder, object);
}
/**
* @brief Deserialize an object.
*
* @param messageValidator
* @param object object to deserialize into.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
static StatusCode Deserialize(MessageValidator &messageValidator, OBJECT_TYPE &object)
{
return DeserializeObject(messageValidator, object);
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper class to (de)serialize smart pointers to objects.
*
* Smart pointer type must provide an element_type typedef for the underlying object type.
*
* Provides static member functions which wrap up the global SerializeObject() and DeserializeObject().
* This class can then be used as a parameter to SerializableContainerHelper.
*/
template<typename POINTER_TYPE>
class SerializableSmartPointerHelper
{
public:
typedef typename POINTER_TYPE::element_type ObjectType; ///< Underlying object type
/**
* @brief Serialize an object.
*
* @param messageBuilder
* @param pObject object to serialize.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
static StatusCode Serialize(MessageBuilder &messageBuilder, const POINTER_TYPE &pObject)
{
return SerializeObject(messageBuilder, *pObject);
}
/**
* @brief Deserialize an object.
*
* @param messageValidator
* @param pObject object to deserialize into.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
static StatusCode Deserialize(MessageValidator &messageValidator, POINTER_TYPE &pObject)
{
// new object into raw pointer
ObjectType * const pRawObject = new (std::nothrow) ObjectType;
if (0 == pRawObject)
return STATUS_CODE_HEAP_EMPTY;
// some smart pointers don't allow assignment from raw pointer. Assign from temporary
pObject = POINTER_TYPE(pRawObject);
return DeserializeObject(messageValidator, *pObject);
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper class to (de)serialize raw pointers to objects.
*
* Provides static member functions which wrap up the global SerializeObject() and DeserializeObject().
* This class can then be used as a parameter to SerializableContainerHelper.
*/
template<typename POINTER_TYPE>
class SerializableRawPointerHelper
{
public:
typedef typename Boost::remove_pointer<POINTER_TYPE>::type ObjectType;
/**
* @brief Serialize an object.
*
* @param messageBuilder
* @param pObject object to serialize.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
static StatusCode Serialize(MessageBuilder &messageBuilder, const POINTER_TYPE &pObject)
{
return SerializeObject(messageBuilder, *pObject);
}
/**
* @brief Deserialize an object.
*
* @param messageValidator
* @param pObject object to deserialize into.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
static StatusCode Deserialize(MessageValidator &messageValidator, POINTER_TYPE &pObject)
{
// new object into raw pointer
pObject = new (std::nothrow) ObjectType;
if (0 == pObject)
return STATUS_CODE_HEAP_EMPTY;
return DeserializeObject(messageValidator, *pObject);
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Serialize a non-associative container type.
*
* @param messageBuilder
* @param container the container to serialize.
*/
template<typename CONTAINER_TYPE, typename COUNT_TYPE, COUNT_TYPE MIN_LENGTH, COUNT_TYPE MAX_LENGTH, typename OBJECT_HELPER>
StatusCode SerializeContainer(MessageBuilder &messageBuilder, const CONTAINER_TYPE &container)
{
ACTIV_BOOST_STATIC_ASSERT(Boost::is_unsigned<COUNT_TYPE>::value);
typedef typename Boost::mpl::if_c<
(sizeof(COUNT_TYPE) > sizeof(size_t)),
COUNT_TYPE,
size_t
>::type MaxCount;
typedef CONTAINER_TYPE Container;
typedef OBJECT_HELPER ObjectHelper;
const MaxCount size = container.size();
if (Less<MaxCount, MIN_LENGTH>()(size) || (Greater<MaxCount, MAX_LENGTH>()(size)))
return STATUS_CODE_INVALID_LENGTH;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
messageBuilder.Append<MessageBuilder::ENDIAN_LITTLE>(static_cast<COUNT_TYPE>(size)));
for (typename Container::const_iterator iter = container.begin(), iterEnd = container.end(); iter != iterEnd; ++iter)
{
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, ObjectHelper::Serialize(messageBuilder, *iter));
}
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Serialize an associative container type.
*
* The first type of the pair is assumed to @a not be a pointer.
*
* @param messageBuilder
* @param container the container to serialize.
*/
template<typename CONTAINER_TYPE, typename COUNT_TYPE, COUNT_TYPE MIN_LENGTH, COUNT_TYPE MAX_LENGTH, typename OBJECT_HELPER>
StatusCode SerializeAssociativeContainer(MessageBuilder &messageBuilder, const CONTAINER_TYPE &container)
{
ACTIV_BOOST_STATIC_ASSERT(Boost::is_unsigned<COUNT_TYPE>::value);
typedef typename Boost::mpl::if_c<
(sizeof(COUNT_TYPE) > sizeof(size_t)),
COUNT_TYPE,
size_t
>::type MaxCount;
typedef CONTAINER_TYPE Container;
typedef OBJECT_HELPER ObjectHelper;
const MaxCount size = container.size();
if (Less<MaxCount, MIN_LENGTH>()(size) || (Greater<MaxCount, MAX_LENGTH>()(size)))
return STATUS_CODE_INVALID_LENGTH;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
messageBuilder.Append<MessageBuilder::ENDIAN_LITTLE>(static_cast<COUNT_TYPE>(size)));
for (typename Container::const_iterator iter = container.begin(), iterEnd = container.end(); iter != iterEnd; ++iter)
{
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, SerializeObject(messageBuilder, iter->first));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, ObjectHelper::Serialize(messageBuilder, iter->second));
}
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Deserialize an associative container type.
*
* The first type of the pair is assumed to @a not be a pointer.
*
* @param messageValidator
* @param container the container to deserialize into.
* @param shouldClearContainer whether to clear the container before deserializing.
*/
template<typename CONTAINER_TYPE, typename COUNT_TYPE, COUNT_TYPE MIN_LENGTH, COUNT_TYPE MAX_LENGTH, typename OBJECT_HELPER>
StatusCode DeserializeAssociativeContainer(MessageValidator &messageValidator, CONTAINER_TYPE &container,
const bool shouldClearContainer)
{
typedef COUNT_TYPE Count;
typedef CONTAINER_TYPE Container;
typedef OBJECT_HELPER ObjectHelper;
Count count;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange<
sizeof(count), MessageValidator::ENDIAN_LITTLE, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH>(count)));
if (shouldClearContainer)
container.clear();
for (Count i = 0; i < count; ++i)
{
typename Container::value_type value;
// Cheating slightly to avoid deserializing into separate key and value and resultant additional construction of value_type
// (first of value_type is const. using a pair will incur copy construction if passed to insert).
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
DeserializeObject(messageValidator, const_cast<typename Container::key_type &>(value.first)));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, ObjectHelper::Deserialize(messageValidator, value.second));
std::pair<typename Container::iterator, bool> res = container.insert(value);
// Update an existing entry?
if (!res.second)
res.first->second = value.second;
}
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Deserialize a vector container type.
*
* The first type of the pair is assumed to @a not be a pointer.
*
* @param messageValidator
* @param vector the container to deserialize into.
* @param shouldClearContainer whether to clear the container before deserializing.
*/
template<typename CONTAINER_TYPE, typename COUNT_TYPE, COUNT_TYPE MIN_LENGTH, COUNT_TYPE MAX_LENGTH, typename OBJECT_HELPER>
StatusCode DeserializeVector(MessageValidator &messageValidator, CONTAINER_TYPE &vector, const bool shouldClearContainer)
{
typedef COUNT_TYPE Count;
typedef CONTAINER_TYPE Container;
typedef OBJECT_HELPER ObjectHelper;
Count count;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange<sizeof(count),
MessageValidator::ENDIAN_LITTLE, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH>(count)));
if (shouldClearContainer)
vector.clear();
const size_t originalSize = vector.size();
const size_t newSize = originalSize + count;
StatusCode statusCode;
// Expand the vector to the new size (default constructor called for new objects).
vector.resize(newSize);
for (size_t i = originalSize; i < newSize; ++i)
{
// Deserialize directly into the object in the vector.
statusCode = ObjectHelper::Deserialize(messageValidator, vector.at(i));
if (STATUS_CODE_SUCCESS != statusCode)
{
vector.resize(originalSize);
return statusCode;
}
}
return STATUS_CODE_SUCCESS;
}
} // namespace Activ
#endif // (!defined ACTIV_MISC_DETAIL_SERIALIZABLE_CONTAINER_HELPER_DETAIL_H)