BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
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 #include <string>
31 #include <vector>
32 #include <boost/container/flat_map.hpp>
34 #include <fc/variant.hpp>
35 
36 namespace graphene { namespace wallet {
37 
38 using std::string;
39 using std::vector;
40 using boost::container::flat_map;
41 using fc::variant;
42 
44 {
45  flat_map< string, int > name_to_which;
46  vector< string > which_to_name;
47 };
48 
49 namespace impl {
50 
51 std::string clean_name( const std::string& name );
52 
54 {
56 
57  typedef void result_type;
58 
59  template< typename T >
60  result_type operator()( const T& dummy )
61  {
62  FC_ASSERT( which == m.which_to_name.size(), "This should not happen" );
63  std::string name = clean_name( fc::get_typename<T>::name() );
64  m.name_to_which[ name ] = which;
65  m.which_to_name.push_back( name );
66  }
67 
69  uint16_t which; // 16 bit should be practically enough
70 };
71 
72 template< typename StaticVariant >
74 {
75  typedef StaticVariant result_type;
76 
77  template< typename Member > // Member is member of static_variant
78  result_type operator()( const Member& dummy )
79  {
80  Member result;
81  from_variant( v, result, _max_depth );
82  return result; // converted from StaticVariant to Result automatically due to return type
83  }
84 
85  const variant& v;
86  const uint32_t _max_depth;
87 
88  from_which_visitor( const variant& _v, uint32_t max_depth ) : v(_v), _max_depth(max_depth) {}
89 };
90 
91 } // namespace impl
92 
93 template< typename T >
94 T from_which_variant( int which, const variant& v, uint32_t max_depth )
95 {
96  // Parse a variant for a known which()
97  T dummy;
98  dummy.set_which( which );
99  impl::from_which_visitor< T > vtor(v, max_depth);
100  return dummy.visit( vtor );
101 }
102 
103 template<typename T>
105 {
106  T dummy;
107  int n = dummy.count();
108  FC_ASSERT( n <= std::numeric_limits<uint16_t>::max(), "Too many items in this static_variant" );
110  for( int i=0; i<n; i++ )
111  {
112  dummy.set_which(i);
113  vtor.which = i;
114  dummy.visit( vtor );
115  }
116  return vtor.m;
117 }
118 
119 } } // namespace graphene::wallet
Definition: api.cpp:56
T from_which_variant(int which, const variant &v, uint32_t max_depth)
result_type operator()(const Member &dummy)
std::string clean_name(const std::string &name)
#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
Defines exception&#39;s used by fc.
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
flat_map< string, int > name_to_which
from_which_visitor(const variant &_v, uint32_t max_depth)
static_variant_map create_static_variant_map()