Fix serializing enums

This commit is contained in:
Eric Frias 2014-03-27 14:10:00 -04:00
parent 061007ee29
commit d27be6851b
2 changed files with 35 additions and 7 deletions

View file

@ -205,6 +205,31 @@ namespace fc {
}
};
template<typename IsEnum=fc::false_type>
struct if_enum {
template<typename Stream, typename T>
static inline void pack( Stream& s, const T& v ) {
fc::reflector<T>::visit( pack_object_visitor<Stream,T>( v, s ) );
}
template<typename Stream, typename T>
static inline void unpack( Stream& s, T& v ) {
fc::reflector<T>::visit( unpack_object_visitor<Stream,T>( v, s ) );
}
};
template<>
struct if_enum<fc::true_type> {
template<typename Stream, typename T>
static inline void pack( Stream& s, const T& v ) {
fc::raw::pack(s, (int64_t)v);
}
template<typename Stream, typename T>
static inline void unpack( Stream& s, T& v ) {
int64_t temp;
fc::raw::unpack(s, temp);
v = (T)temp;
}
};
template<typename IsReflected=fc::false_type>
struct if_reflected {
template<typename Stream, typename T>
@ -220,11 +245,11 @@ namespace fc {
struct if_reflected<fc::true_type> {
template<typename Stream, typename T>
static inline void pack( Stream& s, const T& v ) {
fc::reflector<T>::visit( pack_object_visitor<Stream,T>( v, s ) );
if_enum< typename fc::reflector<T>::is_enum >::pack(s,v);
}
template<typename Stream, typename T>
static inline void unpack( Stream& s, T& v ) {
fc::reflector<T>::visit( unpack_object_visitor<Stream,T>( v, s ) );
if_enum< typename fc::reflector<T>::is_enum >::unpack(s,v);
}
};

View file

@ -124,10 +124,6 @@ namespace fc { \
template<> struct reflector<ENUM> { \
typedef fc::true_type is_defined; \
typedef fc::true_type is_enum; \
template<typename Visitor> \
static inline void visit( const Visitor& v ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_ENUM, ENUM, FIELDS ) \
}\
static const char* to_string(int64_t i) { \
switch( ENUM(i) ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_STRING, ENUM, FIELDS ) \
@ -144,7 +140,14 @@ template<> struct reflector<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<typename Visitor> \
* 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)