Apache Mesos
overload.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OVERLOAD_HPP__
14 #define __STOUT_OVERLOAD_HPP__
15 
16 #include <stout/traits.hpp>
17 
18 
19 // Using `overload` you can pass in callable objects that have
20 // `operator()` and get a new callable object that has all of the
21 // `operator()`s pulled in. For example:
22 //
23 // auto lambdas = overload(
24 // [](int i) { return stringify(i); },
25 // [](double d) { return stringify(d); },
26 // [](const std::string& s) { return s; });
27 //
28 // See stout/variant.hpp for how this is used to visit variants.
29 //
30 // NOTE: `overload` is declared and defined below `Overload` because
31 // and we can't declare `overload` here and define it below because it
32 // uses an `auto` return type.
33 
34 template <typename F, typename... Fs>
35 struct Overload;
36 
37 
38 template <typename F>
39 struct Overload<F> : F
40 {
41  using F::operator();
42 
43  // NOTE: while not strictly necessary, we include `result_type` so
44  // that this can be used places where `result_type` is required,
45  // e.g., `boost::apply_visitor`.
47 
48  template <typename G>
49  Overload(G&& g) : F(std::forward<G>(g)) {}
50 };
51 
52 
53 template <typename F, typename... Fs>
54 struct Overload : F, Overload<Fs...>
55 {
56  using F::operator();
58 
59  // NOTE: while not strictly necessary, we include `result_type` so
60  // that this can be used places where `result_type` is required,
61  // e.g., `boost::apply_visitor`.
63 
64  template <typename G, typename... Gs>
65  Overload(G&& g, Gs&&... gs)
66  : F(std::forward<G>(g)), Overload<Fs...>(std::forward<Gs>(gs)...) {}
67 };
68 
69 
70 template <typename... Fs>
71 auto overload(Fs&&... fs)
72  -> decltype(Overload<Fs...>(std::forward<Fs>(fs)...))
73 {
74  return Overload<Fs...>(std::forward<Fs>(fs)...);
75 }
76 
77 #endif // __STOUT_OVERLOAD_HPP__
auto overload(Fs &&...fs) -> decltype(Overload< Fs... >(std::forward< Fs >(fs)...))
Definition: overload.hpp:71
typename LambdaTraits< F >::result_type result_type
Definition: overload.hpp:46
typename LambdaTraits< F >::result_type result_type
Definition: overload.hpp:62
Definition: type_utils.hpp:510
Definition: overload.hpp:35
Definition: fs.hpp:29
Definition: traits.hpp:25
Overload(G &&g)
Definition: overload.hpp:49
Overload(G &&g, Gs &&...gs)
Definition: overload.hpp:65