RESTinio

growable_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 <vector>
17
18 namespace restinio
19{
20
21 namespace sync_chain
22{
23
24//
25// growable_size_chain_t
26//
157 template< typename Extra_Data_Factory = no_extra_data_factory_t >
158 class growable_size_chain_t
159{
160 // Helper class to allow the creation of growable_size_chain_t only
161 // for the friends of growable_size_chain_t.
162 struct creation_token_t {};
163
164 public:
165 friend class builder_t;
166
183 class builder_t
184 {
185 public:
186 builder_t()
187 : m_chain{ new growable_size_chain_t( creation_token_t{} ) }
188 {}
189
197 RESTINIO_NODISCARD
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(
217 growable_size_chain_t::handler_holder_t{
218 std::forward<Handler>(handler)
219 } );
220 }
221
222 private:
223 std::unique_ptr< growable_size_chain_t > m_chain;
224 };
225
226 private:
227 using actual_request_handle_t =
228 generic_request_handle_t< typename Extra_Data_Factory::data_t >;
229
230 using handler_holder_t = std::function<
231 request_handling_status_t(const actual_request_handle_t &)
232 >;
233
234 std::vector< handler_holder_t > m_handlers;
235
242 growable_size_chain_t( creation_token_t ) {}
243
244 public:
250 growable_size_chain_t() = delete;
251
252 RESTINIO_NODISCARD
253 request_handling_status_t
254 operator()( const actual_request_handle_t & req ) const
255 {
256 for( auto & h : m_handlers )
257 {
258 const request_handling_status_t result = h( req );
259
260 switch( result )
261 {
262 case request_handling_status_t::accepted:
263 case request_handling_status_t::rejected:
264 // There is no need to try next handler.
265 return result;
266
267 case request_handling_status_t::not_handled:
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
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: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::sync_chain::growable_size_chain_t::creation_token_t
Definition: growable_size.hpp:162