RESTinio

fixed_size.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/request_handler.hpp>
15
16#include <array>
17
18 namespace restinio
19{
20
21 namespace sync_chain
22{
23
24//
25// fixed_size_chain_t
26//
160 template<
161 std::size_t Size,
162 typename Extra_Data_Factory = no_extra_data_factory_t >
163 class fixed_size_chain_t
164{
165 using actual_request_handle_t =
166 generic_request_handle_t< typename Extra_Data_Factory::data_t >;
167
168 using handler_holder_t = std::function<
169 request_handling_status_t(const actual_request_handle_t &)
170 >;
171
172 std::array< handler_holder_t, Size > m_handlers;
173
174 template< std::size_t >
175 void
176 store_to() noexcept {}
177
178 template<
179 std::size_t Index,
180 typename Head,
181 typename... Tail >
182 void
183 store_to( Head && head, Tail && ...tail )
184 {
185 m_handlers[ Index ] =
186 [handler = std::move(head)]
187 ( const actual_request_handle_t & req ) -> request_handling_status_t
188 {
189 return handler( req );
190 };
191
192 store_to< Index + 1u >( std::forward<Tail>(tail)... );
193 }
194
195 public:
202 fixed_size_chain_t() = delete;
203
211 template< typename... Handlers >
212 fixed_size_chain_t( Handlers && ...handlers )
213 {
214 static_assert( Size == sizeof...(handlers),
215 "Wrong number of parameters for the constructor of "
216 "fixed_size_chain_t<Size>. Exact `Size` parameters expected" );
217
218 store_to< 0u >( std::forward<Handlers>(handlers)... );
219 }
220
221 RESTINIO_NODISCARD
222 request_handling_status_t
223 operator()( const actual_request_handle_t & req ) const
224 {
225 for( auto & h : m_handlers )
226 {
227 const request_handling_status_t result = h( req );
228
229 switch( result )
230 {
231 case request_handling_status_t::accepted:
232 case request_handling_status_t::rejected:
233 // There is no need to try next handler.
234 return result;
235
236 case request_handling_status_t::not_handled:
237 // Nothing to do. The next handler should be tried.
238 break;
239 }
240 }
241
242 return request_not_handled();
243 }
244};
245
246} /* namespace sync_chain */
247
248} /* namespace restinio */
249
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