RESTinio
fixed_size.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
16#include <array>
17
18namespace restinio
19{
20
21namespace sync_chain
22{
23
24//
25// fixed_size_chain_t
26//
160template<
161 std::size_t Size,
162 typename Extra_Data_Factory = no_extra_data_factory_t >
164{
167
168 using handler_holder_t = std::function<
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)]
188 {
189 return handler( req );
190 };
191
192 store_to< Index + 1u >( std::forward<Tail>(tail)... );
193 }
194
195public:
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
224 {
225 for( auto & h : m_handlers )
226 {
227 const request_handling_status_t result = h( req );
228
229 switch( result )
230 {
233 // There is no need to try next handler.
234 return result;
235
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
A holder of fixed-size chain of synchronous handlers.
Definition: fixed_size.hpp:164
void store_to(Head &&head, Tail &&...tail)
Definition: fixed_size.hpp:183
std::array< handler_holder_t, Size > m_handlers
Definition: fixed_size.hpp:172
RESTINIO_NODISCARD request_handling_status_t operator()(const actual_request_handle_t &req) const
Definition: fixed_size.hpp:223
fixed_size_chain_t(Handlers &&...handlers)
Initializing constructor.
Definition: fixed_size.hpp:212
std::function< request_handling_status_t(const actual_request_handle_t &) > handler_holder_t
Definition: fixed_size.hpp:170
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
Definition: fixed_size.hpp:166
#define RESTINIO_NODISCARD
RESTINIO_NODISCARD constexpr request_handling_status_t request_not_handled() noexcept
std::shared_ptr< generic_request_t< Extra_Data > > generic_request_handle_t
An alias for shared-pointer to incoming request.
request_handling_status_t
Request handling status.
@ accepted
Request accepted for handling.
@ not_handled
The request wasn't handled. If there is another handler to be tried it should be tried....
@ rejected
Request wasn't accepted for handling.
The default extra-data-factory to be used in server's traits if a user doesn't specify own one.