]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/beast/core/impl/flat_static_buffer.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / impl / flat_static_buffer.ipp
CommitLineData
b32b8144
FG
1//
2// Copyright (c) 2016-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// Official repository: https://github.com/boostorg/beast
8//
9
10#ifndef BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP
11#define BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP
12
13#include <boost/beast/core/detail/type_traits.hpp>
14#include <boost/asio/buffer.hpp>
15#include <boost/throw_exception.hpp>
16#include <algorithm>
17#include <cstring>
18#include <iterator>
19#include <stdexcept>
20
21namespace boost {
22namespace beast {
23
24/* Memory is laid out thusly:
25
26 begin_ ..|.. in_ ..|.. out_ ..|.. last_ ..|.. end_
27*/
28
29inline
30auto
31flat_static_buffer_base::
32data() const ->
33 const_buffers_type
34{
35 return {in_, dist(in_, out_)};
36}
37
38inline
39void
40flat_static_buffer_base::
41reset()
42{
43 reset_impl();
44}
45
46inline
47auto
48flat_static_buffer_base::
49prepare(std::size_t n) ->
50 mutable_buffers_type
51{
52 return prepare_impl(n);
53}
54
55inline
56void
57flat_static_buffer_base::
58reset(void* p, std::size_t n)
59{
60 reset_impl(p, n);
61}
62
63template<class>
64void
65flat_static_buffer_base::
66reset_impl()
67{
68 in_ = begin_;
69 out_ = begin_;
70 last_ = begin_;
71}
72
73template<class>
74void
75flat_static_buffer_base::
76reset_impl(void* p, std::size_t n)
77{
78 begin_ =
79 reinterpret_cast<char*>(p);
80 in_ = begin_;
81 out_ = begin_;
82 last_ = begin_;
83 end_ = begin_ + n;
84}
85
86template<class>
87auto
88flat_static_buffer_base::
89prepare_impl(std::size_t n) ->
90 mutable_buffers_type
91{
92 if(n <= dist(out_, end_))
93 {
94 last_ = out_ + n;
95 return {out_, n};
96 }
97 auto const len = size();
98 if(n > capacity() - len)
99 BOOST_THROW_EXCEPTION(std::length_error{
100 "buffer overflow"});
101 if(len > 0)
102 std::memmove(begin_, in_, len);
103 in_ = begin_;
104 out_ = in_ + len;
105 last_ = out_ + n;
106 return {out_, n};
107}
108
109template<class>
110void
111flat_static_buffer_base::
112consume_impl(std::size_t n)
113{
114 if(n >= size())
115 {
116 in_ = begin_;
117 out_ = in_;
118 return;
119 }
120 in_ += n;
121}
122
123//------------------------------------------------------------------------------
124
125template<std::size_t N>
126flat_static_buffer<N>::
127flat_static_buffer(flat_static_buffer const& other)
128 : flat_static_buffer_base(buf_, N)
129{
130 using boost::asio::buffer_copy;
131 this->commit(buffer_copy(
132 this->prepare(other.size()), other.data()));
133}
134
135template<std::size_t N>
136auto
137flat_static_buffer<N>::
138operator=(flat_static_buffer const& other) ->
139 flat_static_buffer<N>&
140{
141 using boost::asio::buffer_copy;
142 this->consume(this->size());
143 this->commit(buffer_copy(
144 this->prepare(other.size()), other.data()));
145 return *this;
146}
147
148} // beast
149} // boost
150
151#endif