2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_PREPARE_BUFFER_HPP
9 #define BEAST_PREPARE_BUFFER_HPP
11 #include <beast/config.hpp>
12 #include <boost/asio/buffer.hpp>
17 /** Return a shortened buffer.
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.
23 @param n The size of the returned buffer.
25 @param buffer The buffer to shorten. Ownership of the
26 underlying memory is not transferred.
28 @return A new buffer that points to the first `n` bytes
29 of the original buffer.
32 boost::asio::const_buffer
33 prepare_buffer(std::size_t n,
34 boost::asio::const_buffer buffer)
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)) };
42 /** Return a shortened buffer.
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.
48 @param n The size of the returned buffer.
50 @param buffer The buffer to shorten. Ownership of the
51 underlying memory is not transferred.
53 @return A new buffer that points to the first `n` bytes
54 of the original buffer.
57 boost::asio::mutable_buffer
58 prepare_buffer(std::size_t n,
59 boost::asio::mutable_buffer buffer)
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)) };