#pragma once /** * @file fc/reflect/reflect.hpp * * @brief Defines types and macros used to provide reflection. * */ #include #include #include #include #include #include #include #include #include #include #include #include namespace fc { template struct reflector; namespace member_names { /// A template which stores the name of the native member at a given index in a given class template struct member_name { constexpr static const char* value = "Unknown member"; }; } /** * @brief A template to store compile-time information about a field in a reflected struct * * @tparam Container The type of the struct or class containing the field * @tparam Member The type of the field * @tparam field A pointer-to-member for the reflected field */ template struct field_reflection { using container = Container; using type = Member; using reflector = fc::reflector; constexpr static std::size_t index = Index; constexpr static bool is_derived = false; constexpr static type container::*pointer = field; /// @brief Given a reference to the container type, get a reference to the field static type& get(container& c) { return c.*field; } static const type& get(const container& c) { return c.*field; } /// @brief Get the name of the field static const char* get_name() { return fc::member_names::member_name::value; } }; /// Basically the same as @ref field_reflection, but for inherited fields /// Note that inherited field reflections do not have an index field; indexes are for native fields only template struct inherited_field_reflection { using container = Derived; using field_container = Base; using type = Member; using reflector = fc::reflector; constexpr static std::size_t index_in_base = IndexInBase; constexpr static bool is_derived = true; constexpr static type field_container::*pointer = field; static type& get(container& c) { // And we need a distinct inherited_field_reflection type because this conversion can't be done statically type container::* derived_field = field; return c.*derived_field; } static const type& get(const container& c) { type container::* derived_field = field; return c.*derived_field; } static const char* get_name() { using Reflector = typename fc::reflector::native_members::template at; return Reflector::get_name(); } }; namespace impl { /// Helper template to create a @ref field_reflection without any commas (makes it macro-friendly) template struct Reflect_type { template struct with_field_type { template struct at_index { template struct with_field_pointer { using type = field_reflection; }; }; }; }; /// Template to make a transformer of a @ref field_reflection from a base class to a derived class template struct Derivation_reflection_transformer { template struct transform; template struct transform> { using type = inherited_field_reflection; }; template struct transform> { using type = inherited_field_reflection; }; }; } // namespace impl /// Macro to transform reflected fields of a base class to a derived class and concatenate them to a type list #define FC_CONCAT_BASE_MEMBER_REFLECTIONS(r, derived, base) \ ::add_list::members, impl::Derivation_reflection_transformer>> /// Macro to concatenate a new @ref field_reflection to a typelist #define FC_CONCAT_MEMBER_REFLECTION(r, container, idx, member) \ ::add::template with_field_type \ ::template at_index \ ::template with_field_pointer<&container::member>::type> #define FC_REFLECT_MEMBER_NAME(r, container, idx, member) \ template<> struct member_name { constexpr static const char* value = BOOST_PP_STRINGIZE(member); }; #define FC_REFLECT_TEMPLATE_MEMBER_NAME(r, data, idx, member) \ template struct member_name { \ constexpr static const char* value = BOOST_PP_STRINGIZE(member); }; /// Macro to concatenate a new type to a typelist #define FC_CONCAT_TYPE(r, x, TYPE) ::add /** * @brief defines visit functions for T * Unless this is specialized, visit() will not be defined for T. * * @tparam T - the type that will be visited. * * The @ref FC_REFLECT(TYPE,MEMBERS) or FC_STATIC_REFLECT_DERIVED(TYPE,BASES,MEMBERS) macro is used to specialize this * class for your type. */ template struct reflector{ typedef T type; typedef std::false_type is_defined; /// A typelist with a @ref field_reflection for each native member (non-inherited) of the struct using native_members = typelist::list<>; /// A typelist with a @ref field_reflection for each inherited member of the struct using inherited_members = typelist::list<>; /// A typelist with a @ref field_reflection for each member of the struct, starting with inherited members using members = typelist::list<>; /// A typelist of base classes for this type using base_classes = typelist::list<>; /** * @tparam Visitor a function object of the form: * * @code * struct functor { * template * void operator()( const char* name )const; * }; * @endcode * * If T is an enum then the functor has the following form: * @code * struct functor { * template * void operator()( const char* name )const; * }; * @endcode * * @param v a functor that will be called for each member on T * * @note - this method is not defined for non-reflected types. */ #ifdef DOXYGEN template static inline void visit( const Visitor& v ); #endif // DOXYGEN }; void throw_bad_enum_cast( int64_t i, const char* e ); void throw_bad_enum_cast( const char* k, const char* e ); } // namespace fc #ifndef DOXYGEN #define FC_REFLECT_VISIT_BASE(r, visitor, base) \ fc::reflector::visit( visitor ); #ifndef _MSC_VER #define TEMPLATE template #else // Disable warning C4482: nonstandard extention used: enum 'enum_type::enum_value' used in qualified name #pragma warning( disable: 4482 ) #define TEMPLATE #endif #define FC_REFLECT_VISIT_MEMBER( r, visitor, elem ) \ { typedef decltype(((type*)nullptr)->elem) member_type; \ visitor.TEMPLATE operator()( BOOST_PP_STRINGIZE(elem) ); \ } #define FC_REFLECT_VISIT_MEMBER_I( r, visitor, I, elem ) \ case I: FC_REFLECT_VISIT_MEMBER( r, visitor, elem ) break; #define FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ template\ 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_MEMBER, v, MEMBERS ) \ } #endif // DOXYGEN #define FC_REFLECT_VISIT_ENUM( r, enum_type, elem ) \ v.operator()(BOOST_PP_STRINGIZE(elem), int64_t(enum_type::elem) ); #define FC_REFLECT_ENUM_TO_STRING( r, enum_type, elem ) \ case enum_type::elem: return BOOST_PP_STRINGIZE(elem); #define FC_REFLECT_ENUM_TO_FC_STRING( r, enum_type, elem ) \ case enum_type::elem: return std::string(BOOST_PP_STRINGIZE(elem)); #define FC_REFLECT_ENUM_FROM_STRING( r, enum_type, elem ) \ if( strcmp( s, BOOST_PP_STRINGIZE(elem) ) == 0 ) return enum_type::elem; #define FC_REFLECT_ENUM_FROM_STRING_CASE( r, enum_type, elem ) \ case enum_type::elem: #define FC_REFLECT_ENUM( ENUM, FIELDS ) \ namespace fc { \ template<> struct reflector { \ typedef std::true_type is_defined; \ 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( fc::to_string(int64_t(elem)).c_str(), BOOST_PP_STRINGIZE(ENUM) ); \ }\ return nullptr; \ } \ static const char* to_string(int64_t i) { \ return to_string(ENUM(i)); \ } \ static std::string to_fc_string(ENUM elem) { \ switch( elem ) { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_FC_STRING, ENUM, FIELDS ) \ } \ return fc::to_string(int64_t(elem)); \ } \ static std::string to_fc_string(int64_t i) { \ return to_fc_string(ENUM(i)); \ } \ static ENUM from_int(int64_t i) { \ ENUM e = ENUM(i); \ switch( e ) \ { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING_CASE, ENUM, FIELDS ) \ break; \ default: \ fc::throw_bad_enum_cast( i, BOOST_PP_STRINGIZE(ENUM) ); \ } \ return e;\ } \ static ENUM from_string( const char* s ) { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING, ENUM, FIELDS ) \ int64_t i = 0; \ try \ { \ i = boost::lexical_cast(s); \ } \ catch( const boost::bad_lexical_cast& ) \ { \ fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \ } \ return from_int(i); \ } \ template< typename Visitor > \ static void visit( Visitor& v ) \ { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_ENUM, ENUM, FIELDS ) \ } \ }; \ template<> struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(ENUM); } }; \ } /* 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. * * template \ * static inline void visit( const Visitor& v ) { \ * BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_ENUM, ENUM, FIELDS ) \ * }\ */ /** * @def FC_REFLECT_DERIVED(TYPE,INHERITS,MEMBERS) * * @brief Specializes fc::reflector for TYPE where * type inherits other reflected classes * * @param INHERITS - a sequence of base class names (basea)(baseb)(basec) * @param MEMBERS - a sequence of member names. (field1)(field2)(field3) */ #define FC_REFLECT_DERIVED( TYPE, INHERITS, MEMBERS ) \ namespace fc { \ template<> struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ template<> struct reflector {\ typedef TYPE type; \ typedef std::true_type is_defined; \ using native_members = \ typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH_I( FC_CONCAT_MEMBER_REFLECTION, TYPE, MEMBERS ) ::finalize; \ using inherited_members = \ typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH( FC_CONCAT_BASE_MEMBER_REFLECTIONS, TYPE, INHERITS ) ::finalize; \ using members = typename typelist::concat::type; \ using base_classes = typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH( FC_CONCAT_TYPE, x, INHERITS ) ::finalize; \ enum member_count_enum { \ local_member_count = typelist::length(), \ total_member_count = typelist::length() \ }; \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ }; \ namespace member_names { \ BOOST_PP_SEQ_FOR_EACH_I( FC_REFLECT_MEMBER_NAME, TYPE, MEMBERS ) \ } } #define FC_REFLECT_DERIVED_TEMPLATE( TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS ) \ namespace fc { \ template struct get_typename { \ static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } \ }; \ template struct reflector {\ typedef TYPE type; \ typedef std::true_type is_defined; \ using native_members = \ typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH_I( FC_CONCAT_MEMBER_REFLECTION, TYPE, MEMBERS ) ::finalize; \ using inherited_members = \ typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH( FC_CONCAT_BASE_MEMBER_REFLECTIONS, TYPE, INHERITS ) ::finalize; \ using members = typename typelist::concat::type; \ using base_classes = typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH( FC_CONCAT_TYPE, x, INHERITS ) ::finalize; \ enum member_count_enum { \ local_member_count = typelist::length(), \ total_member_count = typelist::length() \ }; \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ }; \ namespace member_names { \ BOOST_PP_SEQ_FOR_EACH_I( FC_REFLECT_TEMPLATE_MEMBER_NAME, (TEMPLATE_ARGS)(TYPE), MEMBERS ) \ } } #define FC_REFLECT_DERIVED_NO_TYPENAME( TYPE, INHERITS, MEMBERS ) \ namespace fc { \ template<> struct reflector {\ typedef TYPE type; \ typedef std::true_type is_defined; \ using native_members = \ typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH_I( FC_CONCAT_MEMBER_REFLECTION, TYPE, MEMBERS ) ::finalize; \ using inherited_members = \ typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH( FC_CONCAT_BASE_MEMBER_REFLECTIONS, TYPE, INHERITS ) ::finalize; \ using members = typename typelist::concat::type; \ using base_classes = typename typelist::builder<>::type \ BOOST_PP_SEQ_FOR_EACH( FC_CONCAT_TYPE, x, INHERITS ) ::finalize; \ enum member_count_enum { \ local_member_count = typelist::length(), \ total_member_count = typelist::length() \ }; \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ }; \ } // fc /** * @def FC_REFLECT(TYPE,MEMBERS) * @brief Specializes fc::reflector for TYPE * * @param MEMBERS - a sequence of member names. (field1)(field2)(field3) * * @see FC_REFLECT_DERIVED */ #define FC_REFLECT( TYPE, MEMBERS ) \ FC_REFLECT_DERIVED( TYPE, BOOST_PP_SEQ_NIL, MEMBERS ) #define FC_REFLECT_TEMPLATE( TEMPLATE_ARGS, TYPE, MEMBERS ) \ FC_REFLECT_DERIVED_TEMPLATE( TEMPLATE_ARGS, TYPE, BOOST_PP_SEQ_NIL, MEMBERS ) #define FC_REFLECT_EMPTY( TYPE ) \ FC_REFLECT_DERIVED( TYPE, BOOST_PP_SEQ_NIL, BOOST_PP_SEQ_NIL ) #define FC_REFLECT_TYPENAME( TYPE ) \ namespace fc { \ template<> struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ }