1 | // |
2 | // registered_buffer.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | |
11 | #ifndef BOOST_ASIO_REGISTERED_BUFFER_HPP |
12 | #define BOOST_ASIO_REGISTERED_BUFFER_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/config.hpp> |
19 | #include <boost/asio/buffer.hpp> |
20 | |
21 | #include <boost/asio/detail/push_options.hpp> |
22 | |
23 | namespace boost { |
24 | namespace asio { |
25 | namespace detail { |
26 | |
27 | class buffer_registration_base; |
28 | |
29 | } // namespace detail |
30 | |
31 | class const_registered_buffer; |
32 | |
33 | /// Type used to identify a registered buffer. |
34 | class registered_buffer_id |
35 | { |
36 | public: |
37 | /// The native buffer identifier type. |
38 | typedef int native_handle_type; |
39 | |
40 | /// Default constructor creates an invalid registered buffer identifier. |
41 | registered_buffer_id() noexcept |
42 | : scope_(0), |
43 | index_(-1) |
44 | { |
45 | } |
46 | |
47 | /// Get the native buffer identifier type. |
48 | native_handle_type native_handle() const noexcept |
49 | { |
50 | return index_; |
51 | } |
52 | |
53 | /// Compare two IDs for equality. |
54 | friend bool operator==(const registered_buffer_id& lhs, |
55 | const registered_buffer_id& rhs) noexcept |
56 | { |
57 | return lhs.scope_ == rhs.scope_ && lhs.index_ == rhs.index_; |
58 | } |
59 | |
60 | /// Compare two IDs for equality. |
61 | friend bool operator!=(const registered_buffer_id& lhs, |
62 | const registered_buffer_id& rhs) noexcept |
63 | { |
64 | return lhs.scope_ != rhs.scope_ || lhs.index_ != rhs.index_; |
65 | } |
66 | |
67 | private: |
68 | friend class detail::buffer_registration_base; |
69 | |
70 | // Hidden constructor used by buffer registration. |
71 | registered_buffer_id(const void* scope, int index) noexcept |
72 | : scope_(scope), |
73 | index_(index) |
74 | { |
75 | } |
76 | |
77 | const void* scope_; |
78 | int index_; |
79 | }; |
80 | |
81 | /// Holds a registered buffer over modifiable data. |
82 | /** |
83 | * Satisfies the @c MutableBufferSequence type requirements. |
84 | */ |
85 | class mutable_registered_buffer |
86 | { |
87 | public: |
88 | /// Default constructor creates an invalid registered buffer. |
89 | mutable_registered_buffer() noexcept |
90 | : buffer_(), |
91 | id_() |
92 | { |
93 | } |
94 | |
95 | /// Get the underlying mutable buffer. |
96 | const mutable_buffer& buffer() const noexcept |
97 | { |
98 | return buffer_; |
99 | } |
100 | |
101 | /// Get a pointer to the beginning of the memory range. |
102 | /** |
103 | * @returns <tt>buffer().data()</tt>. |
104 | */ |
105 | void* data() const noexcept |
106 | { |
107 | return buffer_.data(); |
108 | } |
109 | |
110 | /// Get the size of the memory range. |
111 | /** |
112 | * @returns <tt>buffer().size()</tt>. |
113 | */ |
114 | std::size_t size() const noexcept |
115 | { |
116 | return buffer_.size(); |
117 | } |
118 | |
119 | /// Get the registered buffer identifier. |
120 | const registered_buffer_id& id() const noexcept |
121 | { |
122 | return id_; |
123 | } |
124 | |
125 | /// Move the start of the buffer by the specified number of bytes. |
126 | mutable_registered_buffer& operator+=(std::size_t n) noexcept |
127 | { |
128 | buffer_ += n; |
129 | return *this; |
130 | } |
131 | |
132 | private: |
133 | friend class detail::buffer_registration_base; |
134 | |
135 | // Hidden constructor used by buffer registration and operators. |
136 | mutable_registered_buffer(const mutable_buffer& b, |
137 | const registered_buffer_id& i) noexcept |
138 | : buffer_(b), |
139 | id_(i) |
140 | { |
141 | } |
142 | |
143 | #if !defined(GENERATING_DOCUMENTATION) |
144 | friend mutable_registered_buffer buffer( |
145 | const mutable_registered_buffer& b, std::size_t n) noexcept; |
146 | #endif // !defined(GENERATING_DOCUMENTATION) |
147 | |
148 | mutable_buffer buffer_; |
149 | registered_buffer_id id_; |
150 | }; |
151 | |
152 | /// Holds a registered buffer over non-modifiable data. |
153 | /** |
154 | * Satisfies the @c ConstBufferSequence type requirements. |
155 | */ |
156 | class const_registered_buffer |
157 | { |
158 | public: |
159 | /// Default constructor creates an invalid registered buffer. |
160 | const_registered_buffer() noexcept |
161 | : buffer_(), |
162 | id_() |
163 | { |
164 | } |
165 | |
166 | /// Construct a non-modifiable buffer from a modifiable one. |
167 | const_registered_buffer( |
168 | const mutable_registered_buffer& b) noexcept |
169 | : buffer_(b.buffer()), |
170 | id_(b.id()) |
171 | { |
172 | } |
173 | |
174 | /// Get the underlying constant buffer. |
175 | const const_buffer& buffer() const noexcept |
176 | { |
177 | return buffer_; |
178 | } |
179 | |
180 | /// Get a pointer to the beginning of the memory range. |
181 | /** |
182 | * @returns <tt>buffer().data()</tt>. |
183 | */ |
184 | const void* data() const noexcept |
185 | { |
186 | return buffer_.data(); |
187 | } |
188 | |
189 | /// Get the size of the memory range. |
190 | /** |
191 | * @returns <tt>buffer().size()</tt>. |
192 | */ |
193 | std::size_t size() const noexcept |
194 | { |
195 | return buffer_.size(); |
196 | } |
197 | |
198 | /// Get the registered buffer identifier. |
199 | const registered_buffer_id& id() const noexcept |
200 | { |
201 | return id_; |
202 | } |
203 | |
204 | /// Move the start of the buffer by the specified number of bytes. |
205 | const_registered_buffer& operator+=(std::size_t n) noexcept |
206 | { |
207 | buffer_ += n; |
208 | return *this; |
209 | } |
210 | |
211 | private: |
212 | // Hidden constructor used by operators. |
213 | const_registered_buffer(const const_buffer& b, |
214 | const registered_buffer_id& i) noexcept |
215 | : buffer_(b), |
216 | id_(i) |
217 | { |
218 | } |
219 | |
220 | #if !defined(GENERATING_DOCUMENTATION) |
221 | friend const_registered_buffer buffer( |
222 | const const_registered_buffer& b, std::size_t n) noexcept; |
223 | #endif // !defined(GENERATING_DOCUMENTATION) |
224 | |
225 | const_buffer buffer_; |
226 | registered_buffer_id id_; |
227 | }; |
228 | |
229 | /** @addtogroup buffer_sequence_begin */ |
230 | |
231 | /// Get an iterator to the first element in a buffer sequence. |
232 | inline const mutable_buffer* buffer_sequence_begin( |
233 | const mutable_registered_buffer& b) noexcept |
234 | { |
235 | return &b.buffer(); |
236 | } |
237 | |
238 | /// Get an iterator to the first element in a buffer sequence. |
239 | inline const const_buffer* buffer_sequence_begin( |
240 | const const_registered_buffer& b) noexcept |
241 | { |
242 | return &b.buffer(); |
243 | } |
244 | |
245 | /** @} */ |
246 | /** @addtogroup buffer_sequence_end */ |
247 | |
248 | /// Get an iterator to one past the end element in a buffer sequence. |
249 | inline const mutable_buffer* buffer_sequence_end( |
250 | const mutable_registered_buffer& b) noexcept |
251 | { |
252 | return &b.buffer() + 1; |
253 | } |
254 | |
255 | /// Get an iterator to one past the end element in a buffer sequence. |
256 | inline const const_buffer* buffer_sequence_end( |
257 | const const_registered_buffer& b) noexcept |
258 | { |
259 | return &b.buffer() + 1; |
260 | } |
261 | |
262 | /** @} */ |
263 | /** @addtogroup buffer */ |
264 | |
265 | /// Obtain a buffer representing the entire registered buffer. |
266 | inline mutable_registered_buffer buffer( |
267 | const mutable_registered_buffer& b) noexcept |
268 | { |
269 | return b; |
270 | } |
271 | |
272 | /// Obtain a buffer representing the entire registered buffer. |
273 | inline const_registered_buffer buffer( |
274 | const const_registered_buffer& b) noexcept |
275 | { |
276 | return b; |
277 | } |
278 | |
279 | /// Obtain a buffer representing part of a registered buffer. |
280 | inline mutable_registered_buffer buffer( |
281 | const mutable_registered_buffer& b, std::size_t n) noexcept |
282 | { |
283 | return mutable_registered_buffer(buffer(b: b.buffer_, max_size_in_bytes: n), b.id_); |
284 | } |
285 | |
286 | /// Obtain a buffer representing part of a registered buffer. |
287 | inline const_registered_buffer buffer( |
288 | const const_registered_buffer& b, std::size_t n) noexcept |
289 | { |
290 | return const_registered_buffer(buffer(b: b.buffer_, max_size_in_bytes: n), b.id_); |
291 | } |
292 | |
293 | /** @} */ |
294 | |
295 | /// Create a new modifiable registered buffer that is offset from the start of |
296 | /// another. |
297 | /** |
298 | * @relates mutable_registered_buffer |
299 | */ |
300 | inline mutable_registered_buffer operator+( |
301 | const mutable_registered_buffer& b, std::size_t n) noexcept |
302 | { |
303 | mutable_registered_buffer tmp(b); |
304 | tmp += n; |
305 | return tmp; |
306 | } |
307 | |
308 | /// Create a new modifiable buffer that is offset from the start of another. |
309 | /** |
310 | * @relates mutable_registered_buffer |
311 | */ |
312 | inline mutable_registered_buffer operator+(std::size_t n, |
313 | const mutable_registered_buffer& b) noexcept |
314 | { |
315 | return b + n; |
316 | } |
317 | |
318 | /// Create a new non-modifiable registered buffer that is offset from the start |
319 | /// of another. |
320 | /** |
321 | * @relates const_registered_buffer |
322 | */ |
323 | inline const_registered_buffer operator+(const const_registered_buffer& b, |
324 | std::size_t n) noexcept |
325 | { |
326 | const_registered_buffer tmp(b); |
327 | tmp += n; |
328 | return tmp; |
329 | } |
330 | |
331 | /// Create a new non-modifiable buffer that is offset from the start of another. |
332 | /** |
333 | * @relates const_registered_buffer |
334 | */ |
335 | inline const_registered_buffer operator+(std::size_t n, |
336 | const const_registered_buffer& b) noexcept |
337 | { |
338 | return b + n; |
339 | } |
340 | |
341 | } // namespace asio |
342 | } // namespace boost |
343 | |
344 | #include <boost/asio/detail/pop_options.hpp> |
345 | |
346 | #endif // BOOST_ASIO_REGISTERED_BUFFER_HPP |
347 | |