forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor.hpp
158 lines (138 loc) · 4.6 KB
/
visitor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <optional>
#include <type_traits> // for std::decay
#include <utility> // for std::forward
#include <variant>
#include <boost/variant.hpp>
#include <boost/variant/apply_visitor.hpp> // for boost::apply_visitor
#include "common/tagged.hpp"
namespace kagome {
template <typename... Lambdas>
struct lambda_visitor;
template <typename Lambda, typename... Lambdas>
struct lambda_visitor<Lambda, Lambdas...>
: public Lambda, public lambda_visitor<Lambdas...> {
using Lambda::operator();
using lambda_visitor<Lambdas...>::operator();
// NOLINTNEXTLINE(google-explicit-constructor)
lambda_visitor(Lambda lambda, Lambdas... lambdas)
: Lambda(lambda), lambda_visitor<Lambdas...>(lambdas...) {}
};
template <typename Lambda>
struct lambda_visitor<Lambda> : public Lambda {
using Lambda::operator();
// NOLINTNEXTLINE(google-explicit-constructor)
lambda_visitor(Lambda lambda) : Lambda(lambda) {}
};
/**
* @brief Convenient in-place compile-time visitor creation, from a set of
* lambdas
*
* @code
* make_visitor([](int a){ return 1; },
* [](std::string b) { return 2; });
* @nocode
*
* is essentially the same as
*
* @code
* struct visitor : public boost::static_visitor<int> {
* int operator()(int a) { return 1; }
* int operator()(std::string b) { return 2; }
* }
* @nocode
*
* @param lambdas
* @return visitor
*/
template <class... Fs>
constexpr auto make_visitor(Fs &&...fs) {
using visitor_type = lambda_visitor<std::decay_t<Fs>...>;
return visitor_type(std::forward<Fs>(fs)...);
}
template <typename... Ts>
struct is_std_variant : std::false_type {};
template <typename... Ts>
struct is_std_variant<std::variant<Ts...>> : std::true_type {};
/**
* @brief Inplace visitor for boost::variant.
* @code
* boost::variant<int, std::string> value = "1234";
* ...
* visit_in_place(value,
* [](int v) { std::cout << "(int)" << v; },
* [](std::string v) { std::cout << "(string)" << v;}
* );
* @nocode
*
* @param variant
* @param lambdas
*/
template <
typename TVariant,
std::enable_if_t<is_std_variant<std::decay_t<TVariant>>::value == false,
bool> = true,
typename... TVisitors>
constexpr decltype(auto) visit_in_place(TVariant &&variant,
TVisitors &&...visitors) {
return boost::apply_visitor(
make_visitor(std::forward<TVisitors>(visitors)...),
std::forward<TVariant>(variant));
}
/**
* @brief Inplace visitor for std::variant.
* @code
* std::variant<int, std::string> value = "1234";
* ...
* visit_in_place(value,
* [](int v) { std::cout << "(int)" << v; },
* [](std::string v) { std::cout << "(string)" << v;}
* );
* @nocode
*
* @param variant
* @param lambdas
*/
template <
typename TVariant,
std::enable_if_t<is_std_variant<std::decay_t<TVariant>>::value == true,
bool> = true,
typename... TVisitors>
constexpr decltype(auto) visit_in_place(TVariant &&variant,
TVisitors &&...visitors) {
return std::visit(make_visitor(std::forward<TVisitors>(visitors)...),
std::forward<TVariant>(variant));
}
template <typename TReturn, typename TVariant>
constexpr std::optional<std::reference_wrapper<TReturn>> if_type(
TVariant &&variant) {
if (auto ptr = boost::get<TReturn>(&variant)) {
return *ptr;
}
return std::nullopt;
}
template <typename Type, typename TVariant>
constexpr bool is_type(TVariant &&variant) {
return boost::get<Type>(&variant) != nullptr;
}
template <typename T, typename Tag, typename TVariant>
constexpr bool is_tagged_by(TVariant &&variant) {
return is_type<Tagged<T, Tag>>(&variant);
}
/// apply Matcher to optional T
template <typename T, typename Matcher>
constexpr decltype(auto) match(T &&t, Matcher &&m) {
return std::forward<T>(t) ? std::forward<Matcher>(m)(*std::forward<T>(t))
: std::forward<Matcher>(m)();
}
/// construct visitor from Fs and apply it to optional T
template <typename T, typename... Fs>
constexpr decltype(auto) match_in_place(T &&t, Fs &&...fs) {
return match(std::forward<T>(t), make_visitor(std::forward<Fs>(fs)...));
}
} // namespace kagome