Files

596 lines
21 KiB
C++

/**
* @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<typename FORMAT>
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<FieldId ID, typename T, typename TRAITS, typename NAME, typename STORAGE, typename TAG>
class FieldContainer : public FieldDefinitions<ID, T, TRAITS, NAME>
{
public:
typedef STORAGE StorageType;
typedef typename Boost::call_traits<StorageType>::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<typename FORMAT>
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<FieldId ID, typename T, typename TRAITS, typename NAME>
class FieldStorage : public FieldContainer<ID, T, TRAITS, NAME,
typename Detail::FieldTypeToStorageType<T>::type, DefinitionToStorageTag>
{
typedef FieldContainer<ID, T, TRAITS, NAME, typename Detail::FieldTypeToStorageType<T>::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<typename FORMAT>
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<FieldId ID, typename T, typename TRAITS, typename NAME, typename ENABLE = void>
class FieldReference :
public FieldContainer<ID, T, TRAITS, NAME, typename Detail::TypeToFieldReferenceType<T>::type, DefinitionToClassTag>
{
typedef FieldContainer<ID, T, TRAITS, NAME, typename Detail::TypeToFieldReferenceType<T>::type, DefinitionToClassTag> Base;
public:
/**
* @brief Constructor.
*
* @param value
*/
FieldReference(typename Detail::TypeToFieldReferenceType<T>::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<FieldId ID, typename T, typename TRAITS, typename NAME>
class FieldReference<ID, T, TRAITS, NAME, typename Boost::enable_if<SmartPointerHelpers::IsSmartPointer<T> >::type> :
public FieldContainer<ID, T, TRAITS, NAME, typename Detail::TypeToFieldReferenceType<T>::type, DefinitionToClassTag>
{
private:
typedef FieldContainer<ID, T, TRAITS, NAME, typename Detail::TypeToFieldReferenceType<T>::type, DefinitionToClassTag> Base;
public:
/**
* @brief Constructor.
*
* @param value smart pointer to the field's value.
*/
FieldReference(typename Boost::call_traits<typename Detail::FieldTypeToClassType<T>::type>::param_type value) :
Base(SmartPointerHelpers::GetRawPointer<T>()(value))
{
}
/**
* @brief Constructor.
*
* @param value raw pointer to the field's value.
*/
FieldReference(typename Detail::TypeToFieldReferenceType<T>::type value) :
Base(value)
{
}
/**
* @brief Constructor.
*
* @param value reference to the field's value.
*/
FieldReference(typename Boost::add_reference<typename Detail::TypeToFieldReferenceType<
typename SmartPointerHelpers::RemovePointer<T>::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<FieldId ID, typename T, typename TRAITS, typename NAME, typename CONVERTIBLE_TYPE>
class ConvertibleFieldReference : public FieldContainer<ID, T, TRAITS, NAME,
typename Detail::TypeToFieldReferenceType<CONVERTIBLE_TYPE>::type, DefinitionToClassTag>
{
private:
typedef FieldContainer<ID, T, TRAITS, NAME, typename Detail::TypeToFieldReferenceType<CONVERTIBLE_TYPE>::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<typename DEFINITION, typename FIELD_REFERENCE_LIST>
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<typename T>
class IsFieldReferenceList : public CxxYZ::false_type
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename DEFINITION, typename FIELD_REFERENCE_LIST>
class IsFieldReferenceList<FieldReferenceListValue<DEFINITION, FIELD_REFERENCE_LIST> > : public CxxYZ::true_type
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Storage for a list of Field::References.
template<FieldId ID, typename T, typename TRAITS, typename NAME, typename FIELD_REFERENCE_LIST_VALUE>
class FieldReferenceListContainer :
public FieldContainer<ID, T, TRAITS, NAME, FIELD_REFERENCE_LIST_VALUE, DefinitionToFieldReferenceListTag>
{
private:
typedef FIELD_REFERENCE_LIST_VALUE FieldReferenceListValue;
typedef FieldContainer<ID, T, TRAITS, NAME, FieldReferenceListValue, DefinitionToFieldReferenceListTag> 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<typename T, typename CLASS, typename Detail::FieldTypeToClassType<T>::type CLASS::* CLASS_MEMBER_POINTER>
class FieldClassMember
{
public:
typedef CLASS Class; ///< The class the member-pointer refers to.
typedef typename Detail::FieldTypeToClassType<T>::type Type; ///< The type of the member of Class that the member pointer refers to.
typedef typename Boost::call_traits<Type>::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<typename T, typename CLASS, typename Detail::FieldTypeToClassType<T>::type CLASS::* CLASS_MEMBER_POINTER>
const typename FieldClassMember<T, CLASS, CLASS_MEMBER_POINTER>::Pointer FieldClassMember<T, CLASS, CLASS_MEMBER_POINTER>::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<FieldId ID, typename T, typename TRAITS, typename NAME>
class Field : public FieldDefinitions<ID, T, TRAITS, NAME>
{
public:
//! Raw pointer fields are not allowed.
ACTIV_BOOST_STATIC_ASSERT_MSG(!Boost::is_pointer<T>::value, "Raw pointer fields are not supported");
typedef FieldStorage<ID, T, TRAITS, NAME> Storage; ///< Storage for type T.
typedef FieldReference<ID, T, TRAITS, NAME> 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<typename CONVERTIBLE_TYPE>
static ConvertibleFieldReference<ID, T, TRAITS, NAME, typename Detail::TypeToFieldReferenceType<CONVERTIBLE_TYPE>::type>
MakeReference(const CONVERTIBLE_TYPE &value)
{
return ConvertibleFieldReference<ID, T, TRAITS, NAME,
typename Detail::TypeToFieldReferenceType<CONVERTIBLE_TYPE>::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<typename DEFINITION ACTIV_BOOST_PP_REPEAT_FROM_TO(1, ACTIV_BOOST_PP_INC(n), \
ACTIV_GENERIC_MESSAGE_FRL_TMPL_PARAM_DECL, )> \
static typename Boost::enable_if<Detail::IsDefinition<T>, const FieldReferenceListContainer<ID, T, TRAITS, NAME, \
FieldReferenceListValue<DEFINITION, 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)> > > >::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<ID, T, TRAITS, NAME, FieldReferenceListValue<DEFINITION, 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)> > >(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<typename DEFINITION, typename... FIELDS>
static typename Boost::enable_if<Detail::IsDefinition<T>, const FieldReferenceListContainer<ID, T, TRAITS, NAME,
FieldReferenceListValue<DEFINITION, Boost::fusion::vector<FIELDS...>>>>::type
FieldReferenceList(FIELDS... fields)
{
return FieldReferenceListContainer<ID, T, TRAITS, NAME, FieldReferenceListValue<DEFINITION,
Boost::fusion::vector<FIELDS...>>>(Boost::fusion::vector<FIELDS...>(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<FieldId ID, typename T, typename TRAITS, typename CLASS,
typename Detail::FieldTypeToClassType<T>::type CLASS::* CLASS_MEMBER_POINTER, typename NAME = void>
class ClassField : public Field<ID, T, TRAITS, NAME>
{
public:
typedef FieldClassMember<T, CLASS, CLASS_MEMBER_POINTER> 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<id, ACTIV_DECLTYPE(className :: memberName), traits, className, & className :: memberName>
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @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<id, type, traits, ACTIV_MAKE_STRING_LITERAL(ACTIV_STRINGIZE(name))> 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<id, ACTIV_DECLTYPE(className :: memberName), traits, className, \
& className :: memberName, ACTIV_MAKE_STRING_LITERAL(ACTIV_STRINGIZE(name))> name
} // namespace GenericMessages
} // namespace Activ
#endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_FIELD_H)