RESTinio
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory > Class Template Reference

A holder of fixed-size chain of synchronous handlers. More...

#include <fixed_size.hpp>

Public Member Functions

 fixed_size_chain_t ()=delete
 
template<typename... Handlers>
 fixed_size_chain_t (Handlers &&...handlers)
 Initializing constructor. More...
 
RESTINIO_NODISCARD request_handling_status_t operator() (const actual_request_handle_t &req) const
 

Private Types

using actual_request_handle_t = generic_request_handle_t< typename Extra_Data_Factory::data_t >
 
using handler_holder_t = std::function< request_handling_status_t(const actual_request_handle_t &) >
 

Private Member Functions

template<std::size_t >
void store_to () noexcept
 
template<std::size_t Index, typename Head , typename... Tail>
void store_to (Head &&head, Tail &&...tail)
 

Private Attributes

std::array< handler_holder_t, Size > m_handlers
 

Detailed Description

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
class restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >

A holder of fixed-size chain of synchronous handlers.

Note
An instance of that type is intended to be filled with actual handlers at the creation time. After that new handlers can't be added to the chain, and old handlers can't be removed from the chain.

Usage example for the case when there is no extra-data in a request object.

struct my_traits : public restinio::default_traits_t {
};
// The first handler in the chain.
{
... // Checks values of HTTP-fields and rejects invalid requests.
}
// The second handler in the chain.
{
... // Checks user's credentials and rejects requests from
// non-authentificated users.
}
// The last handler in the chain.
{
... // Actual processing.
}
on_thread_pool<my_traits>(16)
.address(...)
.port(...)
.request_handler(
// Just enumerate all handlers.
headers_checker,
authentificator,
actual_handler )
);
A holder of fixed-size chain of synchronous handlers.
Definition: fixed_size.hpp:164
void run(asio_ns::io_context &ioctx, run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
request_handling_status_t
Request handling status.
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.

An instance of fixed_size_chain_t can also be created manually and passed to server's settings by unique_ptr:

auto chain = std::make_unique<restinio::fixed_size_chain_t<3>>(
headers_checker, authentificator, actual_handler);
...
restinio::run(
on_thread_pool<my_traits>(16)
.address(...)
.port(...)
.request_handler(std::move(chain))
);

Usage example for the case when some extra-data is incorporated into a request object.

struct my_extra_data_factory {
// A data formed by checker of HTTP-fields.
struct request_specific_fields_t {...};
// A data formed by user-authentificator.
struct user_info_t {...};
// A data to be incorporated into a request object.
using data_t = std::tuple<request_specific_fields_t, user_info_t>;
void make_within(restinio::extra_data_buffer_t<data_t> buf) {
new(buf.get()) data_t{};
}
};
struct my_traits : public restinio::default_traits_t {
using extra_data_factory_t = my_extra_data_factory;
using request_handler_t = restinio::sync_chain::fixed_size_chain_t<
3,
extra_data_factory>;
};
using my_request_handle_t =
// The first handler in the chain.
const my_request_handle_t & req )
{
... // Checks values of HTTP-fields and rejects invalid requests.
}
// The second handler in the chain.
const my_request_handle_t & req )
{
... // Checks user's credentials and rejects requests from
// non-authentificated users.
}
// The last handler in the chain.
const my_request_handle_t & req )
{
auto & field_values = std::get<my_extra_data_factory::request_specific_fields_t>(req->extra_data());
auto & user_info = std::get<my_extra_data_factory::user_info_t>(req->extra_data());
... // Actual processing.
}
on_thread_pool<my_traits>(16)
.address(...)
.port(...)
.request_handler(
// Just enumerate all handlers.
headers_checker,
authentificator,
actual_handler )
);
Helper for holding a pointer to a buffer where a new object of type Extra_Data should be constructed.
RESTINIO_NODISCARD void * get() const noexcept
std::shared_ptr< generic_request_t< Extra_Data > > generic_request_handle_t
An alias for shared-pointer to incoming request.
Template Parameters
SizeThe exact number of handlers in the chain.
Extra_Data_FactoryThe type of extra-data-factory specified in the server's traits.
Since
v.0.6.13
Examples
sample/chained_handlers/main.cpp.

Definition at line 163 of file fixed_size.hpp.

Member Typedef Documentation

◆ actual_request_handle_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::actual_request_handle_t = generic_request_handle_t< typename Extra_Data_Factory::data_t >
private

Definition at line 165 of file fixed_size.hpp.

◆ handler_holder_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::handler_holder_t = std::function< request_handling_status_t(const actual_request_handle_t &) >
private

Definition at line 168 of file fixed_size.hpp.

Constructor & Destructor Documentation

◆ fixed_size_chain_t() [1/2]

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::fixed_size_chain_t ( )
delete
Attention
The default constructor is disabled. It's because a chain should be initialized by handlers at the creation time. Because of that fixed_size_chain_t isn't a DefaultConstructible type.

◆ fixed_size_chain_t() [2/2]

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
template<typename... Handlers>
restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::fixed_size_chain_t ( Handlers &&...  handlers)
inline

Initializing constructor.

Note
The number of parameters should match the value of Size template parameter.

Definition at line 212 of file fixed_size.hpp.

Member Function Documentation

◆ operator()()

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
RESTINIO_NODISCARD request_handling_status_t restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::operator() ( const actual_request_handle_t req) const
inline

Definition at line 223 of file fixed_size.hpp.

◆ store_to() [1/2]

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
template<std::size_t >
void restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::store_to ( )
inlineprivatenoexcept

Definition at line 176 of file fixed_size.hpp.

◆ store_to() [2/2]

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
template<std::size_t Index, typename Head , typename... Tail>
void restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::store_to ( Head &&  head,
Tail &&...  tail 
)
inlineprivate

Definition at line 183 of file fixed_size.hpp.

Member Data Documentation

◆ m_handlers

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
std::array< handler_holder_t, Size > restinio::sync_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::m_handlers
private

Definition at line 172 of file fixed_size.hpp.


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