/** * @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 class GetDefinitionName { public: CxxYZ::string_view operator()() const { return CxxYZ::string_view(); } }; template class GetDefinitionName::value>::type> { public: CxxYZ::string_view operator()() const { return T::ToStringView(); } }; } // namespace Detail // --------------------------------------------------------------------------------------------------------------------------------- // Undefined. Specialization exists for each format. template 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 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::value; typedef typename Boost::mpl::transform >::type FieldIdList; typedef typename Boost::mpl::sort::type SortedFieldIdList; typedef typename Boost::mpl::unique >::type UniqueFieldIdList; ACTIV_BOOST_STATIC_ASSERT_MSG(static_cast(Boost::mpl::size::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::type>::type::value; // Implementation note: doing something "sensible" such as having SortedFieldIdList sorted with mpl::greater and then // just taking mpl::front for highestFieldId makes compilation much, much slower. //! List of defaulted optional fields. typedef typename Boost::mpl::copy_if >::type DefaultedOptionalFieldList; //! Storage for all the fields. typedef MessageStorage Storage; //! For serialization via a regular C++ object, rather than a Storage, when fields are defined as ClassFields. typedef ObjectReference Reference; //! Helper to access format-specific definition. #if (!defined ACTIV_BOOST_NO_CXX11_TEMPLATE_ALIASES) template using Format = DefinitionFormat; #else template class Format : public DefinitionFormat { }; #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 static StatusCode Serialize(MessageBuilder &messageBuilder, const FIELDS&... fields) { return DefinitionFormat::Serialize(messageBuilder, fields...); } #else static StatusCode Serialize(MessageBuilder &messageBuilder) { return DefinitionFormat::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 \ 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::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()(); } }; } // namespace GenericMessages } // namespace Activ #endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_DEFINITION_H)