summaryrefslogtreecommitdiff
path: root/boost/beast/core/impl/buffers_prefix.ipp
blob: c595455d60c30cb5ed8ba9d0f15ea07d19f7bba4 (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
//
// 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_PREFIX_IPP
#define BOOST_BEAST_IMPL_BUFFERS_PREFIX_IPP

#include <algorithm>
#include <cstdint>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>

namespace boost {
namespace beast {

namespace detail {

inline
boost::asio::const_buffer
buffers_prefix(std::size_t size,
    boost::asio::const_buffer buffer)
{
    return {buffer.data(),
        (std::min)(size, buffer.size())};
}

inline
boost::asio::mutable_buffer
buffers_prefix(std::size_t size,
    boost::asio::mutable_buffer buffer)
{
    return {buffer.data(),
        (std::min)(size, buffer.size())};
}

} // detail

template<class BufferSequence>
class buffers_prefix_view<BufferSequence>::const_iterator
{
    friend class buffers_prefix_view<BufferSequence>;

    buffers_prefix_view const* b_ = nullptr;
    std::size_t remain_;
    iter_type it_;

public:
    using value_type = typename std::conditional<
        boost::is_convertible<typename
            std::iterator_traits<iter_type>::value_type,
                boost::asio::mutable_buffer>::value,
                    boost::asio::mutable_buffer,
                        boost::asio::const_buffer>::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
    {
        return
            (b_ == nullptr) ?
            (
                other.b_ == nullptr ||
                other.it_ == other.b_->end_
            ):(
                (other.b_ == nullptr) ?
                (
                    it_ == b_->end_
                ): (
                    b_ == other.b_ &&
                    it_ == other.it_
                )
            );
    }

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

    reference
    operator*() const
    {
        return detail::buffers_prefix(remain_, *it_);
    }

    pointer
    operator->() const = delete;

    const_iterator&
    operator++()
    {
        remain_ -= boost::asio::buffer_size(*it_++);
        return *this;
    }

    const_iterator
    operator++(int)
    {
        auto temp = *this;
        remain_ -= boost::asio::buffer_size(*it_++);
        return temp;
    }

    const_iterator&
    operator--()
    {
        remain_ += boost::asio::buffer_size(*--it_);
        return *this;
    }

    const_iterator
    operator--(int)
    {
        auto temp = *this;
        remain_ += boost::asio::buffer_size(*--it_);
        return temp;
    }

private:
    const_iterator(buffers_prefix_view const& b,
            std::true_type)
        : b_(&b)
        , remain_(b.remain_)
        , it_(b_->end_)
    {
    }

    const_iterator(buffers_prefix_view const& b,
            std::false_type)
        : b_(&b)
        , remain_(b_->size_)
        , it_(boost::asio::buffer_sequence_begin(b_->bs_))
    {
    }
};

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

template<class BufferSequence>
void
buffers_prefix_view<BufferSequence>::
setup(std::size_t size)
{
    size_ = 0;
    remain_ = 0;
    end_ = boost::asio::buffer_sequence_begin(bs_);
    auto const last = bs_.end();
    while(end_ != last)
    {
        auto const len =
            boost::asio::buffer_size(*end_++);
        if(len >= size)
        {
            size_ += size;
            remain_ = size - len;
            break;
        }
        size -= len;
        size_ += len;
    }
}

template<class BufferSequence>
buffers_prefix_view<BufferSequence>::
buffers_prefix_view(buffers_prefix_view&& other)
    : buffers_prefix_view(std::move(other),
        std::distance<iter_type>(
            boost::asio::buffer_sequence_begin(other.bs_),
            other.end_))
{
}

template<class BufferSequence>
buffers_prefix_view<BufferSequence>::
buffers_prefix_view(buffers_prefix_view const& other)
    : buffers_prefix_view(other,
        std::distance<iter_type>(
            boost::asio::buffer_sequence_begin(other.bs_),
                other.end_))
{
}

template<class BufferSequence>
auto
buffers_prefix_view<BufferSequence>::
operator=(buffers_prefix_view&& other) ->
    buffers_prefix_view&
{
    auto const dist = std::distance<iter_type>(
        boost::asio::buffer_sequence_begin(other.bs_),
        other.end_);
    bs_ = std::move(other.bs_);
    size_ = other.size_;
    remain_ = other.remain_;
    end_ = std::next(
        boost::asio::buffer_sequence_begin(bs_),
            dist);
    return *this;
}

template<class BufferSequence>
auto
buffers_prefix_view<BufferSequence>::
operator=(buffers_prefix_view const& other) ->
    buffers_prefix_view&
{
    auto const dist = std::distance<iter_type>(
        boost::asio::buffer_sequence_begin(other.bs_),
        other.end_);
    bs_ = other.bs_;
    size_ = other.size_;
    remain_ = other.remain_;
    end_ = std::next(
        boost::asio::buffer_sequence_begin(bs_),
            dist);
    return *this;
}

template<class BufferSequence>
buffers_prefix_view<BufferSequence>::
buffers_prefix_view(std::size_t size,
        BufferSequence const& bs)
    : bs_(bs)
{
    setup(size);
}

template<class BufferSequence>
template<class... Args>
buffers_prefix_view<BufferSequence>::
buffers_prefix_view(std::size_t size,
        boost::in_place_init_t, Args&&... args)
    : bs_(std::forward<Args>(args)...)
{
    setup(size);
}

template<class BufferSequence>
inline
auto
buffers_prefix_view<BufferSequence>::begin() const ->
    const_iterator
{
    return const_iterator{*this, std::false_type{}};
}

template<class BufferSequence>
inline
auto
buffers_prefix_view<BufferSequence>::end() const ->
    const_iterator
{
    return const_iterator{*this, std::true_type{}};
}

} // beast
} // boost

#endif