summaryrefslogtreecommitdiff
path: root/boost/beast/core/impl/buffers_cat.ipp
blob: 9de1187f3608fdda54557ccf20f180bf5b34b56d (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_IMPL_BUFFERS_CAT_IPP
#define BOOST_BEAST_IMPL_BUFFERS_CAT_IPP

#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/beast/core/detail/variant.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/throw_exception.hpp>
#include <cstdint>
#include <iterator>
#include <new>
#include <stdexcept>
#include <tuple>
#include <utility>

namespace boost {
namespace beast {

template<class... Bn>
class buffers_cat_view<Bn...>::const_iterator
{
    // VFALCO The logic to skip empty sequences fails
    //        if there is just one buffer in the list.
    static_assert(sizeof...(Bn) >= 2,
        "A minimum of two sequences are required");

    struct past_end
    {
        char unused = 0; // make g++8 happy

        operator bool() const noexcept
        {
            return true;
        }
    };

    std::tuple<Bn...> const* bn_ = nullptr;
    detail::variant<typename
        detail::buffer_sequence_iterator<Bn>::type...,
            past_end> it_;

    friend class buffers_cat_view<Bn...>;

    template<std::size_t I>
    using C = std::integral_constant<std::size_t, I>;

public:
    using value_type = typename
        detail::common_buffers_type<Bn...>::type;
    using pointer = value_type const*;
    using reference = value_type;
    using difference_type = std::ptrdiff_t;
    using iterator_category =
        std::bidirectional_iterator_tag;

    const_iterator() = default;
    const_iterator(const_iterator&& other) = default;
    const_iterator(const_iterator const& other) = default;
    const_iterator& operator=(const_iterator&& other) = default;
    const_iterator& operator=(const_iterator const& other) = default;

    bool
    operator==(const_iterator const& other) const;

    bool
    operator!=(const_iterator const& other) const
    {
        return ! (*this == other);
    }

    reference
    operator*() const;

    pointer
    operator->() const = delete;

    const_iterator&
    operator++();

    const_iterator
    operator++(int);

    // deprecated
    const_iterator&
    operator--();

    // deprecated
    const_iterator
    operator--(int);

private:
    const_iterator(
        std::tuple<Bn...> const& bn, bool at_end);

    template<std::size_t I>
    void
    construct(C<I> const&)
    {
        if(boost::asio::buffer_size(
            std::get<I>(*bn_)) != 0)
        {
            it_.template emplace<I+1>(
                boost::asio::buffer_sequence_begin(
                    std::get<I>(*bn_)));
            return;
        }
        construct(C<I+1>{});
    }

    void
    construct(C<sizeof...(Bn)-1> const&)
    {
        auto constexpr I = sizeof...(Bn)-1;
        it_.template emplace<I+1>(
            boost::asio::buffer_sequence_begin(
                std::get<I>(*bn_)));
    }

    void
    construct(C<sizeof...(Bn)> const&)
    {
        // end
        auto constexpr I = sizeof...(Bn);
        it_.template emplace<I+1>();
    }

    template<std::size_t I>
    void
    next(C<I> const&)
    {
        if(boost::asio::buffer_size(
            std::get<I>(*bn_)) != 0)
        {
            it_.template emplace<I+1>(
                boost::asio::buffer_sequence_begin(
                    std::get<I>(*bn_)));
            return;
        }
        next(C<I+1>{});
    }

    void
    next(C<sizeof...(Bn)> const&)
    {
        // end
        auto constexpr I = sizeof...(Bn);
        it_.template emplace<I+1>();
    }

    template<std::size_t I>
    void
    prev(C<I> const&)
    {
        if(boost::asio::buffer_size(
            std::get<I>(*bn_)) != 0)
        {
            it_.template emplace<I+1>(
                boost::asio::buffer_sequence_end(
                    std::get<I>(*bn_)));
            return;
        }
        prev(C<I-1>{});
    }

    void
    prev(C<0> const&)
    {
        auto constexpr I = 0;
        it_.template emplace<I+1>(
            boost::asio::buffer_sequence_end(
                std::get<I>(*bn_)));
    }

    template<std::size_t I>
    reference
    dereference(C<I> const&) const
    {
        if(it_.index() == I+1)
            return *it_.template get<I+1>();
        return dereference(C<I+1>{});
    }

    [[noreturn]]
    reference
    dereference(C<sizeof...(Bn)> const&) const
    {
        BOOST_THROW_EXCEPTION(std::logic_error{
            "invalid iterator"});
    }

    template<std::size_t I>
    void
    increment(C<I> const&)
    {
        if(it_.index() == I+1)
        {
            if(++it_.template get<I+1>() !=
                boost::asio::buffer_sequence_end(
                    std::get<I>(*bn_)))
                return;
            return next(C<I+1>{});
        }
        increment(C<I+1>{});
    }

    [[noreturn]]
    void
    increment(C<sizeof...(Bn)> const&)
    {
        BOOST_THROW_EXCEPTION(std::logic_error{
            "invalid iterator"});
    }

    void
    decrement(C<sizeof...(Bn)> const&)
    {
        auto constexpr I = sizeof...(Bn);
        if(it_.index() == I+1)
            prev(C<I-1>{});
        decrement(C<I-1>{});
    }

    template<std::size_t I>
    void
    decrement(C<I> const&)
    {
        if(it_.index() == I+1)
        {
            if(it_.template get<I+1>() !=
                boost::asio::buffer_sequence_begin(
                    std::get<I>(*bn_)))
            {
                --it_.template get<I+1>();
                return;
            }
            prev(C<I-1>{});
        }
        decrement(C<I-1>{});
    }

    void
    decrement(C<0> const&)
    {
        auto constexpr I = 0;
        if(it_.template get<I+1>() !=
            boost::asio::buffer_sequence_begin(
                std::get<I>(*bn_)))
        {
            --it_.template get<I+1>();
            return;
        }
        BOOST_THROW_EXCEPTION(std::logic_error{
            "invalid iterator"});
    }
};

//------------------------------------------------------------------------------

template<class... Bn>
buffers_cat_view<Bn...>::
const_iterator::
const_iterator(
    std::tuple<Bn...> const& bn, bool at_end)
    : bn_(&bn)
{
    if(! at_end)
        construct(C<0>{});
    else
        construct(C<sizeof...(Bn)>{});
}

template<class... Bn>
bool
buffers_cat_view<Bn...>::
const_iterator::
operator==(const_iterator const& other) const
{
    return
        (bn_ == nullptr) ?
        (
            other.bn_ == nullptr ||
            other.it_.index() == sizeof...(Bn)
        ):(
            (other.bn_ == nullptr) ?
            (
                it_.index() == sizeof...(Bn)
            ): (
                bn_ == other.bn_ &&
                it_ == other.it_
            )
        );
}

template<class... Bn>
auto
buffers_cat_view<Bn...>::
const_iterator::
operator*() const ->
    reference
{
    return dereference(C<0>{});
}

template<class... Bn>
auto
buffers_cat_view<Bn...>::
const_iterator::
operator++() ->
    const_iterator&
{
    increment(C<0>{});
    return *this;
}

template<class... Bn>
auto
buffers_cat_view<Bn...>::
const_iterator::
operator++(int) ->
    const_iterator
{
    auto temp = *this;
    ++(*this);
    return temp;
}

template<class... Bn>
auto
buffers_cat_view<Bn...>::
const_iterator::
operator--() ->
    const_iterator&
{
    decrement(C<sizeof...(Bn)>{});
    return *this;
}

template<class... Bn>
auto
buffers_cat_view<Bn...>::
const_iterator::
operator--(int) ->
    const_iterator
{
    auto temp = *this;
    --(*this);
    return temp;
}

//------------------------------------------------------------------------------

template<class... Bn>
buffers_cat_view<Bn...>::
buffers_cat_view(Bn const&... bn)
    : bn_(bn...)
{
}


template<class... Bn>
inline
auto
buffers_cat_view<Bn...>::begin() const ->
    const_iterator
{
    return const_iterator{bn_, false};
}

template<class... Bn>
inline
auto
buffers_cat_view<Bn...>::end() const ->
    const_iterator
{
    return const_iterator{bn_, true};
}

} // beast
} // boost

#endif