Files

332 lines
12 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/GenericMessages/Detail/FieldMetaFunctions.h
*
* @brief Header for field metafunction classes used internally.
*/
#if (!defined ACTIV_BASE_GENERIC_MESSAGES_DETAIL_FIELD_META_FUNCTIONS_H)
#define ACTIV_BASE_GENERIC_MESSAGES_DETAIL_FIELD_META_FUNCTIONS_H
#include "ActivMiddleware/CxxYZ/type_traits.h"
#include "ActivMiddleware/Misc/ContainerTraits.h"
#include ACTIV_BOOST_INCLUDE(mpl/count_if.hpp)
//! Activ namespace.
namespace Activ
{
//! GenericMessages namespace.
namespace GenericMessages
{
namespace Detail
{
//! Compare two field types for equality.
template<typename F1, typename F2>
class IsFieldEqual : public MakeBoolConstant<
(F1::id == F2::id) &&
CxxYZ::is_same<typename F1::ProvidedType, typename F2::ProvidedType>::value &&
CxxYZ::is_same<typename F1::Traits, typename F2::Traits>::value &&
CxxYZ::is_same<typename F1::Name, typename F2::Name>::value>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
template<template<typename> class TRANSFORM, typename T>
class FieldTransform;
// ---------------------------------------------------------------------------------------------------------------------------------
// Transform a field's type.
template<template<typename> class TRANSFORM, typename T, typename ENABLE = void>
class FieldTransformImpl
{
public:
typedef typename TRANSFORM<T>::type type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Specialization for a std::pair field.
template<template<typename> class TRANSFORM, typename T1, typename T2>
class FieldTransformImpl<TRANSFORM, std::pair<T1, T2> >
{
private:
typedef typename FieldTransform<TRANSFORM, T1>::type firstType;
typedef typename FieldTransform<TRANSFORM, T2>::type secondType;
public:
typedef std::pair<firstType, secondType> type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
#if (defined ACTIV_GM_HAS_STD_TUPLE_SUPPORT)
// Specialization for a std::tuple field.
template<template<typename> class TRANSFORM, typename... T>
class FieldTransformImpl<TRANSFORM, std::tuple<T...>>
{
public:
typedef std::tuple<typename FieldTransform<TRANSFORM, T>::type...> type;
};
#endif // (defined ACTIV_GM_HAS_STD_TUPLE_SUPPORT)
// ---------------------------------------------------------------------------------------------------------------------------------
// Specialization for a container field.
template<template<typename> class TRANSFORM, typename T>
class FieldTransformImpl<TRANSFORM, T, typename CxxYZ::enable_if<ContainerTraits::IsSupported<T>::value>::type>
{
public:
typedef typename ContainerTraits::Rebind<T, TRANSFORM>::type type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Transform a field's type.
*
* @tparam TRANSFORM the transform to apply (e.g. Definition -> Definition::Storage; Definition -> Definition::Reference).
* @tparam T the field's type (as provided to a Field template).
*/
template<template<typename> class TRANSFORM, typename T>
class FieldTransform : public FieldTransformImpl<TRANSFORM, T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
// Transform a field's type to that suitable for a Message::Storage.
template<typename T, typename ENABLE = void>
class FieldTypeToStorageTransformImpl
{
public:
typedef T type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Specialization for a field that is a Definition (i.e. field is a nested message). The storage type for the nested message field
// is Definition::Storage.
template<typename T>
class FieldTypeToStorageTransformImpl<T, typename CxxYZ::enable_if<Detail::IsDefinition<T>::value>::type>
{
public:
typedef typename T::Storage type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Transform a field's type to one suitable for storage in a Definition::Storage type.
*
* @tparam T the field's type (as provided to a Field template).
*/
template<typename T>
class FieldTypeToStorageTransform : public FieldTypeToStorageTransformImpl<T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
// Transform a field's type to that suitable for a Definition::Reference.
template<typename T, typename ENABLE = void>
class FieldTypeToReferenceTransformImpl
{
public:
typedef T type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Specialization for a Definition field.
template<typename T>
class FieldTypeToReferenceTransformImpl<T, typename CxxYZ::enable_if<Detail::IsDefinition<T>::value>::type>
{
public:
typedef typename T::Reference type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Transform a field's type to one suitable for storage in a Definition::Reference type.
*
* @tparam T the field's type (as provided to a Field template).
*/
template<typename T>
class FieldTypeToReferenceTransform : public FieldTypeToReferenceTransformImpl<T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Metafunction to determine the underlying C++ class type for a field in a Definition::Storage::Reference.
*
* T is the field's type as provided to a Field template.
*/
// Transform a field's type to that suitable for a Definition::Storage::Reference.
template<typename T, typename ENABLE = void>
class FieldTypeToClassTransformImpl
{
public:
typedef T type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// Partial specialization for a Definition field.
template<typename T>
class FieldTypeToClassTransformImpl<T, typename CxxYZ::enable_if<Detail::IsDefinition<T>::value>::type>
{
public:
typedef typename T::Reference::Class type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Transform a field's type to one suitable for storage in a Definition::Storage::Reference type.
*
* @tparam T the field's type (as provided to a Field template).
*/
template<typename T>
class FieldTypeToClassTransform : public FieldTypeToClassTransformImpl<T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Metafunction to determine the suitable storage type for a field in a Definition::Storage.
*
* @tparam T the field's type (as provided to a Field template).
*/
template<typename T>
class FieldTypeToStorageType : public Detail::FieldTransform<Detail::FieldTypeToStorageTransform, T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Metafunction to determine the suitable reference type for a field in a MessageStorage::Reference.
*
* T is the field's type as provided to a Field template.
*/
template<typename T>
class FieldTypeToReferenceType : public Detail::FieldTransform<Detail::FieldTypeToReferenceTransform, T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Metafunction to determine the underlying C++ class type for a field in a MessageStorage::Reference.
*
* T is the field's type as provided to a Field template.
*/
template<typename T>
class FieldTypeToClassType : public Detail::FieldTransform<Detail::FieldTypeToClassTransform, T>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Metafunction to determine the C++ class that a ClassField member pointer points to.
template<typename T>
class ClassFieldToClass
{
public:
typedef typename T::ClassMember::Class type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
//! Metafunction to get the field id as an integral constant from a Field.
template<typename FIELD>
class FieldToFieldIdType : public Boost::mpl::integral_c<FieldId, FIELD::id>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Is a Field mandatory?
template<typename FIELD>
class IsFieldMandatory : public MakeIntegralConstant<CxxYZ::is_same<typename FIELD::Traits::type, MandatoryTag> >
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
//! Is a Field optional and defatuled?
template<typename FIELD>
class IsFieldDefaultedOptional : public MakeIntegralConstant<CxxYZ::is_same<typename FIELD::Traits::type, DefaultedOptionalTag> >
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
//! Is a Field optional and non-defaulted?
template<typename FIELD>
class IsFieldNonDefaultedOptional :
public MakeIntegralConstant<CxxYZ::is_same<typename FIELD::Traits::type, NonDefaultedOptionalTag> >
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Determine the type to be stored in a Field::Reference.
*
* For non-smart pointer fields, the type is most likely const T &, or const T for built in types.
*
* The exceptions are if T is a Definition, or a container containing a Definition, when T is mapped to the underlying
* C++ class (if possible, by Definition using ClassField fields).
*/
template<typename T, typename ENABLE = void>
class TypeToFieldReferenceType
{
public:
typedef typename Boost::call_traits<typename Detail::FieldTypeToClassType<T>::type>::param_type type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Determine the type to be stored in a Field::Reference.
*
* For smart pointer fields, the type is most likely const T *. This allows Field::References to be constructed by
* smart pointer, raw pointer or reference to the pointed to type for convenience.
*
* The exceptions are if T is a Definition, or a container containing a Definition, when T is mapped to the underlying
* C++ class (if possible, by Definition using ClassField fields).
*/
template<typename T>
class TypeToFieldReferenceType<T, typename Boost::enable_if<SmartPointerHelpers::IsSmartPointer<T> >::type>
{
public:
typedef const typename Detail::FieldTypeToClassType<typename SmartPointerHelpers::RemovePointer<T>::type>::type *type;
};
} // namespace Detail
} // namespace GenericMessages
} // namespace Activ
#endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_DETAIL_FIELD_META_FUNCTIONS_H)