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: If an lvalue/rvalue reference to a callable is passed,
31 // the callable will be copied/moved into the returned object.
32 //
33 // NOTE: `overload` is declared and defined below `Overload` because
34 // and we can't declare `overload` here and define it below because it
35 // uses an `auto` return type.
36 
37 template <typename F, typename... Fs>
38 struct Overload;
39 
40 
41 template <typename F>
42 struct Overload<F> : std::remove_reference<F>::type
43 {
45 
46  using Callable::operator();
47 
48  // NOTE: while not strictly necessary, we include `result_type` so
49  // that this can be used places where `result_type` is required,
50  // e.g., `boost::apply_visitor`.
52 
53  template <typename G>
54  Overload(G&& g) : Callable(std::forward<G>(g)) {}
55 };
56 
57 
58 template <typename F, typename... Fs>
59 struct Overload : std::remove_reference<F>::type, Overload<Fs...>
60 {
62 
63  using Callable::operator();
65 
66  // NOTE: while not strictly necessary, we include `result_type` so
67  // that this can be used in places where `result_type` is required,
68  // e.g., `boost::apply_visitor`.
70 
71  template <typename G, typename... Gs>
72  Overload(G&& g, Gs&&... gs)
73  : Callable(std::forward<G>(g)),
74  Overload<Fs...>(std::forward<Gs>(gs)...)
75  {}
76 };
77 
78 
79 template <typename... Fs>
80 auto overload(Fs&&... fs)
81  -> decltype(Overload<Fs...>(std::forward<Fs>(fs)...))
82 {
83  return Overload<Fs...>(std::forward<Fs>(fs)...);
84 }
85 
86 #endif // __STOUT_OVERLOAD_HPP__
auto overload(Fs &&...fs) -> decltype(Overload< Fs... >(std::forward< Fs >(fs)...))
Definition: overload.hpp:80
Definition: type_utils.hpp:619
Definition: overload.hpp:38
Definition: fs.hpp:29
typename LambdaTraits< Callable >::result_type result_type
Definition: overload.hpp:69
typename LambdaTraits< Callable >::result_type result_type
Definition: overload.hpp:51
typename std::remove_reference< F >::type Callable
Definition: overload.hpp:44
Definition: traits.hpp:25
Try< uint32_t > type(const std::string &path)
typename std::remove_reference< F >::type Callable
Definition: overload.hpp:61
Overload(G &&g)
Definition: overload.hpp:54
Overload(G &&g, Gs &&...gs)
Definition: overload.hpp:72