Scorum
operation_util_impl.hpp
Go to the documentation of this file.
1 
2 #pragma once
3 
5 
6 #include <fc/static_variant.hpp>
7 
8 namespace fc {
9 using namespace scorum::protocol;
10 
11 std::string name_from_type(const std::string& type_name);
12 
14 {
15  variant& var;
16  from_operation(variant& dv)
17  : var(dv)
18  {
19  }
20 
21  template <typename T> void operator()(const T& v) const
22  {
23  auto name = name_from_type(fc::get_typename<T>::name());
24  var = variant(std::make_pair(name, v));
25  }
26 };
27 
29 {
30  std::string& name;
31  get_operation_name(std::string& dv)
32  : name(dv)
33  {
34  }
35 
36  template <typename T> void operator()(const T& v) const
37  {
38  name = name_from_type(fc::get_typename<T>::name());
39  }
40 };
41 }
42 
43 namespace scorum {
44 namespace protocol {
45 
47 {
48  template <typename T> void operator()(const T& v) const
49  {
50  v.validate();
51  }
52 };
53 
55 {
56  flat_set<account_name_type>& active;
57  flat_set<account_name_type>& owner;
58  flat_set<account_name_type>& posting;
59  std::vector<authority>& other;
60 
61  operation_get_required_auth_visitor(flat_set<account_name_type>& a,
62  flat_set<account_name_type>& own,
63  flat_set<account_name_type>& post,
64  std::vector<authority>& oth)
65  : active(a)
66  , owner(own)
67  , posting(post)
68  , other(oth)
69  {
70  }
71 
72  template <typename T> void operator()(const T& v) const
73  {
74  v.get_required_active_authorities(active);
75  v.get_required_owner_authorities(owner);
76  v.get_required_posting_authorities(posting);
77  v.get_required_authorities(other);
78  }
79 };
80 }
81 } // scorum::protocol
82 
83 //
84 // Place DEFINE_OPERATION_TYPE in a .cpp file to define
85 // functions related to your operation type
86 //
87 #define DEFINE_OPERATION_TYPE(OperationType) \
88  DEFINE_OPERATION_SERIALIZATOR(OperationType) \
89  \
90  namespace scorum { \
91  namespace protocol { \
92  \
93  void operation_validate(const OperationType& op) \
94  { \
95  op.visit(scorum::protocol::operation_validate_visitor()); \
96  } \
97  \
98  void operation_get_required_authorities(const OperationType& op, \
99  flat_set<account_name_type>& active, \
100  flat_set<account_name_type>& owner, \
101  flat_set<account_name_type>& posting, \
102  std::vector<authority>& other) \
103  { \
104  op.visit(scorum::protocol::operation_get_required_auth_visitor(active, owner, posting, other)); \
105  } \
106  } \
107  } /* scorum::protocol */
108 
109 #define DEFINE_OPERATION_SERIALIZATOR(OperationType) \
110  namespace fc { \
111  \
112  void to_variant(const OperationType& var, fc::variant& vo) \
113  { \
114  var.visit(from_operation(vo)); \
115  } \
116  \
117  void from_variant(const fc::variant& var, OperationType& vo) \
118  { \
119  static std::map<std::string, uint32_t> to_tag = []() { \
120  std::map<std::string, uint32_t> name_map; \
121  for (int i = 0; i < OperationType::count(); ++i) \
122  { \
123  OperationType tmp; \
124  tmp.set_which(i); \
125  std::string n; \
126  tmp.visit(get_operation_name(n)); \
127  name_map[n] = i; \
128  } \
129  return name_map; \
130  }(); \
131  \
132  auto ar = var.get_array(); \
133  if (ar.size() < 2) \
134  return; \
135  if (ar[0].is_uint64()) \
136  vo.set_which(ar[0].as_uint64()); \
137  else \
138  { \
139  auto itr = to_tag.find(ar[0].as_string()); \
140  FC_ASSERT(itr != to_tag.end(), "Invalid operation name: ${n}", ("n", ar[0])); \
141  vo.set_which(to_tag[ar[0].as_string()]); \
142  } \
143  vo.visit(fc::to_static_variant(ar[1])); \
144  } \
145  } // namespace fc
Definition: game.cpp:4
std::string name_from_type(const std::string &type_name)
Definition: asset.cpp:15
void operator()(const T &v) const
get_operation_name(std::string &dv)
void operator()(const T &v) const
operation_get_required_auth_visitor(flat_set< account_name_type > &a, flat_set< account_name_type > &own, flat_set< account_name_type > &post, std::vector< authority > &oth)