fixed_size.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/request_handler.hpp>
#include <array>
namespace restinio
{
namespace sync_chain
{
//
// fixed_size_chain_t
//
template<
std::size_t Size,
typename Extra_Data_Factory = no_extra_data_factory_t >
class fixed_size_chain_t
{
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 &)
>;
std::array< handler_holder_t, Size > m_handlers;
template< std::size_t >
void
store_to() noexcept {}
template<
std::size_t Index,
typename Head,
typename... Tail >
void
store_to( Head && head, Tail && ...tail )
{
m_handlers[ Index ] =
[handler = std::move(head)]
( const actual_request_handle_t & req ) -> request_handling_status_t
{
return handler( req );
};
store_to< Index + 1u >( std::forward<Tail>(tail)... );
}
public:
fixed_size_chain_t() = delete;
template< typename... Handlers >
fixed_size_chain_t( Handlers && ...handlers )
{
static_assert( Size == sizeof...(handlers),
"Wrong number of parameters for the constructor of "
"fixed_size_chain_t<Size>. Exact `Size` parameters expected" );
store_to< 0u >( std::forward<Handlers>(handlers)... );
}
RESTINIO_NODISCARD
request_handling_status_t
operator()( const actual_request_handle_t & req ) const
{
for( auto & h : m_handlers )
{
const request_handling_status_t result = h( req );
switch( result )
{
case request_handling_status_t::accepted:
case request_handling_status_t::rejected:
// There is no need to try next handler.
return result;
case request_handling_status_t::not_handled:
// Nothing to do. The next handler should be tried.
break;
}
}
return request_not_handled();
}
};
} /* namespace sync_chain */
} /* namespace restinio */
restinio::sync_chain::fixed_size_chain_t
A holder of fixed-size chain of synchronous handlers.
Definition: fixed_size.hpp:164
restinio::sync_chain::fixed_size_chain_t::store_to
void store_to(Head &&head, Tail &&...tail)
Definition: fixed_size.hpp:183
restinio::sync_chain::fixed_size_chain_t::m_handlers
std::array< handler_holder_t, Size > m_handlers
Definition: fixed_size.hpp:172
restinio::sync_chain::fixed_size_chain_t::fixed_size_chain_t
fixed_size_chain_t()=delete
restinio::sync_chain::fixed_size_chain_t::operator()
RESTINIO_NODISCARD request_handling_status_t operator()(const actual_request_handle_t &req) const
Definition: fixed_size.hpp:223
restinio::sync_chain::fixed_size_chain_t::store_to
void store_to() noexcept
Definition: fixed_size.hpp:176
restinio::sync_chain::fixed_size_chain_t::fixed_size_chain_t
fixed_size_chain_t(Handlers &&...handlers)
Initializing constructor.
Definition: fixed_size.hpp:212
restinio::sync_chain::fixed_size_chain_t::handler_holder_t
std::function< request_handling_status_t(const actual_request_handle_t &) > handler_holder_t
Definition: fixed_size.hpp:170
restinio::sync_chain::fixed_size_chain_t::actual_request_handle_t
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
Definition: fixed_size.hpp:166
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio
Definition: asio_include.hpp:21
restinio::request_not_handled
RESTINIO_NODISCARD constexpr request_handling_status_t request_not_handled() noexcept
Definition: common_types.hpp:63
restinio::generic_request_handle_t
std::shared_ptr< generic_request_t< Extra_Data > > generic_request_handle_t
An alias for shared-pointer to incoming request.
Definition: request_handler.hpp:447
restinio::request_handling_status_t
request_handling_status_t
Request handling status.
Definition: common_types.hpp:26
restinio::request_handling_status_t::accepted
@ accepted
Request accepted for handling.
restinio::request_handling_status_t::not_handled
@ not_handled
The request wasn't handled. If there is another handler to be tried it should be tried....
restinio::request_handling_status_t::rejected
@ rejected
Request wasn't accepted for handling.
request_handler.hpp
restinio::no_extra_data_factory_t
The default extra-data-factory to be used in server's traits if a user doesn't specify own one.
Definition: request_handler.hpp:75