2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <type_traits>
23 * folly implementation of `std::overload` like functionality
28 * boost::variant<One, Two> value;
30 * variant_match(value,
31 * [] (const One& one) { ... },
32 * [] (const Two& two) { ... });
38 template <typename...>
41 template <typename Case, typename... Cases>
42 struct Overload<Case, Cases...> : Overload<Cases...>, Case {
43 Overload(Case c, Cases... cs)
44 : Overload<Cases...>(std::move(cs)...), Case(std::move(c)) {}
46 using Case::operator();
47 using Overload<Cases...>::operator();
50 template <typename Case>
51 struct Overload<Case> : Case {
52 explicit Overload(Case c) : Case(std::move(c)) {}
54 using Case::operator();
59 * Combine multiple `Cases` in one function object
61 template <typename... Cases>
62 decltype(auto) overload(Cases&&... cases) {
63 return details::Overload<typename std::decay<Cases>::type...>{
64 std::forward<Cases>(cases)...};
68 * Match `Variant` with one of the `Cases`
70 * Note: you can also use `[] (const auto&) {...}` as default case
73 template <typename Variant, typename... Cases>
74 decltype(auto) variant_match(Variant&& variant, Cases&&... cases) {
76 overload(std::forward<Cases>(cases)...), std::forward<Variant>(variant));