Kagome
Polkadot Runtime Engine in C++17
variant_builder.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_COMMON_DYNAMIC_VARIANT_HPP
7 #define KAGOME_CORE_COMMON_DYNAMIC_VARIANT_HPP
8 
9 #include <functional>
10 #include <type_traits>
11 #include <vector>
12 
13 #include <boost/mpl/at.hpp>
14 #include <boost/mpl/for_each.hpp>
15 #include <boost/mpl/int.hpp>
16 #include <boost/variant.hpp>
17 
18 namespace kagome::common {
19 
21  namespace dynamic_variant {
28  template <typename Concrete, typename Variant>
29  void init_variant(Variant &v) {
30  v = Concrete{};
31  }
32 
34  template <typename Variant>
36  std::vector<std::function<void(Variant &)>> *vec;
37  template <typename Element>
38  void operator()(Element) {
39  vec->push_back(&init_variant<Element, Variant>);
40  }
41  };
42 
43  template <typename>
44  struct is_boost_variant : std::false_type {};
45 
58  template <typename... Ts>
59  struct is_boost_variant<boost::variant<Ts...>> : std::true_type {};
60  } // namespace dynamic_variant
61 
74  template <typename Variant, typename Types = typename Variant::types>
76  Variant &v_;
77  std::vector<std::function<void(Variant &)>> funcs_;
78 
79  public:
81  explicit VariantBuilder(Variant &v) : v_(v) {
84  boost::mpl::for_each<Types>(builder);
85  };
86 
93  void init(size_t index) {
94  funcs_[index](v_);
95  }
96  };
97 
98 } // namespace kagome::common
99 
100 #endif // KAGOME_CORE_COMMON_DYNAMIC_VARIANT_HPP
Prepares initializers for each type of variant.
std::vector< std::function< void(Variant &)> > * vec
std::vector< std::function< void(Variant &)> > funcs_