BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
enum_type.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/reflect/reflect.hpp>
3 #include <fc/io/raw_fwd.hpp>
4 #include <fc/variant.hpp>
5 
6 namespace fc
7 {
8  template<typename IntType, typename EnumType>
9  class enum_type
10  {
11  public:
12  enum_type( EnumType t )
13  :value(t){}
14 
15  enum_type( IntType t )
16  :value( (EnumType)t ){}
17 
19 
20  explicit operator IntType()const { return static_cast<IntType>(value); }
21  operator EnumType()const { return value; }
22  operator std::string()const { return fc::reflector<EnumType>::to_string(value); }
23 
24  enum_type& operator=( IntType i ) { value = (EnumType)i; return *this;}
25  enum_type& operator=( EnumType i ) { value = i; return *this;}
26  bool operator<( EnumType i ) const { return value < i; }
27  bool operator>( EnumType i ) const { return value < i; }
28 
29  bool operator<(const enum_type& e) const { return value < e.value;}
30  bool operator>(const enum_type& e) const { return value > e.value;}
31 
32  bool operator<=(const enum_type& e) const { return value <= e.value;}
33  bool operator>=(const enum_type& e) const { return value >= e.value;}
34 
35  friend bool operator==( const enum_type& e, IntType i ){ return e.value == (EnumType)i;}
36  friend bool operator==( const enum_type& e, EnumType i ){ return e.value == i; }
37 
38  friend bool operator==( const enum_type& e, const enum_type& i ){ return e.value == i.value; }
39  friend bool operator==( IntType i, const enum_type& e){ return e.value == (EnumType)i; }
40  friend bool operator==( EnumType i, const enum_type& e ){ return e.value == i; }
41 
42  friend bool operator!=( const enum_type& e, IntType i ){ return e.value != (EnumType)i;}
43  friend bool operator!=( const enum_type& e, EnumType i ){ return e.value != i; }
44  friend bool operator!=( const enum_type& e, const enum_type& i ){ return e.value != i.value; }
45 
46  EnumType value;
47  };
48 
49 
50  template<typename IntType, typename EnumType>
51  void to_variant( const enum_type<IntType,EnumType>& var, variant& vo, uint32_t max_depth = 1 )
52  {
53  to_variant( var.value, vo, max_depth );
54  }
55  template<typename IntType, typename EnumType>
56  void from_variant( const variant& var, enum_type<IntType,EnumType>& vo, uint32_t max_depth )
57  {
58  vo.value = var.as<EnumType>(1);
59  }
60 
61 
63  namespace raw
64  {
65  template<typename Stream, typename IntType, typename EnumType>
66  inline void pack( Stream& s, const fc::enum_type<IntType,EnumType>& tp, uint32_t _max_depth )
67  {
68  FC_ASSERT( _max_depth > 0 );
69  fc::raw::pack( s, static_cast<IntType>(tp), _max_depth - 1 );
70  }
71 
72  template<typename Stream, typename IntType, typename EnumType>
73  inline void unpack( Stream& s, fc::enum_type<IntType,EnumType>& tp, uint32_t _max_depth )
74  {
75  FC_ASSERT( _max_depth > 0 );
76  IntType t;
77  fc::raw::unpack( s, t, _max_depth - 1 );
78  tp = t;
79  }
80  }
81 
82 }
83 
84 
enum_type & operator=(EnumType i)
Definition: enum_type.hpp:25
T as(uint32_t max_depth) const
Definition: variant.hpp:336
friend bool operator==(const enum_type &e, IntType i)
Definition: enum_type.hpp:35
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
friend bool operator==(const enum_type &e, const enum_type &i)
Definition: enum_type.hpp:38
Defines types and macros used to provide reflection.
bool operator>(const enum_type &e) const
Definition: enum_type.hpp:30
enum_type & operator=(IntType i)
Definition: enum_type.hpp:24
friend bool operator!=(const enum_type &e, const enum_type &i)
Definition: enum_type.hpp:44
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
void unpack(Stream &s, fc::enum_type< IntType, EnumType > &tp, uint32_t _max_depth)
Definition: enum_type.hpp:73
friend bool operator==(IntType i, const enum_type &e)
Definition: enum_type.hpp:39
enum_type(EnumType t)
Definition: enum_type.hpp:12
bool operator>=(const enum_type &e) const
Definition: enum_type.hpp:33
void pack(Stream &s, const fc::enum_type< IntType, EnumType > &tp, uint32_t _max_depth)
Definition: enum_type.hpp:66
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
friend bool operator!=(const enum_type &e, EnumType i)
Definition: enum_type.hpp:43
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
friend bool operator!=(const enum_type &e, IntType i)
Definition: enum_type.hpp:42
friend bool operator==(const enum_type &e, EnumType i)
Definition: enum_type.hpp:36
bool operator<=(const enum_type &e) const
Definition: enum_type.hpp:32
bool operator<(EnumType i) const
Definition: enum_type.hpp:26
bool operator<(const enum_type &e) const
Definition: enum_type.hpp:29
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
std::string to_string(double)
Definition: string.cpp:73
enum_type(IntType t)
Definition: enum_type.hpp:15
Definition: api.hpp:15
friend bool operator==(EnumType i, const enum_type &e)
Definition: enum_type.hpp:40
EnumType value
Definition: enum_type.hpp:46
bool operator>(EnumType i) const
Definition: enum_type.hpp:27