diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 4b46086..2369d6c 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -358,17 +358,21 @@ namespace fc { const uint32_t max_depth; }; - // Default pack/unpack functions for classes (if_class) are removed due to recursion issue. + // Default pack/unpack functions for classes are removed due to recursion issue. // Classes should implement pack/unpack functions explicitly. - template + template struct if_class; - template<> - struct if_class { - template + template + struct if_class::value>> { + template static inline void pack( Stream& s, const T v, uint32_t _max_depth ) = delete; - template + template static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) = delete; + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const int64_t v, uint32_t _max_depth ) { boost::endian::little_int64_buf_t tmp; @@ -381,6 +385,10 @@ namespace fc { s.read( (char*)&tmp, sizeof(tmp) ); v = tmp.value(); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const uint64_t v, uint32_t _max_depth ) { boost::endian::little_uint64_buf_t tmp; @@ -393,6 +401,10 @@ namespace fc { s.read( (char*)&tmp, sizeof(tmp) ); v = tmp.value(); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const int32_t v, uint32_t _max_depth ) { boost::endian::little_int32_buf_t tmp; @@ -405,6 +417,10 @@ namespace fc { s.read( (char*)&tmp, sizeof(tmp) ); v = tmp.value(); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const uint32_t v, uint32_t _max_depth ) { boost::endian::little_uint32_buf_t tmp; @@ -417,6 +433,10 @@ namespace fc { s.read( (char*)&tmp, sizeof(tmp) ); v = tmp.value(); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const int16_t v, uint32_t _max_depth ) { boost::endian::little_int16_buf_t tmp; @@ -429,6 +449,10 @@ namespace fc { s.read( (char*)&tmp, sizeof(tmp) ); v = tmp.value(); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const uint16_t v, uint32_t _max_depth ) { boost::endian::little_uint16_buf_t tmp; @@ -441,6 +465,10 @@ namespace fc { s.read( (char*)&tmp, sizeof(tmp) ); v = tmp.value(); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const int8_t v, uint32_t _max_depth ) { s.write( (char*)&v, 1 ); @@ -449,6 +477,10 @@ namespace fc { static inline void unpack( Stream& s, int8_t& v, uint32_t _max_depth ) { s.read( (char*)&v, 1 ); } + }; + + template<> + struct if_class { template static inline void pack( Stream& s, const uint8_t v, uint32_t _max_depth ) { s.write( (char*)&v, 1 ); @@ -459,27 +491,29 @@ namespace fc { } }; - template - struct if_enum { - template + template + struct if_enum; + template + struct if_enum::value>> { + template static inline void pack( Stream& s, const T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); fc::reflector::visit( pack_object_visitor( v, s, _max_depth - 1 ) ); } - template + template static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); fc::reflector::visit( unpack_object_visitor( v, s, _max_depth - 1 ) ); } }; - template<> - struct if_enum { - template + template + struct if_enum::value>> { + template static inline void pack( Stream& s, const T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); fc::raw::pack( s, (int64_t)v, _max_depth - 1 ); } - template + template static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); int64_t temp; @@ -488,30 +522,30 @@ namespace fc { } }; - template + template struct if_reflected { template static inline void pack( Stream& s, const T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - if_class::type>::pack( s, v, _max_depth - 1 ); + if_class::pack( s, v, _max_depth - 1 ); } template static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - if_class::type>::unpack( s, v, _max_depth - 1 ); + if_class::unpack( s, v, _max_depth - 1 ); } }; template<> - struct if_reflected { + struct if_reflected { template static inline void pack( Stream& s, const T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - if_enum< typename fc::reflector::is_enum >::pack( s, v, _max_depth - 1 ); + if_enum::pack( s, v, _max_depth - 1 ); } template static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - if_enum< typename fc::reflector::is_enum >::unpack( s, v, _max_depth - 1 ); + if_enum::unpack( s, v, _max_depth - 1 ); } }; diff --git a/include/fc/reflect/reflect.hpp b/include/fc/reflect/reflect.hpp index a5122d9..3bf0ef7 100644 --- a/include/fc/reflect/reflect.hpp +++ b/include/fc/reflect/reflect.hpp @@ -7,7 +7,6 @@ */ #include -#include #include #include #include @@ -16,6 +15,7 @@ #include #include #include +#include #include @@ -33,8 +33,7 @@ namespace fc { template struct reflector{ typedef T type; - typedef fc::false_type is_defined; - typedef fc::false_type is_enum; + typedef std::false_type is_defined; /** * @tparam Visitor a function object of the form: @@ -137,8 +136,7 @@ void fc::reflector::visit( const Visitor& v ) { \ #define FC_REFLECT_ENUM( ENUM, FIELDS ) \ namespace fc { \ template<> struct reflector { \ - typedef fc::true_type is_defined; \ - typedef fc::true_type is_enum; \ + 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 ) \ @@ -215,8 +213,7 @@ namespace fc { \ template<> struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ template<> struct reflector {\ typedef TYPE type; \ - typedef fc::true_type is_defined; \ - typedef fc::false_type is_enum; \ + typedef std::true_type is_defined; \ enum member_count_enum { \ local_member_count = 0 BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_MEMBER_COUNT, +, MEMBERS ),\ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ @@ -228,8 +225,7 @@ namespace fc { \ template struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ template struct reflector {\ typedef TYPE type; \ - typedef fc::true_type is_defined; \ - typedef fc::false_type is_enum; \ + typedef std::true_type is_defined; \ enum member_count_enum { \ local_member_count = 0 BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_MEMBER_COUNT, +, MEMBERS ),\ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ @@ -266,7 +262,7 @@ namespace fc { \ template<> struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ template<> struct reflector {\ typedef TYPE type; \ - typedef fc::true_type is_defined; \ + typedef std::true_type is_defined; \ enum member_count_enum { \ local_member_count = BOOST_PP_SEQ_SIZE(MEMBERS), \ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ diff --git a/include/fc/reflect/variant.hpp b/include/fc/reflect/variant.hpp index b47de30..8f88dc9 100644 --- a/include/fc/reflect/variant.hpp +++ b/include/fc/reflect/variant.hpp @@ -63,17 +63,17 @@ namespace fc const uint32_t _max_depth; }; - template - struct if_enum + template + struct if_enum; + template + struct if_enum::value>> { - template static inline void to_variant( const T& v, fc::variant& vo, uint32_t max_depth ) { mutable_variant_object mvo; fc::reflector::visit( to_variant_visitor( mvo, v, max_depth ) ); vo = std::move(mvo); } - template static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth ) { const variant_object& vo = v.get_object(); @@ -81,15 +81,13 @@ namespace fc } }; - template<> - struct if_enum + template + struct if_enum::value>> { - template static inline void to_variant( const T& o, fc::variant& v, uint32_t max_depth = 1 ) { v = fc::reflector::to_fc_string(o); } - template static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth = 1 ) { if( v.is_string() ) @@ -103,12 +101,12 @@ namespace fc template void to_variant( const T& o, variant& v, uint32_t max_depth ) { - if_enum::is_enum>::to_variant( o, v, max_depth ); + if_enum::to_variant( o, v, max_depth ); } template void from_variant( const variant& v, T& o, uint32_t max_depth ) { - if_enum::is_enum>::from_variant( v, o, max_depth ); + if_enum::from_variant( v, o, max_depth ); } } diff --git a/include/fc/utility.hpp b/include/fc/utility.hpp deleted file mode 100644 index ce46946..0000000 --- a/include/fc/utility.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include -#include - -namespace fc { - using std::size_t; - - struct true_type { enum _value { value = 1 }; }; - struct false_type { enum _value { value = 0 }; }; - - namespace detail { - template fc::true_type is_class_helper(void(T::*)()); - template fc::false_type is_class_helper(...); - } - - template - struct is_class { typedef decltype(detail::is_class_helper(0)) type; enum value_enum { value = type::value }; }; -}