ゴミ箱
flat_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_BUFFER_HPP
11 #define BOOST_BEAST_FLAT_BUFFER_HPP
12 
13 #include <boost/beast/config.hpp>
16 #include <boost/asio/buffer.hpp>
17 #include <limits>
18 #include <memory>
19 
20 namespace boost {
21 namespace beast {
22 
44 template<class Allocator>
46 #if ! BOOST_BEAST_DOXYGEN
48  typename detail::allocator_traits<Allocator>::
49  template rebind_alloc<char>>
50 #endif
51 {
52  enum
53  {
54  min_size = 512
55  };
56 
57  template<class OtherAlloc>
58  friend class basic_flat_buffer;
59 
60  using base_alloc_type = typename
62  template rebind_alloc<char>;
63 
64  using alloc_traits =
66 
67  static
68  inline
69  std::size_t
70  dist(char const* first, char const* last)
71  {
72  return static_cast<std::size_t>(last - first);
73  }
74 
75  char* begin_;
76  char* in_;
77  char* out_;
78  char* last_;
79  char* end_;
80  std::size_t max_;
81 
82 public:
84  using allocator_type = Allocator;
85 
87  using const_buffers_type = boost::asio::mutable_buffers_1;
88 
90  using mutable_buffers_type = boost::asio::mutable_buffers_1;
91 
94 
100 
107  explicit
108  basic_flat_buffer(std::size_t limit);
109 
116  explicit
117  basic_flat_buffer(Allocator const& alloc);
118 
128  std::size_t limit, Allocator const& alloc);
129 
139 
151  basic_flat_buffer&& other, Allocator const& alloc);
152 
157  basic_flat_buffer(basic_flat_buffer const& other);
158 
166  Allocator const& alloc);
167 
172  template<class OtherAlloc>
174  basic_flat_buffer<OtherAlloc> const& other);
175 
182  template<class OtherAlloc>
184  basic_flat_buffer<OtherAlloc> const& other,
185  Allocator const& alloc);
186 
196  operator=(basic_flat_buffer&& other);
197 
205  operator=(basic_flat_buffer const& other);
206 
213  template<class OtherAlloc>
216 
220  {
221  return this->member();
222  }
223 
225  std::size_t
226  size() const
227  {
228  return dist(in_, out_);
229  }
230 
232  std::size_t
233  max_size() const
234  {
235  return max_;
236  }
237 
239  std::size_t
240  capacity() const
241  {
242  return dist(begin_, end_);
243  }
244 
247  data() const
248  {
249  return {in_, dist(in_, out_)};
250  }
251 
260  prepare(std::size_t n);
261 
271  void
272  commit(std::size_t n)
273  {
274  out_ += (std::min)(n, dist(out_, last_));
275  }
276 
285  void
286  consume(std::size_t n);
287 
293  void
294  shrink_to_fit();
295 
297  template<class Alloc>
298  friend
299  void
300  swap(
303 
304 private:
305  void
306  reset();
307 
308  template<class DynamicBuffer>
309  void
310  copy_from(DynamicBuffer const& other);
311 
312  void
313  move_assign(basic_flat_buffer&, std::true_type);
314 
315  void
316  move_assign(basic_flat_buffer&, std::false_type);
317 
318  void
319  copy_assign(basic_flat_buffer const&, std::true_type);
320 
321  void
322  copy_assign(basic_flat_buffer const&, std::false_type);
323 
324  void
326 
327  void
328  swap(basic_flat_buffer&, std::true_type);
329 
330  void
331  swap(basic_flat_buffer&, std::false_type);
332 };
333 
334 using flat_buffer =
336 
337 } // beast
338 } // boost
339 
341 
342 #endif
~basic_flat_buffer()
Destructor.
Definition: flat_buffer.ipp:27
allocator_type get_allocator() const
Returns a copy of the associated allocator.
Definition: flat_buffer.hpp:219
mutable_buffers_type prepare(std::size_t n)
Definition: flat_buffer.ipp:240
Definition: async_result.hpp:20
void shrink_to_fit()
Definition: flat_buffer.ipp:304
basic_flat_buffer()
Definition: flat_buffer.ipp:36
std::size_t size() const
Returns the size of the input sequence.
Definition: flat_buffer.hpp:226
Definition: flat_buffer.hpp:45
friend void swap(basic_flat_buffer< Alloc > &lhs, basic_flat_buffer< Alloc > &rhs)
Exchange two flat buffers.
boost::asio::mutable_buffers_1 mutable_buffers_type
The type used to represent the output sequence as a list of buffers.
Definition: flat_buffer.hpp:90
basic_flat_buffer & operator=(basic_flat_buffer &&other)
Definition: flat_buffer.ipp:201
const_buffers_type data() const
Get a list of buffers that represent the input sequence.
Definition: flat_buffer.hpp:247
boost::asio::mutable_buffers_1 const_buffers_type
The type used to represent the input sequence as a list of buffers.
Definition: flat_buffer.hpp:87
void commit(std::size_t n)
Definition: flat_buffer.hpp:272
std::size_t max_size() const
Return the maximum sum of the input and output sequence sizes.
Definition: flat_buffer.hpp:233
std::allocator_traits< Alloc > allocator_traits
Definition: allocator.hpp:34
Allocator allocator_type
The type of allocator used.
Definition: flat_buffer.hpp:84
Definition: empty_base_optimization.hpp:35
detail::allocator_traits< Allocator >::template rebind_alloc< char > & member() noexcept
Definition: empty_base_optimization.hpp:48
std::size_t capacity() const
Return the maximum sum of input and output sizes that can be held without an allocation.
Definition: flat_buffer.hpp:240
void consume(std::size_t n)
Definition: flat_buffer.ipp:290