diff --git a/include/fc/io/enum_type.hpp b/include/fc/io/enum_type.hpp new file mode 100644 index 0000000..f6c737a --- /dev/null +++ b/include/fc/io/enum_type.hpp @@ -0,0 +1,72 @@ +#pragma once +#include +#include + + +namespace fc +{ + template + 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(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 + struct reflector< enum_type > + { + typedef EnumType type; + typedef fc::true_type is_defined; + typedef fc::true_type is_enum; + + template + static inline void visit( const Visitor& v ) + { + reflector::visit(v); + } + static const char* to_string(int64_t i) + { + return reflector::to_string(i); + } + static EnumType from_string(const char* s) + { + return reflector::from_string(s); + } + }; + + /** serializes like an IntType */ + namespace raw + { + template + inline void pack( Stream& s, const fc::enum_type& tp ) + { + fc::raw::pack( s, static_cast(tp) ); + } + + template + inline void unpack( Stream& s, fc::enum_type& tp ) + { + IntType t; + fc::raw::unpack( s, t ); + tp = t; + } + } + +} + + diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index e11a98a..0e7f088 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -11,9 +11,14 @@ namespace fc { class time_point_sec; class variant; class variant_object; + template class enum_type; namespace ecc { class public_key; class private_key; } namespace raw { + template + inline void pack( Stream& s, const fc::enum_type& tp ); + template + inline void unpack( Stream& s, fc::enum_type& tp ); template inline void pack( Stream& s, const std::set& value ); template inline void unpack( Stream& s, std::set& value ); diff --git a/include/fc/io/varint.hpp b/include/fc/io/varint.hpp index 0ab2ce9..f3c49ad 100644 --- a/include/fc/io/varint.hpp +++ b/include/fc/io/varint.hpp @@ -6,17 +6,21 @@ namespace fc { struct unsigned_int { unsigned_int( uint32_t v = 0 ):value(v){} - operator uint32_t()const { return value; } + template + unsigned_int( T v ):value(v){} + + //operator uint32_t()const { return value; } + //operator uint64_t()const { return value; } template - unsigned_int& operator=( const T& v ) { value = v; return *this; } + operator T()const { return static_cast(value); } + + unsigned_int& operator=( int32_t v ) { value = v; return *this; } uint32_t value; - template - friend bool operator==( const unsigned_int& i, const T& v ) { return v == i.value; } - template - 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 {