]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_CONSUMING_BUFFERS_HPP | |
9 | #define BEAST_CONSUMING_BUFFERS_HPP | |
10 | ||
11 | #include <beast/config.hpp> | |
12 | #include <beast/core/buffer_concepts.hpp> | |
13 | #include <boost/asio/buffer.hpp> | |
14 | #include <cstdint> | |
15 | #include <iterator> | |
16 | #include <type_traits> | |
17 | #include <utility> | |
18 | ||
19 | namespace beast { | |
20 | ||
21 | /** Adapter to trim the front of a `BufferSequence`. | |
22 | ||
23 | This adapter wraps a buffer sequence to create a new sequence | |
24 | which may be incrementally consumed. Bytes consumed are removed | |
25 | from the front of the buffer. The underlying memory is not changed, | |
26 | instead the adapter efficiently iterates through a subset of | |
27 | the buffers wrapped. | |
28 | ||
29 | The wrapped buffer is not modified, a copy is made instead. | |
30 | Ownership of the underlying memory is not transferred, the application | |
31 | is still responsible for managing its lifetime. | |
32 | ||
33 | @tparam BufferSequence The buffer sequence to wrap. | |
34 | */ | |
35 | template<class BufferSequence> | |
36 | class consuming_buffers | |
37 | { | |
38 | using iter_type = | |
39 | typename BufferSequence::const_iterator; | |
40 | ||
41 | BufferSequence bs_; | |
42 | iter_type begin_; | |
43 | iter_type end_; | |
44 | std::size_t skip_ = 0; | |
45 | ||
46 | template<class Deduced> | |
47 | consuming_buffers(Deduced&& other, std::size_t nbegin) | |
48 | : bs_(std::forward<Deduced>(other).bs_) | |
49 | , begin_(std::next(bs_.begin(), nbegin)) | |
50 | , skip_(other.skip_) | |
51 | { | |
52 | } | |
53 | ||
54 | public: | |
55 | /** The type for each element in the list of buffers. | |
56 | ||
57 | If the buffers in the underlying sequence are convertible to | |
58 | `boost::asio::mutable_buffer`, then this type will be | |
59 | `boost::asio::mutable_buffer`, else this type will be | |
60 | `boost::asio::const_buffer`. | |
61 | */ | |
62 | #if BEAST_DOXYGEN | |
63 | using value_type = ...; | |
64 | #else | |
65 | using value_type = typename std::conditional< | |
66 | std::is_convertible<typename | |
67 | std::iterator_traits<iter_type>::value_type, | |
68 | boost::asio::mutable_buffer>::value, | |
69 | boost::asio::mutable_buffer, | |
70 | boost::asio::const_buffer>::type; | |
71 | #endif | |
72 | ||
73 | #if BEAST_DOXYGEN | |
74 | /// A bidirectional iterator type that may be used to read elements. | |
75 | using const_iterator = implementation_defined; | |
76 | ||
77 | #else | |
78 | class const_iterator; | |
79 | ||
80 | #endif | |
81 | ||
82 | /// Move constructor. | |
83 | consuming_buffers(consuming_buffers&&); | |
84 | ||
85 | /// Copy constructor. | |
86 | consuming_buffers(consuming_buffers const&); | |
87 | ||
88 | /// Move assignment. | |
89 | consuming_buffers& operator=(consuming_buffers&&); | |
90 | ||
91 | /// Copy assignment. | |
92 | consuming_buffers& operator=(consuming_buffers const&); | |
93 | ||
94 | /** Construct to represent a buffer sequence. | |
95 | ||
96 | A copy of the buffer sequence is made. Ownership of the | |
97 | underlying memory is not transferred or copied. | |
98 | */ | |
99 | explicit | |
100 | consuming_buffers(BufferSequence const& buffers); | |
101 | ||
102 | /// Get a bidirectional iterator to the first element. | |
103 | const_iterator | |
104 | begin() const; | |
105 | ||
106 | /// Get a bidirectional iterator to one past the last element. | |
107 | const_iterator | |
108 | end() const; | |
109 | ||
110 | /** Remove bytes from the beginning of the sequence. | |
111 | ||
112 | @param n The number of bytes to remove. If this is | |
113 | larger than the number of bytes remaining, all the | |
114 | bytes remaining are removed. | |
115 | */ | |
116 | void | |
117 | consume(std::size_t n); | |
118 | }; | |
119 | ||
120 | } // beast | |
121 | ||
122 | #include <beast/core/impl/consuming_buffers.ipp> | |
123 | ||
124 | #endif |