Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
static_variant.hpp
Go to the documentation of this file.
1#pragma once
2#include <stdexcept>
3#include <typeinfo>
4#include <type_traits>
6#include <boost/core/typeinfo.hpp>
7#include <variant>
8
9namespace fc {
10
11template<typename Result>
12struct visitor {};
13
14template <typename variant, int32_t i = 0>
15void from_index(variant& v, int index)
16{
17 if constexpr(i >= std::variant_size_v<variant>)
18 {
19 FC_THROW_EXCEPTION(fc::assert_exception, "Provided index out of range for variant.");
20 }
21 else if (index == 0)
22 {
23 auto value = variant(std::in_place_index<i>);
24 v = std::move(value);
25 }
26 else
27 {
28 from_index<variant, i + 1>(v, index - 1);
29 }
30}
31
32template<typename VariantType, typename T, std::size_t index = 0>
33constexpr std::size_t get_index()
34{
35 if constexpr (index == std::variant_size_v<VariantType>)
36 {
37 return index;
38 }
39 else if constexpr (std::is_same_v<std::variant_alternative_t<index, VariantType>, T>)
40 {
41 return index;
42 }
43 else
44 {
46 }
47}
48
50{
53
54 template<typename T> void operator()( const T& v )const
55 {
56 to_variant( v, var );
57 }
58};
59
61{
62 const variant& var;
63 to_static_variant( const variant& dv ):var(dv){}
64
65 template<typename T> void operator()( T& v )const
66 {
67 from_variant( var, v );
68 }
69};
70
71template<typename... T> void to_variant( const std::variant<T...>& s, fc::variant& v )
72{
73 variants vars(2);
74 vars[0] = s.index();
75 std::visit( from_static_variant(vars[1]), s );
76 v = std::move(vars);
77}
78
79template<typename... T> void from_variant( const fc::variant& v, std::variant<T...>& s )
80{
81 auto ar = v.get_array();
82 if( ar.size() < 2 )
83 {
84 s = std::variant<T...>();
85 return;
86 }
87 from_index(s, ar[0].as_uint64());
88 std::visit( to_static_variant(ar[1]), s );
89}
90
91template<typename... T> struct get_typename { static const char* name() { return BOOST_CORE_TYPEID(std::variant<T...>).name(); } };
92
93}
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
variants & get_array()
Definition variant.cpp:496
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
namespace sysio::chain
Definition authority.cpp:3
std::vector< fc::variant > variants
Definition variant.hpp:173
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
constexpr std::size_t get_index()
void from_index(variant &v, int index)
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
void operator()(const T &v) const
static const char * name()
void operator()(T &v) const
to_static_variant(const variant &dv)
char * s