RESTinio
growable_size.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
16#include <vector>
17
18namespace restinio
19{
20
21namespace sync_chain
22{
23
24//
25// growable_size_chain_t
26//
157template< typename Extra_Data_Factory = no_extra_data_factory_t >
159{
160 // Helper class to allow the creation of growable_size_chain_t only
161 // for the friends of growable_size_chain_t.
163
164public:
165 friend class builder_t;
166
184 {
185 public:
188 {}
189
198 std::unique_ptr< growable_size_chain_t >
199 release() noexcept
200 {
201 return { std::move(m_chain) };
202 }
203
207 template< typename Handler >
208 void
209 add( Handler && handler )
210 {
211 if( !m_chain )
212 throw exception_t{ "an attempt to add a handler to "
213 "a growable-size-chain builder that already "
214 "released"
215 };
216 m_chain->m_handlers.push_back(
218 std::forward<Handler>(handler)
219 } );
220 }
221
222 private:
223 std::unique_ptr< growable_size_chain_t > m_chain;
224 };
225
226private:
229
230 using handler_holder_t = std::function<
232 >;
233
234 std::vector< handler_holder_t > m_handlers;
235
243
244public:
251
255 {
256 for( auto & h : m_handlers )
257 {
258 const request_handling_status_t result = h( req );
259
260 switch( result )
261 {
264 // There is no need to try next handler.
265 return result;
266
268 // Nothing to do. The next handler should be tried.
269 break;
270 }
271 }
272
273 return request_not_handled();
274 }
275};
276
277} /* namespace sync_chain */
278
279} /* namespace restinio */
280
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
A builder of an instance of growable_size_chain.
std::unique_ptr< growable_size_chain_t > m_chain
void add(Handler &&handler)
Add a new handler to the chain.
RESTINIO_NODISCARD std::unique_ptr< growable_size_chain_t > release() noexcept
Stop adding of new handlers and acquire the chain instance.
A holder of variable-size chain of synchronous handlers.
growable_size_chain_t(creation_token_t)
The main constructor.
std::function< request_handling_status_t(const actual_request_handle_t &) > handler_holder_t
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
RESTINIO_NODISCARD request_handling_status_t operator()(const actual_request_handle_t &req) const
std::vector< handler_holder_t > m_handlers
#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.