BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
vote.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #pragma once
26 
28 
29 namespace graphene { namespace protocol {
30 
52 {
55  uint32_t content = 0;
56 
57  friend size_t hash_value( vote_id_type v ) { return std::hash<uint32_t>()(v.content); }
58  enum vote_type
59  {
64  };
65 
66  vote_id_type() = default;
68  explicit vote_id_type(vote_type type, uint32_t instance = 0)
69  : content(instance<<8 | type)
70  {}
72  explicit vote_id_type(const std::string& serial)
73  { try {
74  auto colon = serial.find(':');
75  FC_ASSERT( colon != std::string::npos );
76  *this = vote_id_type( vote_type( std::stoul(serial.substr(0, colon)) ),
77  uint32_t( std::stoul(serial.substr(colon+1)) ) );
78  } FC_CAPTURE_AND_RETHROW( (serial) ) }
79 
82  {
83  content &= 0xffffff00;
84  content |= type & 0xff;
85  }
87  vote_type type()const
88  {
89  return vote_type(content & 0xff);
90  }
91 
93  void set_instance(uint32_t instance)
94  {
95  assert(instance < 0x01000000);
96  content &= 0xff;
97  content |= instance << 8;
98  }
100  uint32_t instance()const
101  {
102  return content >> 8;
103  }
104 
106  {
107  content = other.content;
108  return *this;
109  }
112  {
114  return *this;
115  }
117  operator uint32_t()const
118  {
119  return instance();
120  }
121 
123  explicit operator std::string()const
124  {
125  return std::to_string(type()) + ":" + std::to_string(instance());
126  }
127 };
128 
129 } } // graphene::protocol
130 
131 namespace fc
132 {
133 
134 class variant;
135 
136 void to_variant( const graphene::protocol::vote_id_type& var, fc::variant& vo, uint32_t max_depth = 1 );
137 void from_variant( const fc::variant& var, graphene::protocol::vote_id_type& vo, uint32_t max_depth = 1 );
138 
139 } // fc
140 
141 FC_REFLECT_TYPENAME( fc::flat_set<graphene::protocol::vote_id_type> )
142 
143 FC_REFLECT_ENUM( graphene::protocol::vote_id_type::vote_type, (witness)(committee)(worker)(VOTE_TYPE_COUNT) )
145 
FC_CAPTURE_AND_RETHROW
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:479
graphene::protocol::vote_id_type::instance
uint32_t instance() const
Get the instance of this vote_id_type.
Definition: vote.hpp:100
fc::to_string
std::string to_string(double)
Definition: string.cpp:73
graphene::protocol::vote_id_type::witness
@ witness
Definition: vote.hpp:61
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
graphene::protocol::vote_id_type::content
uint32_t content
Definition: vote.hpp:55
fc
Definition: api.hpp:15
graphene::protocol::vote_id_type::vote_type
vote_type
Definition: vote.hpp:58
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
graphene::protocol::vote_id_type::vote_id_type
vote_id_type(vote_type type, uint32_t instance=0)
Construct this vote_id_type with provided type and instance.
Definition: vote.hpp:68
graphene::protocol::vote_id_type
An ID for some votable object.
Definition: vote.hpp:51
graphene::protocol::vote_id_type::set_instance
void set_instance(uint32_t instance)
Set the instance of this vote_id_type.
Definition: vote.hpp:93
graphene::protocol::vote_id_type::committee
@ committee
Definition: vote.hpp:60
types.hpp
graphene::protocol::vote_id_type::worker
@ worker
Definition: vote.hpp:62
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
graphene::protocol::vote_id_type::set_type
void set_type(vote_type type)
Set the type of this vote_id_type.
Definition: vote.hpp:81
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
FC_REFLECT_ENUM
FC_REFLECT_ENUM(graphene::net::core_message_type_enum,(trx_message_type)(block_message_type)(core_message_type_first)(item_ids_inventory_message_type)(blockchain_item_ids_inventory_message_type)(fetch_blockchain_item_ids_message_type)(fetch_items_message_type)(item_not_available_message_type)(hello_message_type)(connection_accepted_message_type)(connection_rejected_message_type)(address_request_message_type)(address_message_type)(closing_connection_message_type)(current_time_request_message_type)(current_time_reply_message_type)(check_firewall_message_type)(check_firewall_reply_message_type)(get_current_connections_request_message_type)(get_current_connections_reply_message_type)(core_message_type_last))(different_chain)(already_connected)(connected_to_self)(not_accepting_connections)(blocked)(invalid_hello_message)(client_too_old))(inbound)(outbound))(firewalled)(not_firewalled))(unable_to_connect)(connection_successful)) namespace std
Definition: core_messages.hpp:404
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
graphene::protocol::vote_id_type::vote_id_type
vote_id_type()=default
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
graphene::protocol::vote_id_type::operator=
vote_id_type & operator=(vote_id_type other)
Definition: vote.hpp:105
graphene::protocol::vote_id_type::VOTE_TYPE_COUNT
@ VOTE_TYPE_COUNT
Definition: vote.hpp:63
graphene::protocol::vote_id_type::vote_id_type
vote_id_type(const std::string &serial)
Construct this vote_id_type from a serial string in the form "type:instance".
Definition: vote.hpp:72
graphene::protocol::vote_id_type::type
vote_type type() const
Get the type of this vote_id_type.
Definition: vote.hpp:87
graphene::protocol::vote_id_type::hash_value
friend size_t hash_value(vote_id_type v)
Definition: vote.hpp:57
graphene
Definition: api.cpp:48