Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:58

0001 /*!
0002 @file
0003 Defines `boost::hana::overload_linearly`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP
0011 #define BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP
0012 
0013 #include <boost/hana/config.hpp>
0014 #include <boost/hana/detail/decay.hpp>
0015 
0016 #include <utility>
0017 
0018 
0019 namespace boost { namespace hana {
0020     //! @ingroup group-functional
0021     //! Call the first function that produces a valid call expression.
0022     //!
0023     //! Given functions `f1, ..., fn`, `overload_linearly(f1, ..., fn)` is
0024     //! a new function that calls the first `fk` producing a valid call
0025     //! expression with the given arguments. Specifically,
0026     //! @code
0027     //!     overload_linearly(f1, ..., fn)(args...) == fk(args...)
0028     //! @endcode
0029     //!
0030     //! where `fk` is the _first_ function such that `fk(args...)` is a valid
0031     //! expression.
0032     //!
0033     //!
0034     //! Example
0035     //! -------
0036     //! @include example/functional/overload_linearly.cpp
0037 #ifdef BOOST_HANA_DOXYGEN_INVOKED
0038     constexpr auto overload_linearly = [](auto&& f1, auto&& f2, ..., auto&& fn) {
0039         return [perfect-capture](auto&& ...x) -> decltype(auto) {
0040             return forwarded(fk)(forwarded(x)...);
0041         };
0042     };
0043 #else
0044     template <typename F, typename G>
0045     struct overload_linearly_t {
0046         F f;
0047         G g;
0048 
0049     private:
0050         template <typename ...Args, typename =
0051             decltype(std::declval<F const&>()(std::declval<Args>()...))>
0052         constexpr F const& which(int) const& { return f; }
0053 
0054         template <typename ...Args, typename =
0055             decltype(std::declval<F&>()(std::declval<Args>()...))>
0056         constexpr F& which(int) & { return f; }
0057 
0058         template <typename ...Args, typename =
0059             decltype(std::declval<F&&>()(std::declval<Args>()...))>
0060         constexpr F which(int) && { return static_cast<F&&>(f); }
0061 
0062         template <typename ...Args>
0063         constexpr G const& which(long) const& { return g; }
0064 
0065         template <typename ...Args>
0066         constexpr G& which(long) & { return g; }
0067 
0068         template <typename ...Args>
0069         constexpr G which(long) && { return static_cast<G&&>(g); }
0070 
0071     public:
0072         template <typename ...Args>
0073         constexpr decltype(auto) operator()(Args&& ...args) const&
0074         { return which<Args...>(int{})(static_cast<Args&&>(args)...); }
0075 
0076         template <typename ...Args>
0077         constexpr decltype(auto) operator()(Args&& ...args) &
0078         { return which<Args...>(int{})(static_cast<Args&&>(args)...); }
0079 
0080         template <typename ...Args>
0081         constexpr decltype(auto) operator()(Args&& ...args) &&
0082         { return which<Args...>(int{})(static_cast<Args&&>(args)...); }
0083     };
0084 
0085     struct make_overload_linearly_t {
0086         template <typename F, typename G>
0087         constexpr overload_linearly_t<
0088             typename detail::decay<F>::type,
0089             typename detail::decay<G>::type
0090         > operator()(F&& f, G&& g) const {
0091             return {static_cast<F&&>(f), static_cast<G&&>(g)};
0092         }
0093 
0094         template <typename F, typename G, typename ...H>
0095         constexpr decltype(auto) operator()(F&& f, G&& g, H&& ...h) const {
0096             return (*this)(static_cast<F&&>(f),
0097                     (*this)(static_cast<G&&>(g), static_cast<H&&>(h)...));
0098         }
0099 
0100         template <typename F>
0101         constexpr typename detail::decay<F>::type operator()(F&& f) const {
0102             return static_cast<F&&>(f);
0103         }
0104     };
0105 
0106     BOOST_HANA_INLINE_VARIABLE constexpr make_overload_linearly_t overload_linearly{};
0107 #endif
0108 }} // end namespace boost::hana
0109 
0110 #endif // !BOOST_HANA_FUNCTIONAL_OVERLOAD_LINEARLY_HPP