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_UTIL_MATCH_H 16 #define GRPC_SRC_CORE_UTIL_MATCH_H 17 18 #include <grpc/support/port_platform.h> 19 20 #include <utility> 21 22 #include "absl/types/variant.h" 23 #include "src/core/util/overload.h" 24 25 namespace grpc_core { 26 27 namespace detail { 28 29 template <typename... Cases> 30 struct MatchPointerExtractor { 31 OverloadType<Cases...> cases; 32 template <typename T> 33 auto operator()(T& value) -> decltype(cases(&value)) { 34 return cases(&value); 35 } 36 }; 37 38 } // namespace detail 39 40 /// Match on a variant. 41 /// Given variant \a value, and a set of callables \a fs, call the appropriate 42 /// callable based on the type contained in \a value. 43 /// 44 /// Example (prints "hoorah"): 45 /// variant<int, string> v = 42; 46 /// Match(v, 47 /// [](int i) { puts("hoorah"); }, 48 /// [](string s) { puts("boo"); }); 49 template <typename... Fs, typename T0, typename... Ts> 50 auto Match(const absl::variant<T0, Ts...>& value, Fs... fs) 51 -> decltype(std::declval<OverloadType<Fs...>>()(std::declval<T0>())) { 52 return absl::visit(Overload(std::move(fs)...), value); 53 } 54 55 /// A version of Match that takes a mutable pointer to a variant and calls its 56 /// overload callables with a mutable pointer to the current variant value. 57 /// 58 /// Example: 59 /// variant<int, string> v = 42; 60 /// MatchMutable(&v, 61 /// [](int* i) { *i = 1; }, 62 /// [](string* s) { *s = "foo"; }); 63 /// // v now contains 1. 64 template <typename... Fs, typename T0, typename... Ts> 65 auto MatchMutable(absl::variant<T0, Ts...>* value, Fs... fs) 66 -> decltype(std::declval<OverloadType<Fs...>>()(std::declval<T0*>())) { 67 return absl::visit(detail::MatchPointerExtractor<Fs...>{OverloadType<Fs...>( 68 std::move(fs)...)}, 69 *value); 70 } 71 72 } // namespace grpc_core 73 74 #endif // GRPC_SRC_CORE_UTIL_MATCH_H 75