171 lines
5.4 KiB
C++
171 lines
5.4 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/GenericMessages/ObjectReference.h
|
|
*
|
|
* @brief Header for ObjectReference class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_GENERIC_MESSAGES_OBJECT_REFERENCE_H)
|
|
#define ACTIV_BASE_GENERIC_MESSAGES_OBJECT_REFERENCE_H
|
|
|
|
#include "ActivMiddleware/ActivBase/GenericMessages/Detail/OptionalFieldsHelper.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(mpl/front.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(mpl/copy_if.hpp)
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! GenericMessages namespace.
|
|
namespace GenericMessages
|
|
{
|
|
|
|
// Undefined. Specialization exists for each format.
|
|
template<typename FORMAT, typename OBJECT_REFERENCE>
|
|
class ObjectReferenceFormat;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Reference to a C++ object to serialize/deserlialize.
|
|
*
|
|
* ObjectReference does not manage storage for each field, it provides only static Serialize() and Deserialize() methods to which
|
|
* a reference to a C++ object can be passed.
|
|
*
|
|
* @tparam FIELD_LIST a Boost::mpl sequence defining the message. At least one field in FIELD_LIST must be a ClassField to make
|
|
* ObjectReference of any use.
|
|
* @tparam NAME an optional StringLiteral for the name of the message.
|
|
*/
|
|
template<typename FIELD_LIST, typename NAME>
|
|
class ObjectReference
|
|
{
|
|
private:
|
|
typedef GenericMessages::Definition<FIELD_LIST, NAME> Definition;
|
|
|
|
// List of types from FIELD_LIST that are ClassFields rather than just Fields.
|
|
typedef typename Boost::mpl::copy_if<FIELD_LIST, IsClassField<Boost::mpl::_1> >::type ClassFieldList;
|
|
|
|
// List of the Class nested types from ClassFieldList. They don't need to all be the same type, but they must be
|
|
// from the same (linear!) hierarchy.
|
|
typedef typename Boost::mpl::transform<ClassFieldList, Detail::ClassFieldToClass<Boost::mpl::_1> >::type ClassList;
|
|
|
|
public:
|
|
typedef typename Detail::SequenceFront<ClassList>::type Class; ///< The C++ class type that we are (de)serializing.
|
|
|
|
/**
|
|
* @brief Serialize all the fields in FIELD_LIST that are a ClassField from an object reference.
|
|
*
|
|
* @tparam FORMAT the format to serialize to.
|
|
* @tparam T the serializer (MessageBuilder, rapidjson::writer, etc.).
|
|
*
|
|
* @param serializer
|
|
* @param object
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or error status.
|
|
*/
|
|
template<typename FORMAT, typename T>
|
|
static StatusCode Serialize(T &serializer, const Class &object)
|
|
{
|
|
return ObjectReferenceFormat<FORMAT, ObjectReference>::Serialize(serializer, object);
|
|
}
|
|
|
|
/**
|
|
* @brief Deserialize a message from a specific format.
|
|
*
|
|
* @tparam FORMAT the received format.
|
|
* @tparam T the deserializer (MessageValidator, Json::Value, etc.).
|
|
*
|
|
* @param deserializer
|
|
* @param object
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or error status.
|
|
*/
|
|
template<typename FORMAT, typename T>
|
|
static StatusCode Deserialize(T &deserializer, Class &object)
|
|
{
|
|
typedef typename FieldDispatcherSingleton<FORMAT, Definition>::type FieldDispatcherSingleton;
|
|
|
|
const FieldDispatcherSingleton &fieldDispatcherSingleton(FieldDispatcherSingleton::GetInstance());
|
|
FieldIdBitmap presentFieldIdBitmap(Definition::highestFieldId + 1, false);
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (ObjectReferenceFormat<FORMAT, ObjectReference>::Deserialize(
|
|
deserializer, object, fieldDispatcherSingleton, presentFieldIdBitmap)));
|
|
|
|
if (ACTIV_LIKELY(fieldDispatcherSingleton.AreAllMandatoryOrNonDefaultedOptionalFieldsPresent(presentFieldIdBitmap)))
|
|
SetOptional(presentFieldIdBitmap, object, fieldDispatcherSingleton);
|
|
else
|
|
return STATUS_CODE_MISSING_FIELDS;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* @brief Serialize all the fields in FIELD_LIST that are a ClassField from an object reference to ACTIV format.
|
|
*
|
|
* @param messageBuilder
|
|
* @param object
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or error status.
|
|
*/
|
|
static StatusCode Serialize(MessageBuilder &messageBuilder, const Class &object)
|
|
{
|
|
return Serialize<Formats::Activ::Tag>(messageBuilder, object);
|
|
}
|
|
|
|
/**
|
|
* @brief Deserialize a message from ACTIV format.
|
|
*
|
|
* @param messageValidator
|
|
* @param object
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or error status.
|
|
*/
|
|
static StatusCode Deserialize(MessageValidator &messageValidator, Class &object)
|
|
{
|
|
return Deserialize<Formats::Activ::Tag>(messageValidator, object);
|
|
}
|
|
|
|
private:
|
|
template<typename T>
|
|
class SetOptionalField
|
|
{
|
|
public:
|
|
SetOptionalField(Class &object, const T &fieldDispatcherSingleton) :
|
|
m_object(object),
|
|
m_fieldDispatcherSingleton(fieldDispatcherSingleton)
|
|
{
|
|
}
|
|
|
|
void operator()(const FieldId fieldId) const
|
|
{
|
|
const typename T::IFieldDispatcher * const pIFieldDispatcher(m_fieldDispatcherSingleton.GetFieldDispatcher(fieldId));
|
|
|
|
if (ACTIV_LIKELY(ACTIV_NULLPTR != pIFieldDispatcher))
|
|
pIFieldDispatcher->SetDefault(m_object);
|
|
else
|
|
assert(false);
|
|
}
|
|
|
|
private:
|
|
Class & m_object;
|
|
const T & m_fieldDispatcherSingleton;
|
|
};
|
|
|
|
template<typename T>
|
|
static void SetOptional(const FieldIdBitmap &presentFieldIdBitmap, Class &object, const T &fieldDispatcherSingleton)
|
|
{
|
|
SetOptionalField<T> setOptionalField(object, fieldDispatcherSingleton);
|
|
Detail::ForNotPresentOptionalFields<typename Definition::DefaultedOptionalFieldList>(presentFieldIdBitmap,
|
|
setOptionalField);
|
|
}
|
|
|
|
template<typename FORMAT, typename OBJECT_REFERENCE>
|
|
friend class ObjectReferenceFormat;
|
|
};
|
|
|
|
} // namespace GenericMessages
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_OBJECT_REFERENCE_H)
|