2 // Copyright (c) 2016-2019 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/static_buffer.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <boost/throw_exception.hpp>
23 void* p, std::size_t size) noexcept
24 : begin_(static_cast<char*>(p))
40 data() const noexcept ->
43 if(in_off_ + in_size_ <= capacity_)
46 begin_ + in_off_, in_size_},
51 begin_ + in_off_, capacity_ - in_off_},
53 begin_, in_size_ - (capacity_ - in_off_)}};
61 if(in_off_ + in_size_ <= capacity_)
64 begin_ + in_off_, in_size_},
69 begin_ + in_off_, capacity_ - in_off_},
71 begin_, in_size_ - (capacity_ - in_off_)}};
76 prepare(std::size_t n) ->
79 using net::mutable_buffer;
80 if(n > capacity_ - in_size_)
81 BOOST_THROW_EXCEPTION(std::length_error{
82 "static_buffer overflow"});
85 (in_off_ + in_size_) % capacity_;
86 if(out_off + out_size_ <= capacity_ )
89 begin_ + out_off, out_size_},
94 begin_ + out_off, capacity_ - out_off},
96 begin_, out_size_ - (capacity_ - out_off)}};
101 commit(std::size_t n) noexcept
103 in_size_ += (std::min)(n, out_size_);
109 consume(std::size_t n) noexcept
113 in_off_ = (in_off_ + n) % capacity_;
118 // rewind the offset, so the next call to prepare
119 // can have a longer contiguous segment. this helps
120 // algorithms optimized for larger buffers.