hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
visitor.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_VISITOR_HPP
7 #define IROHA_VISITOR_HPP
8 
9 #include <boost/variant/apply_visitor.hpp> // for boost::apply_visitor
10 #include <type_traits> // for std::decay
11 #include <utility> // for std::forward
12 
13 namespace iroha {
14 
15  template <typename... Lambdas>
16  struct lambda_visitor;
17 
18  template <typename Lambda, typename... Lambdas>
19  struct lambda_visitor<Lambda, Lambdas...>
20  : public Lambda, public lambda_visitor<Lambdas...> {
21  using Lambda::operator();
22  using lambda_visitor<Lambdas...>::operator();
23 
24  lambda_visitor(Lambda lambda, Lambdas... lambdas)
25  : Lambda(lambda), lambda_visitor<Lambdas...>(lambdas...) {}
26  };
27 
28  template <typename Lambda>
29  struct lambda_visitor<Lambda> : public Lambda {
30  using Lambda::operator();
31 
32  lambda_visitor(Lambda lambda) : Lambda(lambda) {}
33  };
34 
56  template <class... Fs>
57  constexpr auto make_visitor(Fs &&... fs) {
58  using visitor_type = lambda_visitor<std::decay_t<Fs>...>;
59  return visitor_type(std::forward<Fs>(fs)...);
60  }
61 
77  template <typename TVariant, typename... TVisitors>
78  constexpr decltype(auto) visit_in_place(TVariant &&variant,
79  TVisitors &&... visitors) {
80  return boost::apply_visitor(
81  make_visitor(std::forward<TVisitors>(visitors)...),
82  std::forward<TVariant>(variant));
83  }
84 
86  template <typename T, typename Matcher>
87  constexpr decltype(auto) match(T &&t, Matcher &&m) {
88  return std::forward<T>(t) ? std::forward<Matcher>(m)(*std::forward<T>(t))
89  : std::forward<Matcher>(m)();
90  }
91 
93  template <typename T, typename... Fs>
94  constexpr decltype(auto) match_in_place(T &&t, Fs &&... fs) {
95  return match(std::forward<T>(t), make_visitor(std::forward<Fs>(fs)...));
96  }
97 } // namespace iroha
98 
99 #endif // IROHA_VISITOR_HPP
Definition: block_query.hpp:16