2 // Copyright (c) 2016-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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
11 #define BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
13 #include <boost/beast/core/detail/type_traits.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <boost/throw_exception.hpp>
26 static_buffer_base(void* p, std::size_t size)
27 : begin_(reinterpret_cast<char*>(p))
38 using boost::asio::mutable_buffer;
39 const_buffers_type result;
40 if(in_off_ + in_size_ <= capacity_)
42 result[0] = mutable_buffer{begin_ + in_off_, in_size_};
43 result[1] = mutable_buffer{begin_, 0};
47 result[0] = mutable_buffer{begin_ + in_off_, capacity_ - in_off_};
48 result[1] = mutable_buffer{begin_, in_size_ - (capacity_ - in_off_)};
56 prepare(std::size_t size) ->
59 using boost::asio::mutable_buffer;
60 if(size > capacity_ - in_size_)
61 BOOST_THROW_EXCEPTION(std::length_error{
64 auto const out_off = (in_off_ + in_size_) % capacity_;
65 mutable_buffers_type result;
66 if(out_off + out_size_ <= capacity_ )
68 result[0] = mutable_buffer{begin_ + out_off, out_size_};
69 result[1] = mutable_buffer{begin_, 0};
73 result[0] = mutable_buffer{begin_ + out_off, capacity_ - out_off};
74 result[1] = mutable_buffer{begin_, out_size_ - (capacity_ - out_off)};
82 commit(std::size_t size)
84 in_size_ += (std::min)(size, out_size_);
91 consume(std::size_t size)
95 in_off_ = (in_off_ + size) % capacity_;
100 // rewind the offset, so the next call to prepare
101 // can have a longer continguous segment. this helps
102 // algorithms optimized for larger buffesr.
111 reset(void* p, std::size_t size)
113 begin_ = reinterpret_cast<char*>(p);
120 //------------------------------------------------------------------------------
122 template<std::size_t N>
124 static_buffer(static_buffer const& other)
125 : static_buffer_base(buf_, N)
127 using boost::asio::buffer_copy;
128 this->commit(buffer_copy(
129 this->prepare(other.size()), other.data()));
132 template<std::size_t N>
135 operator=(static_buffer const& other) ->
138 using boost::asio::buffer_copy;
139 this->consume(this->size());
140 this->commit(buffer_copy(
141 this->prepare(other.size()), other.data()));