emlabcpp
modern opinionated embedded C++ library
match.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./visit.h"
27 
28 namespace emlabcpp
29 {
30 
31 template < typename... Callables >
32 struct matcher : Callables...
33 {
34  matcher( matcher const& ) = default;
35  matcher( matcher&& ) noexcept = default;
36 
37  matcher& operator=( matcher const& ) = default;
38  matcher& operator=( matcher&& ) noexcept = default;
39 
40  template < typename... Ts >
41  explicit matcher( Ts&&... ts )
42  : Callables{ std::forward< Ts >( ts ) }...
43  {
44  }
45 
46  using Callables::operator()...;
47 
48  ~matcher() = default;
49 };
50 
51 template < typename... Callables >
52 matcher( Callables&&... ) -> matcher< std::decay_t< Callables >... >;
53 
54 template < typename Variant, typename... Callables >
55 decltype( auto ) match( Variant&& var, Callables&&... cals )
56 {
57  return emlabcpp::visit(
58  matcher< std::decay_t< Callables >... >( std::forward< Callables >( cals )... ),
59  std::forward< Variant >( var ) );
60 }
61 
62 template < typename Variant, typename... Callables >
63 decltype( auto ) apply_on_match( Variant&& var, Callables&&... cals )
64 {
65  return apply_on_visit(
66  matcher< std::decay_t< Callables >... >( std::forward< Callables >( cals )... ),
67  std::forward< Variant >( var ) );
68 }
69 
70 } // namespace emlabcpp
MIT License.
Definition: impl.h:31
decltype(auto) match(Variant &&var, Callables &&... cals)
Definition: match.h:55
decltype(auto) apply_on_match(Variant &&var, Callables &&... cals)
Definition: match.h:63
decltype(auto) visit(Visitor &&vis, Variant &&var)
Reimplementation of std::visit.
Definition: visit.h:44
decltype(auto) apply_on_visit(Visitor &&vis, Variant &&var)
Combines visit and std::apply into one step - provided variant is expanded with visit and apply is ca...
Definition: visit.h:57
matcher(Callables &&...) -> matcher< std::decay_t< Callables >... >
Definition: match.h:33
~matcher()=default
matcher(matcher &&) noexcept=default
matcher(matcher const &)=default