RESTinio
fixed_buffer.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
9 #pragma once
10 
11 #include <vector>
12 
14 
15 namespace restinio
16 {
17 
18 namespace impl
19 {
20 
21 //
22 // fixed_buffer_t
23 //
24 
27 {
28  public:
29  fixed_buffer_t( const fixed_buffer_t & ) = delete;
30  fixed_buffer_t & operator = ( const fixed_buffer_t & ) = delete;
33 
34  explicit fixed_buffer_t( std::size_t size )
35  {
36  m_buf.resize( size );
37  }
38 
40  auto
41  make_asio_buffer() noexcept
42  {
43  return asio_ns::buffer( m_buf.data(), m_buf.size() );
44  }
45 
47  void
48  obtained_bytes( std::size_t length ) noexcept
49  {
50  m_ready_length = length; // Current bytes in buffer.
51  m_ready_pos = 0; // Reset current pos.
52  }
53 
55  void
56  consumed_bytes( std::size_t length ) noexcept
57  {
58  m_ready_length -= length; // decrement buffer length.
59  m_ready_pos += length; // Shift current pos.
60  }
61 
63  std::size_t length() const noexcept { return m_ready_length; }
64 
66 
69  const char * bytes() const noexcept { return m_buf.data() + m_ready_pos; }
70 
71  private:
73  std::vector< char > m_buf;
74 
78  std::size_t m_ready_pos{0};
79 
81  std::size_t m_ready_length{0};
83 };
84 
85 } /* namespace impl */
86 
87 } /* namespace restinio */
restinio::impl::fixed_buffer_t::length
std::size_t length() const noexcept
How many unconsumed bytes are there in buffer.
Definition: fixed_buffer.hpp:63
restinio::impl::fixed_buffer_t
Helper class for reading bytes and feeding them to parser.
Definition: fixed_buffer.hpp:27
restinio::impl::fixed_buffer_t::fixed_buffer_t
fixed_buffer_t(fixed_buffer_t &&)=delete
asio_include.hpp
restinio::impl::fixed_buffer_t::fixed_buffer_t
fixed_buffer_t(std::size_t size)
Definition: fixed_buffer.hpp:34
restinio::impl::fixed_buffer_t::consumed_bytes
void consumed_bytes(std::size_t length) noexcept
Mark how many bytes were obtained.
Definition: fixed_buffer.hpp:56
restinio::impl::fixed_buffer_t::m_buf
std::vector< char > m_buf
Buffer for io operation.
Definition: fixed_buffer.hpp:73
restinio::impl::fixed_buffer_t::fixed_buffer_t
fixed_buffer_t(const fixed_buffer_t &)=delete
restinio::impl::fixed_buffer_t::obtained_bytes
void obtained_bytes(std::size_t length) noexcept
Mark how many bytes were obtained.
Definition: fixed_buffer.hpp:48
restinio::impl::fixed_buffer_t::make_asio_buffer
auto make_asio_buffer() noexcept
Make asio buffer for reading bytes from socket.
Definition: fixed_buffer.hpp:41
restinio
Definition: asio_include.hpp:21
restinio::impl::fixed_buffer_t::bytes
const char * bytes() const noexcept
Get pointer to unconsumed bytes.
Definition: fixed_buffer.hpp:69
restinio::impl::fixed_buffer_t::m_ready_pos
std::size_t m_ready_pos
unconsumed data left in buffer:Start of data in buffer.
Definition: fixed_buffer.hpp:78
restinio::impl::fixed_buffer_t::m_ready_length
std::size_t m_ready_length
Data size.
Definition: fixed_buffer.hpp:81
restinio::impl::fixed_buffer_t::operator=
fixed_buffer_t & operator=(const fixed_buffer_t &)=delete
const
#define const
Definition: zconf.h:230