BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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
graphene::wallet::create_static_variant_map
static_variant_map create_static_variant_map()
Definition: reflect_util.hpp:104
graphene::wallet::impl::static_variant_map_visitor
Definition: reflect_util.hpp:53
graphene::wallet::impl::static_variant_map_visitor::result_type
void result_type
Definition: reflect_util.hpp:57
graphene::wallet::impl::from_which_visitor::from_which_visitor
from_which_visitor(const variant &_v, uint32_t max_depth)
Definition: reflect_util.hpp:88
graphene::wallet::impl::static_variant_map_visitor::static_variant_map_visitor
static_variant_map_visitor()
Definition: reflect_util.hpp:55
graphene::wallet::from_which_variant
T from_which_variant(int which, const variant &v, uint32_t max_depth)
Definition: reflect_util.hpp:94
graphene::wallet::impl::from_which_visitor::result_type
StaticVariant result_type
Definition: reflect_util.hpp:75
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
graphene::wallet::impl::from_which_visitor::_max_depth
const uint32_t _max_depth
Definition: reflect_util.hpp:86
fc::get_typename
Definition: typename.hpp:20
graphene::wallet::impl::static_variant_map_visitor::which
uint16_t which
Definition: reflect_util.hpp:69
graphene::wallet::impl::static_variant_map_visitor::m
static_variant_map m
Definition: reflect_util.hpp:68
graphene::wallet::impl::clean_name
std::string clean_name(const std::string &name)
Definition: reflect_util.cpp:28
graphene::wallet::impl::from_which_visitor::v
const variant & v
Definition: reflect_util.hpp:85
graphene::wallet::impl::from_which_visitor::operator()
result_type operator()(const Member &dummy)
Definition: reflect_util.hpp:78
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::wallet::static_variant_map::which_to_name
vector< string > which_to_name
Definition: reflect_util.hpp:46
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
exception.hpp
Defines exception's used by fc.
graphene::wallet::impl::from_which_visitor
Definition: reflect_util.hpp:73
variant.hpp
graphene::wallet::static_variant_map::name_to_which
flat_map< string, int > name_to_which
Definition: reflect_util.hpp:45
graphene::wallet::static_variant_map
Definition: reflect_util.hpp:43
graphene::wallet::impl::static_variant_map_visitor::operator()
result_type operator()(const T &dummy)
Definition: reflect_util.hpp:60
graphene
Definition: api.cpp:48