• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_LIB_GPRPP_MATCH_H
16 #define GRPC_SRC_CORE_LIB_GPRPP_MATCH_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <utility>
21 
22 #include "absl/types/variant.h"
23 
24 #include "src/core/lib/gprpp/overload.h"
25 
26 namespace grpc_core {
27 
28 namespace detail {
29 
30 template <typename... Cases>
31 struct MatchPointerExtractor {
32   OverloadType<Cases...> cases;
33   template <typename T>
34   auto operator()(T& value) -> decltype(cases(&value)) {
35     return cases(&value);
36   }
37 };
38 
39 }  // namespace detail
40 
41 /// Match on a variant.
42 /// Given variant \a value, and a set of callables \a fs, call the appropriate
43 /// callable based on the type contained in \a value.
44 ///
45 /// Example (prints "hoorah"):
46 ///   variant<int, string> v = 42;
47 ///   Match(v,
48 ///         [](int i) { puts("hoorah"); },
49 ///         [](string s) { puts("boo"); });
50 template <typename... Fs, typename T0, typename... Ts>
51 auto Match(const absl::variant<T0, Ts...>& value, Fs... fs)
52     -> decltype(std::declval<OverloadType<Fs...>>()(std::declval<T0>())) {
53   return absl::visit(Overload(std::move(fs)...), value);
54 }
55 
56 /// A version of Match that takes a mutable pointer to a variant and calls its
57 /// overload callables with a mutable pointer to the current variant value.
58 ///
59 /// Example:
60 ///   variant<int, string> v = 42;
61 ///   MatchMutable(&v,
62 ///                [](int* i) { *i = 1; },
63 ///                [](string* s) { *s = "foo"; });
64 ///   // v now contains 1.
65 template <typename... Fs, typename T0, typename... Ts>
66 auto MatchMutable(absl::variant<T0, Ts...>* value, Fs... fs)
67     -> decltype(std::declval<OverloadType<Fs...>>()(std::declval<T0*>())) {
68   return absl::visit(detail::MatchPointerExtractor<Fs...>{OverloadType<Fs...>(
69                          std::move(fs)...)},
70                      *value);
71 }
72 
73 }  // namespace grpc_core
74 
75 #endif  // GRPC_SRC_CORE_LIB_GPRPP_MATCH_H
76