BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
fc::enum_type::enum_type
enum_type(IntType t)
Definition: enum_type.hpp:15
fc::to_string
std::string to_string(double)
Definition: string.cpp:73
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc
Definition: api.hpp:15
fc::enum_type::operator!=
friend bool operator!=(const enum_type &e, EnumType i)
Definition: enum_type.hpp:43
fc::enum_type::operator==
friend bool operator==(const enum_type &e, EnumType i)
Definition: enum_type.hpp:36
fc::enum_type
Definition: enum_type.hpp:9
fc::enum_type::operator<
bool operator<(const enum_type &e) const
Definition: enum_type.hpp:29
fc::enum_type::operator<
bool operator<(EnumType i) const
Definition: enum_type.hpp:26
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
reflect.hpp
Defines types and macros used to provide reflection.
fc::enum_type::value
EnumType value
Definition: enum_type.hpp:46
fc::enum_type::operator==
friend bool operator==(EnumType i, const enum_type &e)
Definition: enum_type.hpp:40
fc::enum_type::operator=
enum_type & operator=(EnumType i)
Definition: enum_type.hpp:25
fc::enum_type::operator>
bool operator>(const enum_type &e) const
Definition: enum_type.hpp:30
fc::enum_type::operator>=
bool operator>=(const enum_type &e) const
Definition: enum_type.hpp:33
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::enum_type::operator<=
bool operator<=(const enum_type &e) const
Definition: enum_type.hpp:32
fc::enum_type::operator==
friend bool operator==(const enum_type &e, IntType i)
Definition: enum_type.hpp:35
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::enum_type::enum_type
enum_type()
Definition: enum_type.hpp:18
fc::enum_type::operator>
bool operator>(EnumType i) const
Definition: enum_type.hpp:27
fc::enum_type::operator!=
friend bool operator!=(const enum_type &e, const enum_type &i)
Definition: enum_type.hpp:44
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::enum_type::operator=
enum_type & operator=(IntType i)
Definition: enum_type.hpp:24
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::enum_type::enum_type
enum_type(EnumType t)
Definition: enum_type.hpp:12
fc::enum_type::operator==
friend bool operator==(const enum_type &e, const enum_type &i)
Definition: enum_type.hpp:38
raw_fwd.hpp
fc::enum_type::operator==
friend bool operator==(IntType i, const enum_type &e)
Definition: enum_type.hpp:39
variant.hpp
fc::enum_type::operator!=
friend bool operator!=(const enum_type &e, IntType i)
Definition: enum_type.hpp:42
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11