ゴミ箱
flat_static_buffer.hpp
Go to the documentation of this file.
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_FLAT_STATIC_BUFFER_HPP
11 #define BOOST_BEAST_FLAT_STATIC_BUFFER_HPP
12 
13 #include <boost/beast/config.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <algorithm>
16 #include <cstddef>
17 #include <cstring>
18 
19 namespace boost {
20 namespace beast {
21 
40 {
41  char* begin_;
42  char* in_;
43  char* out_;
44  char* last_;
45  char* end_;
46 
47  flat_static_buffer_base(flat_static_buffer_base const& other) = delete;
48  flat_static_buffer_base& operator=(flat_static_buffer_base const&) = delete;
49 
50 public:
55  using const_buffers_type = boost::asio::mutable_buffers_1;
56 
61  using mutable_buffers_type = boost::asio::mutable_buffers_1;
62 
71  flat_static_buffer_base(void* p, std::size_t n)
72  {
73  reset_impl(p, n);
74  }
75 
77  std::size_t
78  size() const
79  {
80  return out_ - in_;
81  }
82 
84  std::size_t
85  max_size() const
86  {
87  return dist(begin_, end_);
88  }
89 
91  std::size_t
92  capacity() const
93  {
94  return max_size();
95  }
96 
102  data() const;
103 
105  void
106  reset();
107 
117  prepare(std::size_t n);
118 
124  void
125  commit(std::size_t n)
126  {
127  out_ += std::min<std::size_t>(n, last_ - out_);
128  }
129 
131  void
132  consume(std::size_t n)
133  {
134  consume_impl(n);
135  }
136 
137 protected:
144  flat_static_buffer_base() = default;
145 
157  void
158  reset(void* p, std::size_t n);
159 
160 private:
161  static
162  inline
163  std::size_t
164  dist(char const* first, char const* last)
165  {
166  return static_cast<std::size_t>(last - first);
167  }
168 
169  template<class = void>
170  void
171  reset_impl();
172 
173  template<class = void>
174  void
175  reset_impl(void* p, std::size_t n);
176 
177  template<class = void>
179  prepare_impl(std::size_t n);
180 
181  template<class = void>
182  void
183  consume_impl(std::size_t n);
184 };
185 
186 //------------------------------------------------------------------------------
187 
202 template<std::size_t N>
204 {
205  char buf_[N];
206 
207 public:
210 
213  : flat_static_buffer_base(buf_, N)
214  {
215  }
216 
218  flat_static_buffer& operator=(flat_static_buffer const&);
219 
223  {
224  return *this;
225  }
226 
229  base() const
230  {
231  return *this;
232  }
233 
235  std::size_t constexpr
236  max_size() const
237  {
238  return N;
239  }
240 
242  std::size_t constexpr
243  capacity() const
244  {
245  return N;
246  }
247 };
248 
249 } // beast
250 } // boost
251 
253 
254 #endif
Definition: async_result.hpp:20
boost::asio::mutable_buffers_1 mutable_buffers_type
Definition: flat_static_buffer.hpp:61
flat_static_buffer_base const & base() const
Returns the flat_static_buffer_base portion of this object.
Definition: flat_static_buffer.hpp:229
mutable_buffers_type prepare(std::size_t n)
Definition: flat_static_buffer.ipp:49
boost::asio::mutable_buffers_1 const_buffers_type
Definition: flat_static_buffer.hpp:55
std::size_t constexpr capacity() const
Return the maximum sum of input and output sizes that can be held without an allocation.
Definition: flat_static_buffer.hpp:243
std::size_t size() const
Return the size of the input sequence.
Definition: flat_static_buffer.hpp:78
flat_static_buffer_base & base()
Returns the flat_static_buffer_base portion of this object.
Definition: flat_static_buffer.hpp:222
std::size_t capacity() const
Return the maximum sum of input and output sizes that can be held without an allocation.
Definition: flat_static_buffer.hpp:92
void consume(std::size_t n)
Remove bytes from the input sequence.
Definition: flat_static_buffer.hpp:132
void commit(std::size_t n)
Definition: flat_static_buffer.hpp:125
flat_static_buffer()
Constructor.
Definition: flat_static_buffer.hpp:212
std::size_t constexpr max_size() const
Return the maximum sum of the input and output sequence sizes.
Definition: flat_static_buffer.hpp:236
const_buffers_type data() const
Definition: flat_static_buffer.ipp:32
Definition: flat_static_buffer.hpp:203
Definition: flat_static_buffer.hpp:39
std::size_t max_size() const
Return the maximum sum of the input and output sequence sizes.
Definition: flat_static_buffer.hpp:85
flat_static_buffer_base(void *p, std::size_t n)
Definition: flat_static_buffer.hpp:71
void reset()
Set the input and output sequences to size 0.
Definition: flat_static_buffer.ipp:41