RESTinio

Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
restinio::writable_item_t Class Reference

Class for storing the buffers used for streaming body (request/response). More...

`#include <buffers.hpp>`

## Public Member Functions
| writable_item_t (const writable_item_t &)=delete
writable_item_t & | operator= (const writable_item_t &)=delete
| writable_item_t ()
| writable_item_t (const_buffer_t const_buf)
template<typename Datasizeable , typename S = typename std::enable_if_t< !std::is_same< std::vector< writable_item_t >, Datasizeable >::value >>
| writable_item_t (Datasizeable ds)
| writable_item_t (const char *str)
template<typename Datasizeable >
| writable_item_t (std::shared_ptr< Datasizeable > sp)
| writable_item_t (sendfile_t sf_opts)
| writable_item_t (writable_item_t &&b)
writable_item_t & | operator= (writable_item_t &&b)
| ~writable_item_t ()
writable_item_type_t | write_type () const noexcept
| Get a type of a stored buffer object. More...
std::size_t | size () const
| Get the size of the underlying buffer object. More...
asio_ns::const_buffer | buf () const
| Create a buf reference object used by ASIO. More...
sendfile_t & | sendfile_operation ()
| Get a reference to a sendfile operation. More...

## Private Types
using | storage_t = std::aligned_storage_t< impl::needed_storage_max_size, impl::buffer_storage_align >

## Private Member Functions
void | destroy_stored_buffer ()
Access an item as an object of specific types.
Casts a stored object to one of the types.
const impl::writable_base_t * | get_writable_base () const noexcept
| Access as writable_base_t item. More...
impl::writable_base_t * | get_writable_base () noexcept
| Access as writable_base_t item. More...
const impl::buf_iface_t * | get_buf () const noexcept
| Access as trivial buf item. More...
impl::buf_iface_t * | get_buf () noexcept
| Access as trivial buf item. More...
impl::sendfile_write_operation_t * | get_sfwo () noexcept
| Access as sendfile_write_operation_t item. More...

## Private Attributes
writable_item_type_t | m_write_type
storage_t | m_storage
| A storage for a buffer object of various types. More...

## Detailed Description

Class for storing the buffers used for streaming body (request/response).

Supporting different types of entities that eventually result in output data sent to peer is a bit tricky. In the first step RESTionio distinguish two types of output data sources:

- trivial buffers (those ones that can be presented as a pair of a pointer to data and the size of the data).
- sendfile (send a piece of data from file utilizing native sendfile support Linux/FreeBSD/macOS and TransmitFile on windows).

Also trivial buffers are implemented diferently for different cases, includeing a template classes `impl::datasizeable_buf_t<Datasizeable>` and `impl::shared_datasizeable_buf_t<Datasizeable>`.

When using RESTinio response builder, response body can be constructed using different types of buffers, and once the result is flushed to the connection it must be sent to the socket. A couple of issues arises here. And one of them is how to store all these heterogeneous buffers with fewer allocations and less boilerplate necessary to handle each corner case. Storing and moving a bunch of buffers as a vector would be nice, but vector demands a single type to be used. And writable_item_t represents a custom variant-like abstraction for storing an arbitrary buffer object (`std::variant` itself is not the option as it is restricted to types defined beforehand and cannot benifit from the knowledge that all stored items are types derrived from impl::writable_base_t).

For storing the data of buffers m_storage is used. It is an aligned buffer sufficient to store any impl::writable_base_t descendant (there is a limitation concerned with impl::datasizeable_buf_t). Also writable_item_t exposes interface to treat it like a trivial buffer or a sendfile operation. The type of buffer currently stored in writable_item_t instance a write_type() function used.

Having such writable_item_t class, RESTinio can store a sequence of arbitrary buffers in `std::vector`.

Examples
  sample/websocket_detailed/main.cpp.

Definition at line 495 of file buffers.hpp.

## Member Typedef Documentation

## ◆ storage_t

using restinio::writable_item_t::storage_t = std::aligned_storage_t< impl::needed_storage_max_size, impl::buffer_storage_align >
| private

Definition at line 655 of file buffers.hpp.

## Constructor & Destructor Documentation

## ◆ writable_item_t() [1/8]

restinio::writable_item_t::writable_item_t | ( | const writable_item_t & |  | ) |
| delete

## ◆ writable_item_t() [2/8]

restinio::writable_item_t::writable_item_t | ( |  | ) |
| inline

Definition at line 501 of file buffers.hpp.

## ◆ writable_item_t() [3/8]

restinio::writable_item_t::writable_item_t | ( | const_buffer_t | const_buf | ) |
| inline

