Files
boost_beast/include/beast/core/buffer_prefix.hpp
T

214 lines
6.1 KiB
C++
Raw Normal View History

2016-11-08 17:41:20 -05:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-11-08 17:41:20 -05:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_BUFFER_PREFIX_HPP
#define BEAST_BUFFER_PREFIX_HPP
2016-11-08 17:41:20 -05:00
#include <beast/config.hpp>
#include <beast/core/type_traits.hpp>
2017-07-06 21:17:57 -07:00
#include <beast/core/detail/in_place_init.hpp>
2016-11-08 17:41:20 -05:00
#include <boost/asio/buffer.hpp>
#include <cstdint>
#include <type_traits>
2016-11-08 17:41:20 -05:00
namespace beast {
2017-06-03 07:25:22 -07:00
/** A buffer sequence adapter that shortens the sequence size.
The class adapts a buffer sequence to efficiently represent
a shorter subset of the original list of buffers starting
with the first byte of the original sequence.
@tparam BufferSequence The buffer sequence to adapt.
*/
template<class BufferSequence>
class buffer_prefix_view
{
2017-07-06 20:58:07 -07:00
using buffers_type = typename
std::decay<BufferSequence>::type;
2017-07-12 09:02:55 -07:00
using iter_type =
typename buffers_type::const_iterator;
2017-06-03 07:25:22 -07:00
BufferSequence bs_;
std::size_t size_;
2017-07-12 09:02:55 -07:00
iter_type end_;
2017-06-03 07:25:22 -07:00
template<class Deduced>
2017-07-12 09:02:55 -07:00
buffer_prefix_view(
Deduced&& other, std::size_t dist)
2017-06-03 07:25:22 -07:00
: bs_(std::forward<Deduced>(other).bs_)
, size_(other.size_)
2017-07-12 09:02:55 -07:00
, end_(std::next(bs_.begin(), dist))
2017-06-03 07:25:22 -07:00
{
}
void
2017-07-12 09:02:55 -07:00
setup(std::size_t size);
2017-06-03 07:25:22 -07:00
public:
/// The type for each element in the list of buffers.
using value_type = typename std::conditional<
std::is_convertible<typename
std::iterator_traits<iter_type>::value_type,
boost::asio::mutable_buffer>::value,
boost::asio::mutable_buffer,
boost::asio::const_buffer>::type;
#if BEAST_DOXYGEN
/// A bidirectional iterator type that may be used to read elements.
using const_iterator = implementation_defined;
#else
class const_iterator;
#endif
/// Move constructor.
buffer_prefix_view(buffer_prefix_view&&);
/// Copy constructor.
buffer_prefix_view(buffer_prefix_view const&);
/// Move assignment.
buffer_prefix_view& operator=(buffer_prefix_view&&);
/// Copy assignment.
buffer_prefix_view& operator=(buffer_prefix_view const&);
2017-07-06 21:17:57 -07:00
/** Construct a buffer sequence prefix.
2017-06-03 07:25:22 -07:00
2017-07-12 07:05:27 -07:00
@param size The maximum number of bytes in the prefix.
2017-07-06 21:17:57 -07:00
If this is larger than the size of passed, buffers,
the resulting sequence will represent the entire
input sequence.
2017-06-03 07:25:22 -07:00
@param buffers The buffer sequence to adapt. A copy of
the sequence will be made, but ownership of the underlying
memory is not transferred.
*/
2017-07-12 09:02:55 -07:00
buffer_prefix_view(
std::size_t size,
BufferSequence const& buffers);
2017-06-03 07:25:22 -07:00
2017-07-06 21:17:57 -07:00
/** Construct a buffer sequence prefix in-place.
2017-07-12 09:02:55 -07:00
@param size The maximum number of bytes in the prefix.
2017-07-06 21:17:57 -07:00
If this is larger than the size of passed, buffers,
the resulting sequence will represent the entire
input sequence.
@param args Arguments forwarded to the contained buffers constructor.
*/
template<class... Args>
2017-07-12 09:02:55 -07:00
buffer_prefix_view(
std::size_t size,
boost::in_place_init_t,
Args&&... args);
2017-07-06 21:17:57 -07:00
2017-06-03 07:25:22 -07:00
/// Get a bidirectional iterator to the first element.
const_iterator
begin() const;
/// Get a bidirectional iterator to one past the last element.
const_iterator
end() const;
};
/** Returns a prefix of a constant buffer.
2016-11-08 17:41:20 -05:00
The returned buffer points to the same memory as the
passed buffer, but with a size that is equal to or less
than the size of the original buffer.
2017-02-06 20:07:03 -05:00
2017-07-12 09:02:55 -07:00
@param size The size of the returned buffer.
2016-11-08 17:41:20 -05:00
2017-06-03 07:25:22 -07:00
@param buffer The buffer to shorten. The underlying
memory is not modified.
2016-11-08 17:41:20 -05:00
2017-07-12 09:02:55 -07:00
@return A new buffer that points to the first `size`
bytes of the original buffer.
2016-11-08 17:41:20 -05:00
*/
inline
boost::asio::const_buffer
2017-07-12 09:02:55 -07:00
buffer_prefix(std::size_t size,
2016-11-08 17:41:20 -05:00
boost::asio::const_buffer buffer)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
return { buffer_cast<void const*>(buffer),
2017-07-12 09:02:55 -07:00
(std::min)(size, buffer_size(buffer)) };
2016-11-08 17:41:20 -05:00
}
2017-06-03 07:25:22 -07:00
/** Returns a prefix of a mutable buffer.
2016-11-08 17:41:20 -05:00
The returned buffer points to the same memory as the
passed buffer, but with a size that is equal to or less
than the size of the original buffer.
2017-02-06 20:07:03 -05:00
2017-07-12 09:02:55 -07:00
@param size The size of the returned buffer.
2016-11-08 17:41:20 -05:00
2017-06-03 07:25:22 -07:00
@param buffer The buffer to shorten. The underlying
memory is not modified.
2016-11-08 17:41:20 -05:00
2017-07-12 09:02:55 -07:00
@return A new buffer that points to the first `size` bytes
2016-11-08 17:41:20 -05:00
of the original buffer.
*/
inline
boost::asio::mutable_buffer
2017-07-12 09:02:55 -07:00
buffer_prefix(std::size_t size,
2016-11-08 17:41:20 -05:00
boost::asio::mutable_buffer buffer)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
2017-07-12 09:02:55 -07:00
return {buffer_cast<void*>(buffer),
(std::min)(size, buffer_size(buffer))};
2016-11-08 17:41:20 -05:00
}
/** Returns a prefix of a buffer sequence.
This function returns a new buffer sequence which when iterated,
presents a shorter subset of the original list of buffers starting
with the first byte of the original sequence.
2017-07-12 09:02:55 -07:00
@param size The maximum number of bytes in the wrapped
sequence. If this is larger than the size of passed,
buffers, the resulting sequence will represent the
entire input sequence.
@param buffers An instance of @b ConstBufferSequence or
2017-05-15 09:10:15 -07:00
@b MutableBufferSequence to adapt. A copy of the sequence
will be made, but ownership of the underlying memory is
not transferred.
*/
template<class BufferSequence>
#if BEAST_DOXYGEN
2017-06-03 07:25:22 -07:00
buffer_prefix_view<BufferSequence>
#else
inline
typename std::enable_if<
2017-07-12 09:02:55 -07:00
! std::is_same<BufferSequence,
boost::asio::const_buffer>::value &&
! std::is_same<BufferSequence,
boost::asio::mutable_buffer>::value,
buffer_prefix_view<BufferSequence>>::type
#endif
2017-07-12 09:02:55 -07:00
buffer_prefix(std::size_t size, BufferSequence const& buffers)
{
static_assert(
is_const_buffer_sequence<BufferSequence>::value ||
is_mutable_buffer_sequence<BufferSequence>::value,
"BufferSequence requirements not met");
2017-07-12 09:02:55 -07:00
return buffer_prefix_view<BufferSequence>(size, buffers);
}
2016-11-08 17:41:20 -05:00
} // beast
2017-06-03 07:25:22 -07:00
#include <beast/core/impl/buffer_prefix.ipp>
2016-11-08 17:41:20 -05:00
#endif