Merge pull request #21 from vikramrajkumar/phoenix

Make fc::reflector<ENUM>::to_string work with C++11 strongly typed enumerations
This commit is contained in:
Nikolai Mushegian 2014-04-25 16:19:39 -04:00
commit 6b84b990b3

View file

@ -17,7 +17,7 @@
#include <fc/reflect/typename.hpp> #include <fc/reflect/typename.hpp>
namespace fc { namespace fc {
/** /**
* @brief defines visit functions for T * @brief defines visit functions for T
@ -32,13 +32,13 @@ template<typename T>
struct reflector{ struct reflector{
typedef T type; typedef T type;
typedef fc::false_type is_defined; typedef fc::false_type is_defined;
typedef fc::false_type is_enum; typedef fc::false_type is_enum;
/** /**
* @tparam Visitor a function object of the form: * @tparam Visitor a function object of the form:
* *
* @code * @code
* struct functor { * struct functor {
* template<typename Member, class Class, Member (Class::*member)> * template<typename Member, class Class, Member (Class::*member)>
* void operator()( const char* name )const; * void operator()( const char* name )const;
* }; * };
@ -46,19 +46,19 @@ struct reflector{
* *
* If T is an enum then the functor has the following form: * If T is an enum then the functor has the following form:
* @code * @code
* struct functor { * struct functor {
* template<int Value> * template<int Value>
* void operator()( const char* name )const; * void operator()( const char* name )const;
* }; * };
* @endcode * @endcode
* *
* @param v a functor that will be called for each member on T * @param v a functor that will be called for each member on T
* *
* @note - this method is not defined for non-reflected types. * @note - this method is not defined for non-reflected types.
*/ */
#ifdef DOXYGEN #ifdef DOXYGEN
template<typename Visitor> template<typename Visitor>
static inline void visit( const Visitor& v ); static inline void visit( const Visitor& v );
#endif // DOXYGEN #endif // DOXYGEN
}; };
@ -77,7 +77,7 @@ void throw_bad_enum_cast( const char* k, const char* e );
#define TEMPLATE template #define TEMPLATE template
#else #else
// Disable warning C4482: nonstandard extention used: enum 'enum_type::enum_value' used in qualified name // Disable warning C4482: nonstandard extention used: enum 'enum_type::enum_value' used in qualified name
#pragma warning( disable: 4482 ) #pragma warning( disable: 4482 )
#define TEMPLATE #define TEMPLATE
#endif #endif
@ -98,14 +98,14 @@ template<typename Visitor>\
static inline void visit( const Visitor& v ) { \ static inline void visit( const Visitor& v ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_BASE, v, INHERITS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_BASE, v, INHERITS ) \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_MEMBER, v, MEMBERS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_MEMBER, v, MEMBERS ) \
} }
#define FC_REFLECT_DERIVED_IMPL_EXT( TYPE, INHERITS, MEMBERS ) \ #define FC_REFLECT_DERIVED_IMPL_EXT( TYPE, INHERITS, MEMBERS ) \
template<typename Visitor>\ template<typename Visitor>\
void fc::reflector<TYPE>::visit( const Visitor& v ) { \ void fc::reflector<TYPE>::visit( const Visitor& v ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_BASE, v, INHERITS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_BASE, v, INHERITS ) \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_MEMBER, v, MEMBERS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_MEMBER, v, MEMBERS ) \
} }
#endif // DOXYGEN #endif // DOXYGEN
@ -132,15 +132,23 @@ template<> struct reflector<ENUM> { \
}\ }\
return nullptr; \ return nullptr; \
} \ } \
static const char* to_string(ENUM elem) { \
switch( elem ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_STRING, ENUM, FIELDS ) \
default: \
fc::throw_bad_enum_cast( BOOST_PP_STRINGIZE(elem), BOOST_PP_STRINGIZE(ENUM) ); \
}\
return nullptr; \
} \
static ENUM from_string( const char* s ) { \ static ENUM from_string( const char* s ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING, ENUM, FIELDS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING, ENUM, FIELDS ) \
fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \ fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \
return ENUM();\ return ENUM();\
} \ } \
}; \ }; \
} }
/* Note: FC_REFLECT_ENUM previously defined this function, but I don't think it ever /* Note: FC_REFLECT_ENUM previously defined this function, but I don't think it ever
* did what we expected it to do. I've disabled it for now. * did what we expected it to do. I've disabled it for now.
* *
* template<typename Visitor> \ * template<typename Visitor> \
@ -152,7 +160,7 @@ template<> struct reflector<ENUM> { \
/** /**
* @def FC_REFLECT_DERIVED(TYPE,INHERITS,MEMBERS) * @def FC_REFLECT_DERIVED(TYPE,INHERITS,MEMBERS)
* *
* @brief Specializes fc::reflector for TYPE where * @brief Specializes fc::reflector for TYPE where
* type inherits other reflected classes * type inherits other reflected classes
* *
* @param INHERITS - a sequence of base class names (basea)(baseb)(basec) * @param INHERITS - a sequence of base class names (basea)(baseb)(basec)
@ -170,7 +178,7 @@ template<> struct reflector<TYPE> {\
total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\
}; \ }; \
FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \
}; } }; }
#define FC_REFLECT_DERIVED_TEMPLATE( TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS ) \ #define FC_REFLECT_DERIVED_TEMPLATE( TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS ) \
namespace fc { \ namespace fc { \
template<BOOST_PP_SEQ_ENUM(TEMPLATE_ARGS)> struct get_typename<TYPE> { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ template<BOOST_PP_SEQ_ENUM(TEMPLATE_ARGS)> struct get_typename<TYPE> { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \
@ -183,9 +191,9 @@ template<BOOST_PP_SEQ_ENUM(TEMPLATE_ARGS)> struct reflector<TYPE> {\
total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\
}; \ }; \
FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \
}; } }; }
//BOOST_PP_SEQ_SIZE(MEMBERS), //BOOST_PP_SEQ_SIZE(MEMBERS),
/** /**
* @def FC_REFLECT(TYPE,MEMBERS) * @def FC_REFLECT(TYPE,MEMBERS)