]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/include/beast/core/prepare_buffer.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / include / beast / core / prepare_buffer.hpp
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_PREPARE_BUFFER_HPP
9 #define BEAST_PREPARE_BUFFER_HPP
10
11 #include <beast/config.hpp>
12 #include <boost/asio/buffer.hpp>
13 #include <algorithm>
14
15 namespace beast {
16
17 /** Return a shortened buffer.
18
19 The returned buffer points to the same memory as the
20 passed buffer, but with a size that is equal to or less
21 than the size of the original buffer.
22
23 @param n The size of the returned buffer.
24
25 @param buffer The buffer to shorten. Ownership of the
26 underlying memory is not transferred.
27
28 @return A new buffer that points to the first `n` bytes
29 of the original buffer.
30 */
31 inline
32 boost::asio::const_buffer
33 prepare_buffer(std::size_t n,
34 boost::asio::const_buffer buffer)
35 {
36 using boost::asio::buffer_cast;
37 using boost::asio::buffer_size;
38 return { buffer_cast<void const*>(buffer),
39 (std::min)(n, buffer_size(buffer)) };
40 }
41
42 /** Return a shortened buffer.
43
44 The returned buffer points to the same memory as the
45 passed buffer, but with a size that is equal to or less
46 than the size of the original buffer.
47
48 @param n The size of the returned buffer.
49
50 @param buffer The buffer to shorten. Ownership of the
51 underlying memory is not transferred.
52
53 @return A new buffer that points to the first `n` bytes
54 of the original buffer.
55 */
56 inline
57 boost::asio::mutable_buffer
58 prepare_buffer(std::size_t n,
59 boost::asio::mutable_buffer buffer)
60 {
61 using boost::asio::buffer_cast;
62 using boost::asio::buffer_size;
63 return { buffer_cast<void*>(buffer),
64 (std::min)(n, buffer_size(buffer)) };
65 }
66
67 } // beast
68
69 #endif