Scorum
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
reflect_util.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 #pragma once
25 
26 // This file contains various reflection methods that are used to
27 // support the wallet, e.g. allow specifying operations by name
28 // instead of ID.
29 
30 namespace scorum {
31 namespace wallet {
32 
34 {
35  flat_map<std::string, int> name_to_which;
36  std::vector<std::string> which_to_name;
37 };
38 
39 namespace impl {
40 
41 std::string clean_name(const std::string& name)
42 {
43  std::string result;
44  const static std::string prefix = "scorum::protocol::";
45  const static std::string suffix = "_operation";
46  // graphene::chain::.*_operation
47  if ((name.size() >= prefix.size() + suffix.size()) && (name.substr(0, prefix.size()) == prefix)
48  && (name.substr(name.size() - suffix.size(), suffix.size()) == suffix))
49  return name.substr(prefix.size(), name.size() - prefix.size() - suffix.size());
50 
51  // If this line spams the console, please don't just comment it out.
52  // Instead, add code above to deal specifically with the names that are causing the spam.
53  wlog("don't know how to clean name: ${name}", ("name", name));
54  return name;
55 }
56 
58 {
60  {
61  }
62 
63  template <typename T> void operator()(const T& dummy)
64  {
65  assert(which == (int)m.which_to_name.size());
66  std::string name = clean_name(fc::get_typename<T>::name());
67  m.name_to_which[name] = which;
68  m.which_to_name.push_back(name);
69  }
70 
72  int which;
73 };
74 
75 template <typename StaticVariant> struct from_which_visitor
76 {
77  typedef StaticVariant result_type;
78 
79  template <typename Member> // Member is member of static_variant
80  result_type operator()(const Member& dummy)
81  {
82  Member result;
84  return result; // converted from StaticVariant to Result automatically due to return type
85  }
86 
87  const variant& v;
88 
89  from_which_visitor(const variant& _v)
90  : v(_v)
91  {
92  }
93 };
94 
95 } // namespace impl
96 
97 template <typename T> T from_which_variant(int which, const variant& v)
98 {
99  // Parse a variant for a known which()
100  T dummy;
101  dummy.set_which(which);
103  return dummy.visit(vtor);
104 }
105 
107 {
108  T dummy;
109  int n = dummy.count();
111  for (int i = 0; i < n; i++)
112  {
113  dummy.set_which(i);
114  vtor.which = i;
115  dummy.visit(vtor);
116  }
117  return vtor.m;
118 }
119 }
120 } // namespace scorum::wallet
void from_variant(const fc::variant &var, game_type &game)
Definition: game.cpp:12
std::string clean_name(const std::string &name)
static_variant_map create_static_variant_map()
T from_which_variant(int which, const variant &v)
Definition: asset.cpp:15
result_type operator()(const Member &dummy)
std::vector< std::string > which_to_name
flat_map< std::string, int > name_to_which