/** * @file include/ActivMiddleware/ActivBase/GenericMessages/Traits.h * * @brief Header for field traits classes. */ #if (!defined ACTIV_BASE_GENERIC_MESSAGES_TRAITS_H) #define ACTIV_BASE_GENERIC_MESSAGES_TRAITS_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/GenericMessages/Types.h" #include "ActivMiddleware/ActivBase/GenericMessages/Detail/MetaFunctions.h" #include "ActivMiddleware/ActivBase/GenericMessages/Formats/Common/SmartPointerHelpers.h" #include "ActivMiddleware/Misc/StringLiteral.h" #include ACTIV_BOOST_INCLUDE(utility/value_init.hpp) //! Activ namespace. namespace Activ { //! GenericMessages namespace. namespace GenericMessages { class MandatoryTag { }; ///< Tag for mandatory field traits. class NonDefaultedOptionalTag { }; ///< Tag for optional fields that won't be defaulted traits. class DefaultedOptionalTag { }; ///< Tag for optional fields that will be defaulted traits. // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Traits base. * * Derived classes must provided a function of the form: * * template * static void SetDefault(T &value); */ template class Traits { public: typedef TAG type; }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Mandatory field traits. class MandatoryTraits : public Traits { public: template static void SetDefault(T &) { // No default for mandatory field; shouldn't be called. assert(false); } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Optional field traits that aren't defaulted. * * Fields not received "on the wire" will not have a default value set. For Definition::Storage objects, * IsFieldPresent() will return false and Get() will throw. * * If deserializing in to regular C++ objects, missing non-defaulted fields will cause a deserialization failure. */ class NonDefaultedOptionalTraits : public Traits { public: template static void SetDefault(T &) { // No default for non-defaulted optional field; shouldn't be called. assert(false); } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Optional field traits. * * If used for a field of built-in integral type, the default value for the field is 0. * For floating-point built-in types, the default value is 0.0. * For the bool built-in type, the default value is false. * If used with fields of class type, the class must provide a default constructor that sets a useful value. */ class OptionalTraits : public Traits { public: template static void SetDefault(T &value) { SetDefaultImpl(value, SmartPointerHelpers::IsSmartPointer()); } private: template static void SetDefaultImpl(T &value, CxxYZ::false_type) { value = Boost::get(Boost::value_initialized()); } template static void SetDefaultImpl(T &pValue, CxxYZ::true_type) { pValue.reset(); } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Optional field traits with an explicit default value. template class DefaultedNativeOptionalTraits : public Traits { public: static void SetDefault(T &value) { value = DEFAULT_VALUE; } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Optional field traits with an explicit default value via a static const "value" member of another type. template class DefaultedOptionalTraits : public Traits { public: template static void SetDefault(U &value) { value = T::value; } }; // --------------------------------------------------------------------------------------------------------------------------------- //! DefaultedOptionalTraits specialization for StringLiteral types. template class DefaultedOptionalTraits::value>::type> : public Traits { public: template static void SetDefault(U &value) { value = ToStringType(T()); } }; } // namespace GenericMessages } // namespace Activ #endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_TRAITS_H)