/** * @file include/ActivMiddleware/ActivBase/GenericMessages/Field.h * * @brief Header for field classes. */ #if (!defined ACTIV_BASE_GENERIC_MESSAGES_FIELD_H) #define ACTIV_BASE_GENERIC_MESSAGES_FIELD_H #include "ActivMiddleware/ActivBase/GenericMessages/Detail/FieldMetaFunctions.h" #include "ActivMiddleware/ActivBase/GenericMessages/Detail/MetaFunctions.h" #include "ActivMiddleware/ActivBase/GenericMessages/Formats/Common/ContentTypeHelper.h" #include "ActivMiddleware/ActivBase/GenericMessages/Formats/Common/FieldMetaFunctions.h" #include "ActivMiddleware/CxxYZ/memory.h" #include "ActivMiddleware/CxxYZ/type_traits.h" #include "ActivMiddleware/Misc/StringLiteral.h" #include ACTIV_BOOST_INCLUDE(mpl/not.hpp) #include ACTIV_BOOST_INCLUDE(fusion/include/vector.hpp) #if (defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) #include ACTIV_BOOST_INCLUDE(preprocessor/arithmetic/inc.hpp) #include ACTIV_BOOST_INCLUDE(preprocessor/punctuation/comma.hpp) #include ACTIV_BOOST_INCLUDE(preprocessor/repetition/repeat_from_to.hpp) #endif //! Activ namespace. namespace Activ { //! GenericMessages namespace. namespace GenericMessages { // Forward declaration of format-specific field format class for friend usage. template class FieldFormat; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Container for a field. * * @tparam ID the field's numeric id. * @tparam T the field's native type. * @tparam TRAITS mandatory / optional traits. * @tparam NAME optional StringLiteral for the name of the field. * @tparam STORAGE the type of storage for the field. * @tparam TAG is a helper so serialization of another message Definition supports both Definition::Storage and reference. */ template class FieldContainer : public FieldDefinitions { public: typedef STORAGE StorageType; typedef typename Boost::call_traits::param_type StorageParamType; typedef TAG Tag; //! Equality operator. bool operator==(const FieldContainer &rhs) const { return m_value == rhs.m_value; } protected: //! Default constructor. FieldContainer() { } /** * @brief Constructor. * * @param value the field's value, for FieldReference types. */ FieldContainer(StorageParamType value) : m_value(value) { } //! Copy constructor. FieldContainer(const FieldContainer &rhs) : m_value(rhs.m_value) { } //! Move constructor. FieldContainer(ACTIV_BOOST_RV_REF(FieldContainer) rhs) : m_value(CxxYZ::move(rhs.m_value)) { } //! Copy assignment. FieldContainer &operator=(ACTIV_BOOST_COPY_ASSIGN_REF(FieldContainer) rhs) { if (this != CxxYZ::addressof(rhs)) m_value = rhs.m_value; return *this; } //! Move assignment. FieldContainer &operator=(ACTIV_BOOST_RV_REF(FieldContainer) rhs) { if (this != CxxYZ::addressof(rhs)) m_value = CxxYZ::move(rhs.m_value); return *this; } StorageType m_value; private: template friend class FieldFormat; ACTIV_BOOST_COPYABLE_AND_MOVABLE(FieldContainer) }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Storage for a field. * * @tparam ID the field's numeric id. * @tparam T the field's native type. * @tparam TRAITS mandatory / optional traits. * @tparam NAME optional StringLiteral for the name of the field. */ template class FieldStorage : public FieldContainer::type, DefinitionToStorageTag> { typedef FieldContainer::type, DefinitionToStorageTag> Base; public: //! Default constructor. FieldStorage() { } //! Copy constructor. FieldStorage(const FieldStorage &rhs) : Base(rhs) { } //! Move constructor. FieldStorage(ACTIV_BOOST_RV_REF(FieldStorage) rhs) : Base(ACTIV_BOOST_MOVE_BASE(Base, rhs)) { } //! Copy assignment. FieldStorage &operator=(ACTIV_BOOST_COPY_ASSIGN_REF(FieldStorage) rhs) { if (this != CxxYZ::addressof(rhs)) (void) Base::operator=(rhs); return *this; } //! Move assignment. FieldStorage &operator=(ACTIV_BOOST_RV_REF(FieldStorage) rhs) { if (this != CxxYZ::addressof(rhs)) (void) Base::operator=(ACTIV_BOOST_MOVE_BASE(Base, rhs)); return *this; } /** * @brief Set the field's value. * * @param value */ void Set(typename Base::StorageParamType value) { this->m_value = value; } /** * @brief Set the field's value. * * @param value */ void Set(ACTIV_BOOST_RV_REF(typename Base::StorageType) value) { this->m_value = CxxYZ::move(value); } /** * @brief Set the default value of the field from the Traits class. * * Only invoked for optional fields if they are not present in a message. */ void SetDefault() { Base::Traits::SetDefault(this->m_value); } /** * @brief Get the field's value. * * @return Const reference to the field's value (for non-built in types), or the field's value (for built-in types). */ typename Base::StorageParamType Get() const { return this->m_value; } /** * @brief Get the field's value. * * @return Reference to the field's value. */ typename Base::StorageType &Get() { return this->m_value; } private: template friend class FieldFormat; ACTIV_BOOST_COPYABLE_AND_MOVABLE(FieldStorage) }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Reference to a non-smart pointer field. * * @tparam ID the field's numeric id. * @tparam T the field's native type. * @tparam TRAITS mandatory / optional traits. * @tparam NAME optional StringLiteral for the name of the field. */ template class FieldReference : public FieldContainer::type, DefinitionToClassTag> { typedef FieldContainer::type, DefinitionToClassTag> Base; public: /** * @brief Constructor. * * @param value */ FieldReference(typename Detail::TypeToFieldReferenceType::type value) : Base(value) { } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Reference to a smart pointer field. * * @tparam ID the field's numeric id. * @tparam T the field's native type. * @tparam TRAITS mandatory / optional traits. * @tparam NAME optional StringLiteral for the name of the field. */ template class FieldReference >::type> : public FieldContainer::type, DefinitionToClassTag> { private: typedef FieldContainer::type, DefinitionToClassTag> Base; public: /** * @brief Constructor. * * @param value smart pointer to the field's value. */ FieldReference(typename Boost::call_traits::type>::param_type value) : Base(SmartPointerHelpers::GetRawPointer()(value)) { } /** * @brief Constructor. * * @param value raw pointer to the field's value. */ FieldReference(typename Detail::TypeToFieldReferenceType::type value) : Base(value) { } /** * @brief Constructor. * * @param value reference to the field's value. */ FieldReference(typename Boost::add_reference::type>::type>::type value) : Base(&value) { // Note we always want to pass by reference here (hence add_reference), as we take the address of the object. // TypeToFieldReferenceType may or may not return a reference! } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Reference to a field. * * @tparam ID the field's numeric id. * @tparam T the field's native type. * @tparam TRAITS mandatory / optional traits. * @tparam NAME optional StringLiteral for the name of the field. * @tparam CONVERTIBLE_TYPE a type that is not dissimilar to T. */ template class ConvertibleFieldReference : public FieldContainer::type, DefinitionToClassTag> { private: typedef FieldContainer::type, DefinitionToClassTag> Base; public: /** * @brief Constructor. * * @param value */ ConvertibleFieldReference(const CONVERTIBLE_TYPE &value) : Base(value) { } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! The type stored in FieldContainer for a fusion vector of Field::Reference types. template class FieldReferenceListValue : public FIELD_REFERENCE_LIST { private: typedef FIELD_REFERENCE_LIST FieldReferenceList; public: typedef DEFINITION Definition; FieldReferenceListValue(FieldReferenceList fieldReferenceList) : FieldReferenceList(fieldReferenceList) { // Note at the time of writing (Boost 1.50) there doesn't appear to be move (emulation) support in fusion. // So passing by value in the hope the compiler will elide any copies. } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Is a type, as stored in a FieldContainer, a list of Field::Reference types? template class IsFieldReferenceList : public CxxYZ::false_type { }; // --------------------------------------------------------------------------------------------------------------------------------- template class IsFieldReferenceList > : public CxxYZ::true_type { }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Storage for a list of Field::References. template class FieldReferenceListContainer : public FieldContainer { private: typedef FIELD_REFERENCE_LIST_VALUE FieldReferenceListValue; typedef FieldContainer Base; public: /** * @brief Constructor. * * @param fieldReferenceListValue */ FieldReferenceListContainer(FieldReferenceListValue fieldReferenceListValue) : Base(fieldReferenceListValue) { // Note at the time of writing (Boost 1.50) there doesn't appear to be move (emulation) support in fusion. // So passing by value in the hope the compiler will elide any copies. } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Class definition for a class-member pointer to a field. * * @tparam ID the field's numeric id. * @tparam CLASS C++ class containing the member. * @tparam CLASS_MEMBER_POINTER member-pointer to a member of CLASS. */ template::type CLASS::* CLASS_MEMBER_POINTER> class FieldClassMember { public: typedef CLASS Class; ///< The class the member-pointer refers to. typedef typename Detail::FieldTypeToClassType::type Type; ///< The type of the member of Class that the member pointer refers to. typedef typename Boost::call_traits::param_type ParamType; ///< Ideal type to pass values of type Type. typedef Type CLASS::* Pointer; ///< The member-pointer type. static const Pointer pointer; ///< The member-pointer itself. }; template::type CLASS::* CLASS_MEMBER_POINTER> const typename FieldClassMember::Pointer FieldClassMember::pointer = CLASS_MEMBER_POINTER; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Definition of a field. * * @tparam ID the field id. * @tparam T the type the field represents. * @tparam TRAITS optional or mandatory traits for the field. * @tparam NAME optional StringLiteral for the name of the field. */ template class Field : public FieldDefinitions { public: //! Raw pointer fields are not allowed. ACTIV_BOOST_STATIC_ASSERT_MSG(!Boost::is_pointer::value, "Raw pointer fields are not supported"); typedef FieldStorage Storage; ///< Storage for type T. typedef FieldReference Reference; ///< Reference to type T. /** * @brief Make a field reference from something that may or may not be the exact type provided to the message definition. * * @param value * * @return A ConvertibleFieldReference object. */ template static ConvertibleFieldReference::type> MakeReference(const CONVERTIBLE_TYPE &value) { return ConvertibleFieldReference::type>(value); } #if (defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) #define ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM_DECL(z, n, data) , typename F##n #define ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM(z, n, data) F##n data() #define ACTIV_GENERIC_MESSAGE_FRL_PARAM_DECL(z, n, data) F##n f##n data() #define ACTIV_GENERIC_MESSAGE_FRL_PARAM(z, n, data) f##n data() // Boost pre-processor macros to generate FieldReferenceList() functions. See the variadic version for how it will look! #define ACTIV_GENERIC_MESSAGE_FIELD_REFERENCE_LIST(z, n, data) \ template \ static typename Boost::enable_if, const FieldReferenceListContainer > > >::type \ FieldReferenceList( \ ACTIV_BOOST_PP_REPEAT_FROM_TO(1, n, ACTIV_GENERIC_MESSAGE_FRL_PARAM_DECL, ACTIV_BOOST_PP_COMMA) \ ACTIV_GENERIC_MESSAGE_FRL_PARAM_DECL(, n, ACTIV_BOOST_PP_EMPTY)) \ { \ return FieldReferenceListContainer > >(Boost::fusion::vector< \ ACTIV_BOOST_PP_REPEAT_FROM_TO(1, n, ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM, ACTIV_BOOST_PP_COMMA) \ ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM(, n, ACTIV_BOOST_PP_EMPTY)>( \ ACTIV_BOOST_PP_REPEAT_FROM_TO(1, n, ACTIV_GENERIC_MESSAGE_FRL_PARAM, ACTIV_BOOST_PP_COMMA) \ ACTIV_GENERIC_MESSAGE_FRL_PARAM(, n, ACTIV_BOOST_PP_EMPTY))); \ } // Define as many FieldReferenceList()s as max Boost::fusion::vector template parameters. ACTIV_BOOST_PP_REPEAT_FROM_TO(1, ACTIV_BOOST_PP_INC(ACTIV_BOOST_FUSION_MAX_VECTOR_SIZE), ACTIV_GENERIC_MESSAGE_FIELD_REFERENCE_LIST, ) #undef ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM_DECL #undef ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM #undef ACTIV_GENERIC_MESSAGE_FRL_PARAM_DECL #undef ACTIV_GENERIC_MESSAGE_FRL_PARAM #else template static typename Boost::enable_if, const FieldReferenceListContainer>>>::type FieldReferenceList(FIELDS... fields) { return FieldReferenceListContainer>>(Boost::fusion::vector(fields...)); } #endif }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Definition of a field and C++ class member pointer for the field's storage. * * @tparam ID the field id. * @tparam T the type the field represents. * @tparam TRAITS optional or mandatory traits for the field. * @tparam CLASS C++ class containing the member. * @tparam CLASS_MEMBER_POINTER member-pointer to a member of CLASS. * @tparam NAME optional StringLiteral for the name of the field. */ template::type CLASS::* CLASS_MEMBER_POINTER, typename NAME = void> class ClassField : public Field { public: typedef FieldClassMember ClassMember; }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Helper to declare a ClassField in C++11 code, or where BOOST_TYPEOF is sufficiently supported. * * For example: * typedef ACTIV_GM_CLASS_FIELD(1, MyClass, m_someMember, GenericMessages::OptionalTraits) SomeMemberField; * * @param id the field id. * @param className C++ class containing the member. * @param memberName the name of the member in @a className. * @param traits optional or mandatory traits for the field. */ #define ACTIV_GM_CLASS_FIELD(id, className, memberName, traits) \ GenericMessages::ClassField // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Helper to declare a named Field typedef. Requires StringLiteral support. * * For example: * ACTIV_GM_NAMED_FIELD(1, someMember, GenericMessages::OptionalTraits); * * @param id the field id. * @param name the field name. * @param type type of the the field. * @param traits optional or mandatory traits for the field. */ #define ACTIV_GM_NAMED_FIELD(id, name, type, traits) \ typedef GenericMessages::Field name // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Helper to declare a named ClassField typedef. Requires StringLiteral support. * * For example: * ACTIV_GM_NAMED_CLASS_FIELD(1, someMember, MyClass, m_someMember, GenericMessages::OptionalTraits); * * @param id the field id. * @param name the field name. * @param className C++ class containing the member. * @param memberName the name of the member in @a className. * @param traits optional or mandatory traits for the field. */ #define ACTIV_GM_NAMED_CLASS_FIELD(id, name, className, memberName, traits) \ typedef GenericMessages::ClassField name } // namespace GenericMessages } // namespace Activ #endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_FIELD_H)