]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::overload_linearly`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
6 | Distributed under the Boost Software License, Version 1.0. | |
7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP | |
11 | #define BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP | |
12 | ||
13 | #include <boost/hana/config.hpp> | |
14 | #include <boost/hana/detail/decay.hpp> | |
15 | ||
16 | #include <utility> | |
17 | ||
18 | ||
19 | BOOST_HANA_NAMESPACE_BEGIN | |
20 | //! @ingroup group-functional | |
21 | //! Call the first function that produces a valid call expression. | |
22 | //! | |
23 | //! Given functions `f1, ..., fn`, `overload_linearly(f1, ..., fn)` is | |
24 | //! a new function that calls the first `fk` producing a valid call | |
25 | //! expression with the given arguments. Specifically, | |
26 | //! @code | |
27 | //! overload_linearly(f1, ..., fn)(args...) == fk(args...) | |
28 | //! @endcode | |
29 | //! | |
30 | //! where `fk` is the _first_ function such that `fk(args...)` is a valid | |
31 | //! expression. | |
32 | //! | |
33 | //! | |
34 | //! Example | |
35 | //! ------- | |
36 | //! @include example/functional/overload_linearly.cpp | |
37 | #ifdef BOOST_HANA_DOXYGEN_INVOKED | |
38 | constexpr auto overload_linearly = [](auto&& f1, auto&& f2, ..., auto&& fn) { | |
39 | return [perfect-capture](auto&& ...x) -> decltype(auto) { | |
40 | return forwarded(fk)(forwarded(x)...); | |
41 | }; | |
42 | }; | |
43 | #else | |
44 | template <typename F, typename G> | |
45 | struct overload_linearly_t { | |
46 | F f; | |
47 | G g; | |
48 | ||
49 | private: | |
50 | template <typename ...Args, typename = | |
51 | decltype(std::declval<F const&>()(std::declval<Args>()...))> | |
52 | constexpr F const& which(int) const& { return f; } | |
53 | ||
54 | template <typename ...Args, typename = | |
55 | decltype(std::declval<F&>()(std::declval<Args>()...))> | |
56 | constexpr F& which(int) & { return f; } | |
57 | ||
58 | template <typename ...Args, typename = | |
59 | decltype(std::declval<F&&>()(std::declval<Args>()...))> | |
60 | constexpr F which(int) && { return static_cast<F&&>(f); } | |
61 | ||
62 | template <typename ...Args> | |
63 | constexpr G const& which(long) const& { return g; } | |
64 | ||
65 | template <typename ...Args> | |
66 | constexpr G& which(long) & { return g; } | |
67 | ||
68 | template <typename ...Args> | |
69 | constexpr G which(long) && { return static_cast<G&&>(g); } | |
70 | ||
71 | public: | |
72 | template <typename ...Args> | |
73 | constexpr decltype(auto) operator()(Args&& ...args) const& | |
74 | { return which<Args...>(int{})(static_cast<Args&&>(args)...); } | |
75 | ||
76 | template <typename ...Args> | |
77 | constexpr decltype(auto) operator()(Args&& ...args) & | |
78 | { return which<Args...>(int{})(static_cast<Args&&>(args)...); } | |
79 | ||
80 | template <typename ...Args> | |
81 | constexpr decltype(auto) operator()(Args&& ...args) && | |
82 | { return which<Args...>(int{})(static_cast<Args&&>(args)...); } | |
83 | }; | |
84 | ||
85 | struct make_overload_linearly_t { | |
86 | template <typename F, typename G> | |
87 | constexpr overload_linearly_t< | |
88 | typename detail::decay<F>::type, | |
89 | typename detail::decay<G>::type | |
90 | > operator()(F&& f, G&& g) const { | |
91 | return {static_cast<F&&>(f), static_cast<G&&>(g)}; | |
92 | } | |
93 | ||
94 | template <typename F, typename G, typename ...H> | |
95 | constexpr decltype(auto) operator()(F&& f, G&& g, H&& ...h) const { | |
96 | return (*this)(static_cast<F&&>(f), | |
97 | (*this)(static_cast<G&&>(g), static_cast<H&&>(h)...)); | |
98 | } | |
99 | ||
100 | template <typename F> | |
101 | constexpr typename detail::decay<F>::type operator()(F&& f) const { | |
102 | return static_cast<F&&>(f); | |
103 | } | |
104 | }; | |
105 | ||
106 | constexpr make_overload_linearly_t overload_linearly{}; | |
107 | #endif | |
108 | BOOST_HANA_NAMESPACE_END | |
109 | ||
110 | #endif // !BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP |