File: access.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 (304 lines) | stat: -rw-r--r-- 11,064 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2013-present
//  Copyright Casey Carter 2016
//
//  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_ITERATOR_ACCESS_HPP
#define RANGES_V3_ITERATOR_ACCESS_HPP

#include <iterator>
#include <type_traits>
#include <utility>

#include <std/detail/associated_types.hpp>

#include <meta/meta.hpp>

#include <concepts/concepts.hpp>

#include <range/v3/range_fwd.hpp>

#include <range/v3/utility/move.hpp>
#include <range/v3/utility/static_const.hpp>
#include <range/v3/utility/swap.hpp>

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

namespace ranges
{
    /// \addtogroup group-iterator
    /// @{

    /// \cond
    namespace detail
    {
        template<typename I,
#ifdef RANGES_WORKAROUND_MSVC_683388
                 typename R = meta::conditional_t<
                     std::is_pointer<uncvref_t<I>>::value &&
                         std::is_array<std::remove_pointer_t<uncvref_t<I>>>::value,
                     std::add_lvalue_reference_t<std::remove_pointer_t<uncvref_t<I>>>,
                     decltype(*std::declval<I &>())>,
#else
                 typename R = decltype(*std::declval<I &>()),
#endif
                 typename = R &>
        using iter_reference_t_ = R;

#if defined(RANGES_DEEP_STL_INTEGRATION) && RANGES_DEEP_STL_INTEGRATION && \
    !defined(RANGES_DOXYGEN_INVOKED)
        template<typename T>
        using iter_value_t_ =
            typename meta::conditional_t<
                is_std_iterator_traits_specialized_v<T>,
                std::iterator_traits<T>,
                indirectly_readable_traits<T>>::value_type;
#else
        template<typename T>
        using iter_value_t_ = typename indirectly_readable_traits<T>::value_type;
#endif
    } // namespace detail
    /// \endcond

    template<typename R>
    using iter_reference_t = detail::iter_reference_t_<R>;

    template<typename R>
    using iter_value_t = detail::iter_value_t_<uncvref_t<R>>;

    /// \cond
    namespace _iter_move_
    {
#if RANGES_BROKEN_CPO_LOOKUP
        void iter_move(); // unqualified name lookup block
#endif

        template<typename T>
        decltype(iter_move(std::declval<T>())) try_adl_iter_move_(int);

        template<typename T>
        void try_adl_iter_move_(long);

        template<typename T>
        RANGES_INLINE_VAR constexpr bool is_adl_indirectly_movable_v =
            !RANGES_IS_SAME(void, decltype(_iter_move_::try_adl_iter_move_<T>(42)));

        struct fn
        {
            // clang-format off
            template<typename I,
                     typename = detail::enable_if_t<is_adl_indirectly_movable_v<I &>>>
#ifndef RANGES_WORKAROUND_CLANG_23135
            constexpr
#endif // RANGES_WORKAROUND_CLANG_23135
            auto CPP_auto_fun(operator())(I &&i)(const)
            (
                return iter_move(i)
            )

            template<
                typename I,
                typename = detail::enable_if_t<!is_adl_indirectly_movable_v<I &>>,
                typename R = iter_reference_t<I>>
#ifndef RANGES_WORKAROUND_CLANG_23135
            constexpr
#endif // RANGES_WORKAROUND_CLANG_23135
            auto CPP_auto_fun(operator())(I &&i)(const)
            (
                return static_cast<aux::move_t<R>>(aux::move(*i))
            )
            // clang-format on
        };
    } // namespace _iter_move_
    /// \endcond

    RANGES_DEFINE_CPO(_iter_move_::fn, iter_move)

    /// \cond
    namespace detail
    {
        template<typename I, typename O>
        auto is_indirectly_movable_(I & (*i)(), O & (*o)(), iter_value_t<I> * v = nullptr)
            -> always_<std::true_type,
                       decltype(iter_value_t<I>(iter_move(i()))),
                       decltype(*v = iter_move(i())),
                       decltype(*o() = (iter_value_t<I> &&) * v),
                       decltype(*o() = iter_move(i()))>;
        template<typename I, typename O>
        auto is_indirectly_movable_(...) -> std::false_type;

        template<typename I, typename O>
        auto is_nothrow_indirectly_movable_(iter_value_t<I> * v) -> meta::bool_<
            noexcept(iter_value_t<I>(iter_move(std::declval<I &>()))) &&
            noexcept(*v = iter_move(std::declval<I &>())) &&
            noexcept(*std::declval<O &>() = (iter_value_t<I> &&) * v) &&
            noexcept(*std::declval<O &>() = iter_move(std::declval<I &>()))>;
        template<typename I, typename O>
        auto is_nothrow_indirectly_movable_(...) -> std::false_type;
    } // namespace detail
    /// \endcond

    template<typename I, typename O>
    RANGES_INLINE_VAR constexpr bool is_indirectly_movable_v =
        decltype(detail::is_indirectly_movable_<I, O>(nullptr, nullptr))::value;

    template<typename I, typename O>
    RANGES_INLINE_VAR constexpr bool is_nothrow_indirectly_movable_v =
        decltype(detail::is_nothrow_indirectly_movable_<I, O>(nullptr))::value;

    template<typename I, typename O>
    struct is_indirectly_movable : meta::bool_<is_indirectly_movable_v<I, O>>
    {};

    template<typename I, typename O>
    struct is_nothrow_indirectly_movable
      : meta::bool_<is_nothrow_indirectly_movable_v<I, O>>
    {};

    /// \cond
    namespace _iter_swap_
    {
        struct nope
        {};

        // Q: Should std::reference_wrapper be considered a proxy wrt swapping rvalues?
        // A: No. Its operator= is currently defined to reseat the references, so
        //    std::swap(ra, rb) already means something when ra and rb are (lvalue)
        //    reference_wrappers. That reseats the reference wrappers but leaves the
        //    referents unmodified. Treating rvalue reference_wrappers differently would
        //    be confusing.

        // Q: Then why is it OK to "re"-define swap for pairs and tuples of references?
        // A: Because as defined above, swapping an rvalue tuple of references has the
        //    same semantics as swapping an lvalue tuple of references. Rather than
        //    reseat the references, assignment happens *through* the references.

        // Q: But I have an iterator whose operator* returns an rvalue
        //    std::reference_wrapper<T>. How do I make it model indirectly_swappable?
        // A: With an overload of iter_swap.

        // Intentionally create an ambiguity with std::iter_swap, which is
        // unconstrained.
        template<typename T, typename U>
        nope iter_swap(T, U) = delete;

#ifdef RANGES_WORKAROUND_MSVC_895622
        nope iter_swap();
#endif

        template<typename T, typename U>
        decltype(iter_swap(std::declval<T>(), std::declval<U>())) try_adl_iter_swap_(int);

        template<typename T, typename U>
        nope try_adl_iter_swap_(long);

        // Test whether an overload of iter_swap for a T and a U can be found
        // via ADL with the iter_swap overload above participating in the
        // overload set. This depends on user-defined iter_swap overloads
        // being a better match than the overload in namespace std.
        template<typename T, typename U>
        RANGES_INLINE_VAR constexpr bool is_adl_indirectly_swappable_v =
            !RANGES_IS_SAME(nope, decltype(_iter_swap_::try_adl_iter_swap_<T, U>(42)));

        struct fn
        {
            // *If* a user-defined iter_swap is found via ADL, call that:
            template<typename T, typename U>
            constexpr detail::enable_if_t<is_adl_indirectly_swappable_v<T, U>> operator()(
                T && t, U && u) const noexcept(noexcept(iter_swap((T &&) t, (U &&) u)))
            {
                (void)iter_swap((T &&) t, (U &&) u);
            }

            // *Otherwise*, for readable types with swappable reference
            // types, call ranges::swap(*a, *b)
            template<typename I0, typename I1>
            constexpr detail::enable_if_t<
                !is_adl_indirectly_swappable_v<I0, I1> &&
                is_swappable_with<iter_reference_t<I0>, iter_reference_t<I1>>::value>
            operator()(I0 && a, I1 && b) const noexcept(noexcept(ranges::swap(*a, *b)))
            {
                ranges::swap(*a, *b);
            }

            // *Otherwise*, for readable types that are mutually
            // indirectly_movable_storable, implement as:
            //      iter_value_t<T0> tmp = iter_move(a);
            //      *a = iter_move(b);
            //      *b = std::move(tmp);
            template<typename I0, typename I1>
            constexpr detail::enable_if_t<
                !is_adl_indirectly_swappable_v<I0, I1> &&
                !is_swappable_with<iter_reference_t<I0>, iter_reference_t<I1>>::value &&
                is_indirectly_movable_v<I0, I1> && is_indirectly_movable_v<I1, I0>>
            operator()(I0 && a, I1 && b) const
                noexcept(is_nothrow_indirectly_movable_v<I0, I1> &&
                             is_nothrow_indirectly_movable_v<I1, I0>)
            {
                iter_value_t<I0> v0 = iter_move(a);
                *a = iter_move(b);
                *b = detail::move(v0);
            }
        };
    } // namespace _iter_swap_
    /// \endcond

    /// \relates _iter_swap_::fn
    RANGES_DEFINE_CPO(_iter_swap_::fn, iter_swap)

    /// \cond
    namespace detail
    {
        template<typename T, typename U>
        auto is_indirectly_swappable_(T & (*t)(), U & (*u)())
            -> detail::always_<std::true_type, decltype(iter_swap(t(), u()))>;
        template<typename T, typename U>
        auto is_indirectly_swappable_(...) -> std::false_type;

        template<typename T, typename U>
        auto is_nothrow_indirectly_swappable_(int)
            -> meta::bool_<noexcept(iter_swap(std::declval<T &>(), std::declval<U &>()))>;
        template<typename T, typename U>
        auto is_nothrow_indirectly_swappable_(long) -> std::false_type;
    } // namespace detail
    /// \endcond

    template<typename T, typename U>
    RANGES_INLINE_VAR constexpr bool is_indirectly_swappable_v =
        decltype(detail::is_indirectly_swappable_<T, U>(nullptr, nullptr))::value;

    template<typename T, typename U>
    RANGES_INLINE_VAR constexpr bool is_nothrow_indirectly_swappable_v =
        decltype(detail::is_nothrow_indirectly_swappable_<T, U>(0))::value;

    template<typename T, typename U>
    struct is_indirectly_swappable : meta::bool_<is_indirectly_swappable_v<T, U>>
    {};

    template<typename T, typename U>
    struct is_nothrow_indirectly_swappable
      : meta::bool_<is_nothrow_indirectly_swappable_v<T, U>>
    {};

    namespace cpp20
    {
        using ranges::iter_move;
        using ranges::iter_reference_t;
        using ranges::iter_swap;
        using ranges::iter_value_t;
    } // namespace cpp20
    /// @}
} // namespace ranges

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

#endif // RANGES_V3_ITERATOR_ACCESS_HPP