Crow  0.3
A C++ microframework for the web
middleware_context.h
1 #pragma once
2 
3 #include "crow/utility.h"
4 #include "crow/http_request.h"
5 #include "crow/http_response.h"
6 
7 namespace crow
8 {
9  namespace detail
10  {
11 
12 
13  template <typename ... Middlewares>
15  : public black_magic::pop_back<Middlewares...>::template rebind<partial_context>
16  , public black_magic::last_element_type<Middlewares...>::type::context
17  {
18  using parent_context = typename black_magic::pop_back<Middlewares...>::template rebind<::crow::detail::partial_context>;
19  template <int N>
20  using partial = typename std::conditional<N == sizeof...(Middlewares)-1, partial_context, typename parent_context::template partial<N>>::type;
21 
22  template <typename T>
23  typename T::context& get()
24  {
25  return static_cast<typename T::context&>(*this);
26  }
27  };
28 
29 
30 
31  template <>
32  struct partial_context<>
33  {
34  template <int>
35  using partial = partial_context;
36  };
37 
38 
39 
40  template <int N, typename Context, typename Container, typename CurrentMW, typename ... Middlewares>
41  bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx);
42 
43 
44 
45  template <typename ... Middlewares>
46  struct context : private partial_context<Middlewares...>
47  //struct context : private Middlewares::context... // simple but less type-safe
48  {
49  template <int N, typename Context, typename Container>
50  friend typename std::enable_if<(N==0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res);
51  template <int N, typename Context, typename Container>
52  friend typename std::enable_if<(N>0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res);
53 
54  template <int N, typename Context, typename Container, typename CurrentMW, typename ... Middlewares2>
55  friend bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx);
56 
57  template <typename T>
58  typename T::context& get()
59  {
60  return static_cast<typename T::context&>(*this);
61  }
62 
63  template <int N>
64  using partial = typename partial_context<Middlewares...>::template partial<N>;
65  };
66  }
67 }
crow::black_magic::last_element_type
Definition: utility.h:317
crow::request
An HTTP request.
Definition: http_request.h:26
crow::response
HTTP response.
Definition: http_response.h:23
crow::detail::context
Definition: middleware_context.h:46
crow::detail::partial_context
Definition: middleware_context.h:14
crow::black_magic::pop_back
Definition: utility.h:363