File: compose.hpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (104 lines) | stat: -rw-r--r-- 3,227 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2013-present
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
#ifndef RANGES_V3_FUNCTIONAL_COMPOSE_HPP
#define RANGES_V3_FUNCTIONAL_COMPOSE_HPP

#include <type_traits>
#include <utility>

#include <concepts/concepts.hpp>

#include <range/v3/detail/config.hpp>
#include <range/v3/functional/invoke.hpp>
#include <range/v3/utility/static_const.hpp>

#include <range/v3/detail/prologue.hpp>

namespace ranges
{
    /// \addtogroup group-functional
    /// @{
    template<typename Second, typename First>
    struct composed
    {
    private:
        RANGES_NO_UNIQUE_ADDRESS
        First first_;
        RANGES_NO_UNIQUE_ADDRESS
        Second second_;

        // clang-format off
        template<typename A, typename B, typename... Ts>
        static constexpr auto //
        CPP_auto_fun(do_)(A &&a, B &&b, std::false_type, Ts &&... ts)
        (
            return invoke((B &&) b, invoke((A &&) a, (Ts &&) ts...))
        )
        template<typename A, typename B, typename... Ts>
        static constexpr auto CPP_auto_fun(do_)(A &&a, B &&b, std::true_type, Ts &&... ts)
        (
            return (invoke((A &&) a, (Ts &&) ts...), invoke((B &&) b))
        )
    public:
        composed() = default;
        // clang-format on
        constexpr composed(Second second, First first)
          : first_(std::move(first))
          , second_(std::move(second))
        {}
        // clang-format off
        template<typename... Ts>
        constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(mutable &)
        (
            return composed::do_(first_,
                                 second_,
                                 std::is_void<invoke_result_t<First &, Ts...>>{},
                                 (Ts &&) ts...)
        )
        template<typename... Ts>
        constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(const &)
        (
            return composed::do_((First const &)first_,
                                 (Second const &)second_,
                                 std::is_void<invoke_result_t<First const &, Ts...>>{},
                                 (Ts &&) ts...)
        )
        template<typename... Ts>
        constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(mutable &&)
        (
            return composed::do_((First &&)first_,
                                 (Second &&)second_,
                                 std::is_void<invoke_result_t<First &&, Ts...>>{},
                                 (Ts &&) ts...)
        )
        // clang-format on
    };

    struct compose_fn
    {
        template<typename Second, typename First>
        constexpr composed<Second, First> operator()(Second second, First first) const
        {
            return {std::move(second), std::move(first)};
        }
    };

    /// \ingroup group-functional
    /// \sa `compose_fn`
    RANGES_INLINE_VARIABLE(compose_fn, compose)
    /// @}
} // namespace ranges

#include <range/v3/detail/epilogue.hpp>

#endif