Line data Source code
1 : //
2 : // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.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 : // Official repository: https://github.com/cppalliance/buffers
8 : //
9 :
10 : #ifndef BOOST_BUFFERS_IMPL_CONST_BUFFER_SUBSPAN_HPP
11 : #define BOOST_BUFFERS_IMPL_CONST_BUFFER_SUBSPAN_HPP
12 :
13 : #include <boost/assert.hpp>
14 : #include <iterator>
15 :
16 : namespace boost {
17 : namespace buffers {
18 :
19 : class const_buffer_subspan::
20 : const_iterator
21 : {
22 : const_buffer_subspan const* s_ = nullptr;
23 : std::size_t i_ = 0;
24 :
25 : friend class const_buffer_subspan;
26 :
27 1116898 : const_iterator(
28 : const_buffer_subspan const& s,
29 : std::size_t i) noexcept
30 1116898 : : s_(&s)
31 1116898 : , i_(i)
32 : {
33 1116898 : }
34 :
35 : public:
36 : using value_type = const_buffer;
37 : using reference = const_buffer;
38 : using pointer = void;
39 : using difference_type = std::ptrdiff_t;
40 : using iterator_category =
41 : std::bidirectional_iterator_tag;
42 :
43 : const_iterator() = default;
44 : const_iterator(
45 : const_iterator const&) = default;
46 : const_iterator& operator=(
47 : const_iterator const&) = default;
48 :
49 : bool
50 1330880 : operator==(
51 : const_iterator const& other) const noexcept
52 : {
53 : return
54 2661760 : s_ == other.s_ &&
55 2661760 : i_ == other.i_;
56 : }
57 :
58 : bool
59 1330880 : operator!=(
60 : const_iterator const& other) const noexcept
61 : {
62 1330880 : return !(*this == other);
63 : }
64 :
65 : BOOST_BUFFERS_DECL
66 : reference
67 : operator*() const noexcept;
68 :
69 : const_iterator&
70 772407 : operator++() noexcept
71 : {
72 772407 : BOOST_ASSERT(i_ < s_->n_);
73 772407 : ++i_;
74 772407 : return *this;
75 : }
76 :
77 : const_iterator
78 12 : operator++(int) noexcept
79 : {
80 12 : auto temp = *this;
81 12 : ++(*this);
82 12 : return temp;
83 : }
84 :
85 : const_iterator&
86 24 : operator--() noexcept
87 : {
88 24 : BOOST_ASSERT(i_ > 0);
89 24 : --i_;
90 24 : return *this;
91 : }
92 :
93 : const_iterator
94 12 : operator--(int) noexcept
95 : {
96 12 : auto temp = *this;
97 12 : --(*this);
98 12 : return temp;
99 : }
100 : };
101 :
102 : //------------------------------------------------
103 :
104 263431 : const_buffer_subspan::
105 : const_buffer_subspan(
106 : const_buffer const* p,
107 : std::size_t n,
108 : std::size_t p0,
109 263431 : std::size_t p1) noexcept
110 263431 : : p_(p)
111 263431 : , n_(n)
112 263431 : , p0_(p0)
113 263431 : , p1_(p1)
114 : {
115 263431 : BOOST_ASSERT(
116 : n_ > 1 ||
117 : p1_ >= p0_);
118 263431 : BOOST_ASSERT(
119 : n_ == 0 ||
120 : p0 < p[0].size());
121 263431 : BOOST_ASSERT(
122 : n_ == 0 ||
123 : p1 <= p[n_ - 1].size());
124 263431 : }
125 :
126 : auto
127 558449 : const_buffer_subspan::
128 : begin() const noexcept ->
129 : const_iterator
130 : {
131 558449 : return { *this, 0 };
132 : }
133 :
134 : auto
135 558449 : const_buffer_subspan::
136 : end() const noexcept ->
137 : const_iterator
138 : {
139 558449 : return { *this, n_ };
140 : }
141 :
142 : } // buffers
143 : } // boost
144 :
145 : #endif
|