Loading...
Searching...
No Matches
growable_size.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/request_handler.hpp>
#include <vector>
namespace restinio
{
namespace sync_chain
{
//
// growable_size_chain_t
//
template< typename Extra_Data_Factory = no_extra_data_factory_t >
class growable_size_chain_t
{
// Helper class to allow the creation of growable_size_chain_t only
// for the friends of growable_size_chain_t.
struct creation_token_t {};
public:
friend class builder_t;
class builder_t
{
public:
builder_t()
: m_chain{ new growable_size_chain_t( creation_token_t{} ) }
{}
RESTINIO_NODISCARD
std::unique_ptr< growable_size_chain_t >
release() noexcept
{
return { std::move(m_chain) };
}
template< typename Handler >
void
add( Handler && handler )
{
if( !m_chain )
throw exception_t{ "an attempt to add a handler to "
"a growable-size-chain builder that already "
"released"
};
m_chain->m_handlers.push_back(
growable_size_chain_t::handler_holder_t{
std::forward<Handler>(handler)
} );
}
private:
std::unique_ptr< growable_size_chain_t > m_chain;
};
private:
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::vector< handler_holder_t > m_handlers;
growable_size_chain_t( creation_token_t ) {}
public:
growable_size_chain_t() = delete;
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::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio::sync_chain::growable_size_chain_t::builder_t
A builder of an instance of growable_size_chain.
Definition: growable_size.hpp:184
restinio::sync_chain::growable_size_chain_t::builder_t::m_chain
std::unique_ptr< growable_size_chain_t > m_chain
Definition: growable_size.hpp:223
restinio::sync_chain::growable_size_chain_t::builder_t::add
void add(Handler &&handler)
Add a new handler to the chain.
Definition: growable_size.hpp:209
restinio::sync_chain::growable_size_chain_t::builder_t::release
RESTINIO_NODISCARD std::unique_ptr< growable_size_chain_t > release() noexcept
Stop adding of new handlers and acquire the chain instance.
Definition: growable_size.hpp:199
restinio::sync_chain::growable_size_chain_t::builder_t::builder_t
builder_t()
Definition: growable_size.hpp:186
restinio::sync_chain::growable_size_chain_t
A holder of variable-size chain of synchronous handlers.
Definition: growable_size.hpp:159
restinio::sync_chain::growable_size_chain_t::growable_size_chain_t
growable_size_chain_t()=delete
restinio::sync_chain::growable_size_chain_t::growable_size_chain_t
growable_size_chain_t(creation_token_t)
The main constructor.
Definition: growable_size.hpp:242
restinio::sync_chain::growable_size_chain_t::handler_holder_t
std::function< request_handling_status_t(const actual_request_handle_t &) > handler_holder_t
Definition: growable_size.hpp:232
restinio::sync_chain::growable_size_chain_t::actual_request_handle_t
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
Definition: growable_size.hpp:228
restinio::sync_chain::growable_size_chain_t::operator()
RESTINIO_NODISCARD request_handling_status_t operator()(const actual_request_handle_t &req) const
Definition: growable_size.hpp:254
restinio::sync_chain::growable_size_chain_t::m_handlers
std::vector< handler_holder_t > m_handlers
Definition: growable_size.hpp:234
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:36
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:455
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::sync_chain::growable_size_chain_t::creation_token_t
Definition: growable_size.hpp:162