]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::tap`. | |
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_TAP_HPP | |
11 | #define BOOST_HANA_TAP_HPP | |
12 | ||
13 | #include <boost/hana/fwd/tap.hpp> | |
14 | ||
15 | #include <boost/hana/concept/monad.hpp> | |
16 | #include <boost/hana/config.hpp> | |
17 | #include <boost/hana/core/dispatch.hpp> | |
18 | #include <boost/hana/functional/partial.hpp> | |
19 | #include <boost/hana/lift.hpp> | |
20 | ||
21 | ||
22 | BOOST_HANA_NAMESPACE_BEGIN | |
23 | template <typename M> | |
24 | struct tap_t { | |
25 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
26 | static_assert(hana::Monad<M>::value, | |
27 | "hana::tap<M> requires 'M' to be a Monad"); | |
28 | #endif | |
29 | ||
30 | template <typename F> | |
31 | constexpr auto operator()(F&& f) const { | |
32 | using Tap = BOOST_HANA_DISPATCH_IF(tap_impl<M>, | |
33 | hana::Monad<M>::value | |
34 | ); | |
35 | ||
36 | return Tap::apply(static_cast<F&&>(f)); | |
37 | } | |
38 | }; | |
39 | ||
40 | namespace detail { | |
41 | template <typename M> | |
42 | struct tap_helper { | |
43 | template <typename F, typename X> | |
44 | constexpr auto operator()(F&& f, X&& x) const { | |
45 | (void)static_cast<F&&>(f)(x); | |
46 | return hana::lift<M>(static_cast<X&&>(x)); | |
47 | } | |
48 | }; | |
49 | } | |
50 | ||
51 | template <typename M, bool condition> | |
52 | struct tap_impl<M, when<condition>> : default_ { | |
53 | template <typename F> | |
54 | static constexpr auto apply(F&& f) | |
55 | { return hana::partial(detail::tap_helper<M>{}, static_cast<F&&>(f)); } | |
56 | }; | |
57 | BOOST_HANA_NAMESPACE_END | |
58 | ||
59 | #endif // !BOOST_HANA_TAP_HPP |