188 lines
6.6 KiB
C++
188 lines
6.6 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/GenericMessages/Definition.h
|
|
*
|
|
* @brief Header for the message definition class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_GENERIC_MESSAGES_DEFINITION_H)
|
|
#define ACTIV_BASE_GENERIC_MESSAGES_DEFINITION_H
|
|
|
|
#include "ActivMiddleware/ActivBase/StringException.h"
|
|
#include "ActivMiddleware/CxxYZ/type_traits.h"
|
|
#include "ActivMiddleware/Misc/StringLiteral.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(mpl/copy_if.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(mpl/max_element.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(mpl/size.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(mpl/sort.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(mpl/transform.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(mpl/unique.hpp)
|
|
|
|
#if (defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/arithmetic/inc.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/punctuation/comma_if.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/repetition/repeat_from_to.hpp)
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! GenericMessages namespace.
|
|
namespace GenericMessages
|
|
{
|
|
|
|
namespace Detail
|
|
{
|
|
|
|
template<typename T, typename ENABLE = void>
|
|
class GetDefinitionName
|
|
{
|
|
public:
|
|
CxxYZ::string_view operator()() const
|
|
{
|
|
return CxxYZ::string_view();
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
class GetDefinitionName<T, typename CxxYZ::enable_if<IsStringLiteral<T>::value>::type>
|
|
{
|
|
public:
|
|
CxxYZ::string_view operator()() const
|
|
{
|
|
return T::ToStringView();
|
|
}
|
|
};
|
|
|
|
} // namespace Detail
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Undefined. Specialization exists for each format.
|
|
template<typename FORMAT, typename DEFINITION>
|
|
class DefinitionFormat;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Message definition class.
|
|
*
|
|
* @tparam FIELD_LIST is a Boost::mpl sequence of Field classes defining the message.
|
|
* @tparam NAME an optional StringLiteral for the name of the message.
|
|
*
|
|
* Note this class is not derived from Boost::noncopyable such that it can be used containers in field definitions,
|
|
* which are then mapped to the appropriate storage type.
|
|
*/
|
|
template<typename FIELD_LIST, typename NAME>
|
|
class Definition
|
|
{
|
|
public:
|
|
//! Boost::mpl sequence of Field classes defining the message.
|
|
typedef FIELD_LIST FieldList;
|
|
|
|
private:
|
|
// Validate field ids are unique in FieldList.
|
|
static const size_t totalNumberOfFields = Boost::mpl::size<FieldList>::value;
|
|
|
|
typedef typename Boost::mpl::transform<FieldList, Detail::FieldToFieldIdType<Boost::mpl::_1> >::type FieldIdList;
|
|
typedef typename Boost::mpl::sort<FieldIdList>::type SortedFieldIdList;
|
|
typedef typename Boost::mpl::unique<SortedFieldIdList, CxxYZ::is_same<Boost::mpl::_1, Boost::mpl::_2> >::type UniqueFieldIdList;
|
|
ACTIV_BOOST_STATIC_ASSERT_MSG(static_cast<size_t>(Boost::mpl::size<UniqueFieldIdList>::type::value) == totalNumberOfFields,
|
|
"Field ids are not all unique in the message definition");
|
|
ACTIV_BOOST_STATIC_ASSERT_MSG(0 != totalNumberOfFields, "No fields in message definition");
|
|
|
|
public:
|
|
//! Optional StringLiteral for the name of the message.
|
|
typedef NAME Name;
|
|
|
|
//! Highest field id in FieldList.
|
|
static const FieldId highestFieldId = Boost::mpl::deref<typename Boost::mpl::max_element<FieldIdList>::type>::type::value;
|
|
// Implementation note: doing something "sensible" such as having SortedFieldIdList sorted with mpl::greater and then
|
|
// just taking mpl::front<SortedFieldIdList> for highestFieldId makes compilation much, much slower.
|
|
|
|
//! List of defaulted optional fields.
|
|
typedef typename Boost::mpl::copy_if<FieldList,
|
|
Detail::IsFieldDefaultedOptional<Boost::mpl::_1> >::type DefaultedOptionalFieldList;
|
|
|
|
//! Storage for all the fields.
|
|
typedef MessageStorage<FieldList, Name> Storage;
|
|
|
|
//! For serialization via a regular C++ object, rather than a Storage, when fields are defined as ClassFields.
|
|
typedef ObjectReference<FieldList, Name> Reference;
|
|
|
|
//! Helper to access format-specific definition.
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_TEMPLATE_ALIASES)
|
|
template<typename FORMAT>
|
|
using Format = DefinitionFormat<FORMAT, Definition>;
|
|
#else
|
|
template<typename FORMAT>
|
|
class Format : public DefinitionFormat<FORMAT, Definition>
|
|
{
|
|
};
|
|
#endif
|
|
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
|
|
|
/**
|
|
* @brief Serialize fields by reference to each field to ACTIV format.
|
|
*
|
|
* E.g.:
|
|
* MyMessage::Definition::Serialize(messageBuilder, MyMessage::SomeField::Reference(someField),
|
|
* MyMessage::AnotherField::Reference(anotherField));
|
|
*
|
|
* @param messageBuilder
|
|
* @param fields Field::Reference()s to serialize.
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or an error status.
|
|
*/
|
|
template<typename... FIELDS>
|
|
static StatusCode Serialize(MessageBuilder &messageBuilder, const FIELDS&... fields)
|
|
{
|
|
return DefinitionFormat<GenericMessages::Formats::Activ::Tag, Definition>::Serialize(messageBuilder, fields...);
|
|
}
|
|
|
|
#else
|
|
|
|
static StatusCode Serialize(MessageBuilder &messageBuilder)
|
|
{
|
|
return DefinitionFormat<GenericMessages::Formats::Activ::Tag, Definition>::Serialize(messageBuilder);
|
|
}
|
|
|
|
#define ACTIV_GENERIC_MESSAGE_DEFINITION_TEMPLATE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) typename F##n
|
|
|
|
#define ACTIV_GENERIC_MESSAGE_DEFINITION_SIGNATURE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) const F##n &f##n
|
|
|
|
#define ACTIV_GENERIC_MESSAGE_DEFINITION_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) f##n
|
|
|
|
#define ACTIV_GENERIC_MESSAGE_DEFINITION_SERIALIZE(z, n, data) \
|
|
template<ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_GENERIC_MESSAGE_DEFINITION_TEMPLATE_PARAM, )> \
|
|
static StatusCode Serialize(MessageBuilder &messageBuilder, \
|
|
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_GENERIC_MESSAGE_DEFINITION_SIGNATURE_PARAM, )) \
|
|
{ \
|
|
return DefinitionFormat<GenericMessages::Formats::Activ::Tag, Definition>::Serialize(messageBuilder, \
|
|
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_GENERIC_MESSAGE_DEFINITION_PARAM, )); \
|
|
}
|
|
|
|
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_GM_SERIALIZE_MAX_FIELD_REFERENCES, ACTIV_GENERIC_MESSAGE_DEFINITION_SERIALIZE, )
|
|
|
|
#undef ACTIV_GENERIC_MESSAGE_DEFINITION_SERIALIZE
|
|
#undef ACTIV_GENERIC_MESSAGE_DEFINITION_PARAM
|
|
#undef ACTIV_GENERIC_MESSAGE_DEFINITION_SIGNATURE_PARAM
|
|
#undef ACTIV_GENERIC_MESSAGE_DEFINITION_TEMPLATE_PARAM
|
|
|
|
#endif
|
|
|
|
//! Get the name of the message, or an empty CxxYZ::string_view if not available.
|
|
static CxxYZ::string_view GetName()
|
|
{
|
|
return Detail::GetDefinitionName<Name>()();
|
|
}
|
|
};
|
|
|
|
} // namespace GenericMessages
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_DEFINITION_H)
|