added enhanced serialization of enums
This commit is contained in:
parent
2b8b606d08
commit
382f741c73
3 changed files with 87 additions and 6 deletions
72
include/fc/io/enum_type.hpp
Normal file
72
include/fc/io/enum_type.hpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#pragma once
|
||||
#include <fc/reflect/reflect.hpp>
|
||||
#include <fc/io/raw_fwd.hpp>
|
||||
|
||||
|
||||
namespace fc
|
||||
{
|
||||
template<typename IntType, typename EnumType>
|
||||
class enum_type
|
||||
{
|
||||
public:
|
||||
enum_type( EnumType t )
|
||||
:value(t){}
|
||||
|
||||
enum_type( IntType t )
|
||||
:value( (EnumType)t ){}
|
||||
|
||||
enum_type(){}
|
||||
|
||||
operator IntType()const { return static_cast<IntType>(value); }
|
||||
operator EnumType()const { return value; }
|
||||
|
||||
enum_type& operator=( IntType i ) { value = (EnumType)i; return *this;}
|
||||
enum_type& operator=( EnumType i ) { value = i; return *this;}
|
||||
|
||||
EnumType value;
|
||||
};
|
||||
|
||||
/** reflects like an enum */
|
||||
template<typename IntType, typename EnumType>
|
||||
struct reflector< enum_type<IntType,EnumType> >
|
||||
{
|
||||
typedef EnumType type;
|
||||
typedef fc::true_type is_defined;
|
||||
typedef fc::true_type is_enum;
|
||||
|
||||
template<typename Visitor>
|
||||
static inline void visit( const Visitor& v )
|
||||
{
|
||||
reflector<EnumType>::visit(v);
|
||||
}
|
||||
static const char* to_string(int64_t i)
|
||||
{
|
||||
return reflector<EnumType>::to_string(i);
|
||||
}
|
||||
static EnumType from_string(const char* s)
|
||||
{
|
||||
return reflector<EnumType>::from_string(s);
|
||||
}
|
||||
};
|
||||
|
||||
/** serializes like an IntType */
|
||||
namespace raw
|
||||
{
|
||||
template<typename Stream, typename IntType, typename EnumType>
|
||||
inline void pack( Stream& s, const fc::enum_type<IntType,EnumType>& tp )
|
||||
{
|
||||
fc::raw::pack( s, static_cast<IntType>(tp) );
|
||||
}
|
||||
|
||||
template<typename Stream, typename IntType, typename EnumType>
|
||||
inline void unpack( Stream& s, fc::enum_type<IntType,EnumType>& tp )
|
||||
{
|
||||
IntType t;
|
||||
fc::raw::unpack( s, t );
|
||||
tp = t;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -11,9 +11,14 @@ namespace fc {
|
|||
class time_point_sec;
|
||||
class variant;
|
||||
class variant_object;
|
||||
template<typename IntType, typename EnumType> class enum_type;
|
||||
|
||||
namespace ecc { class public_key; class private_key; }
|
||||
namespace raw {
|
||||
template<typename Stream, typename IntType, typename EnumType>
|
||||
inline void pack( Stream& s, const fc::enum_type<IntType,EnumType>& tp );
|
||||
template<typename Stream, typename IntType, typename EnumType>
|
||||
inline void unpack( Stream& s, fc::enum_type<IntType,EnumType>& tp );
|
||||
|
||||
template<typename Stream, typename T> inline void pack( Stream& s, const std::set<T>& value );
|
||||
template<typename Stream, typename T> inline void unpack( Stream& s, std::set<T>& value );
|
||||
|
|
|
|||
|
|
@ -6,17 +6,21 @@ namespace fc {
|
|||
struct unsigned_int {
|
||||
unsigned_int( uint32_t v = 0 ):value(v){}
|
||||
|
||||
operator uint32_t()const { return value; }
|
||||
template<typename T>
|
||||
unsigned_int( T v ):value(v){}
|
||||
|
||||
//operator uint32_t()const { return value; }
|
||||
//operator uint64_t()const { return value; }
|
||||
|
||||
template<typename T>
|
||||
unsigned_int& operator=( const T& v ) { value = v; return *this; }
|
||||
operator T()const { return static_cast<T>(value); }
|
||||
|
||||
unsigned_int& operator=( int32_t v ) { value = v; return *this; }
|
||||
|
||||
uint32_t value;
|
||||
|
||||
template<typename T>
|
||||
friend bool operator==( const unsigned_int& i, const T& v ) { return v == i.value; }
|
||||
template<typename T>
|
||||
friend bool operator!=( const unsigned_int& i, const T& v ) { return v != i.value; }
|
||||
friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return v == i.value; }
|
||||
friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return v != i.value; }
|
||||
};
|
||||
|
||||
struct signed_int {
|
||||
|
|
|
|||
Loading…
Reference in a new issue