Definition at line 507 of file buffers.hpp.

## ◆ writable_item_t() [4/8]

template<typename Datasizeable , typename S = typename std::enable_if_t< !std::is_same< std::vector< writable_item_t >, Datasizeable >::value >>

restinio::writable_item_t::writable_item_t | ( | Datasizeable | ds | ) |
| inline

Definition at line 520 of file buffers.hpp.

## ◆ writable_item_t() [5/8]

restinio::writable_item_t::writable_item_t | ( | const char * | str | ) |
| inline

Definition at line 530 of file buffers.hpp.

## ◆ writable_item_t() [6/8]

template<typename Datasizeable >

restinio::writable_item_t::writable_item_t | ( | std::shared_ptr< Datasizeable > | sp | ) |
| inline

Definition at line 537 of file buffers.hpp.

## ◆ writable_item_t() [7/8]

restinio::writable_item_t::writable_item_t | ( | sendfile_t | sf_opts | ) |
| inline

Definition at line 550 of file buffers.hpp.

## ◆ writable_item_t() [8/8]

restinio::writable_item_t::writable_item_t | ( | writable_item_t && | b | ) |
| inline

Definition at line 556 of file buffers.hpp.

## ◆ ~writable_item_t()

restinio::writable_item_t::~writable_item_t | ( |  | ) |
| inline

Definition at line 575 of file buffers.hpp.

## Member Function Documentation

## ◆ buf()

asio_ns::const_buffer restinio::writable_item_t::buf | ( |  | ) | const
| inline

Create a buf reference object used by ASIO.

Note
  Stored buffer must be of writable_item_type_t::trivial_write_operation.

Definition at line 594 of file buffers.hpp.

## ◆ destroy_stored_buffer()

void restinio::writable_item_t::destroy_stored_buffer | ( |  | ) |
| inline private

Definition at line 611 of file buffers.hpp.

## ◆ get_buf() [1/2]

const impl::buf_iface_t * restinio::writable_item_t::get_buf | ( |  | ) | const
| inline private noexcept

Access as trivial buf item.

Definition at line 637 of file buffers.hpp.

## ◆ get_buf() [2/2]

impl::buf_iface_t * restinio::writable_item_t::get_buf | ( |  | ) |
| inline private noexcept

Access as trivial buf item.

Definition at line 643 of file buffers.hpp.

## ◆ get_sfwo()

impl::sendfile_write_operation_t * restinio::writable_item_t::get_sfwo | ( |  | ) |
| inline private noexcept

Access as sendfile_write_operation_t item.

Definition at line 649 of file buffers.hpp.

## ◆ get_writable_base() [1/2]

const impl::writable_base_t * restinio::writable_item_t::get_writable_base | ( |  | ) | const
| inline private noexcept

Access as writable_base_t item.

Definition at line 625 of file buffers.hpp.

## ◆ get_writable_base() [2/2]

impl::writable_base_t * restinio::writable_item_t::get_writable_base | ( |  | ) |
| inline private noexcept

Access as writable_base_t item.

Definition at line 631 of file buffers.hpp.

## ◆ operator=() [1/2]

writable_item_t & restinio::writable_item_t::operator= | ( | const writable_item_t & |  | ) |
| delete

## ◆ operator=() [2/2]

writable_item_t & restinio::writable_item_t::operator= | ( | writable_item_t && | b | ) |
| inline

Definition at line 563 of file buffers.hpp.

## ◆ sendfile_operation()

sendfile_t & restinio::writable_item_t::sendfile_operation | ( |  | ) |
| inline

Get a reference to a sendfile operation.

Note
  Stored buffer must be of writable_item_type_t::file_write_operation.
  Since v.0.4.9 it is non-const method. It is because we have to work with mutable sendfile_t on some platform (like Windows).

Definition at line 604 of file buffers.hpp.

## ◆ size()

std::size_t restinio::writable_item_t::size | ( |  | ) | const
| inline

Get the size of the underlying buffer object.

Definition at line 588 of file buffers.hpp.

## ◆ write_type()

writable_item_type_t restinio::writable_item_t::write_type | ( |  | ) | const
| inline noexcept

Get a type of a stored buffer object.

Definition at line 582 of file buffers.hpp.

## Member Data Documentation

## ◆ m_storage

storage_t restinio::writable_item_t::m_storage
| private

A storage for a buffer object of various types.

Definition at line 661 of file buffers.hpp.

## ◆ m_write_type

writable_item_type_t restinio::writable_item_t::m_write_type
| private

Definition at line 617 of file buffers.hpp.

The documentation for this class was generated from the following file:

- restinio/buffers.hpp