Crow  1.1
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>
14  struct partial_context : public black_magic::pop_back<Middlewares...>::template rebind<partial_context>, public black_magic::last_element_type<Middlewares...>::type::context
15  {
16  using parent_context = typename black_magic::pop_back<Middlewares...>::template rebind<::crow::detail::partial_context>;
17  template<int N>
18  using partial = typename std::conditional<N == sizeof...(Middlewares) - 1, partial_context, typename parent_context::template partial<N>>::type;
19 
20  template<typename T>
21  typename T::context& get()
22  {
23  return static_cast<typename T::context&>(*this);
24  }
25  };
26 
27 
28 
29  template<>
30  struct partial_context<>
31  {
32  template<int>
33  using partial = partial_context;
34  };
35 
36 
37  template<typename... Middlewares>
38  struct context : private partial_context<Middlewares...>
39  //struct context : private Middlewares::context... // simple but less type-safe
40  {
41  template<typename CallCriteria, int N, typename Context, typename Container>
42  friend typename std::enable_if<(N == 0)>::type after_handlers_call_helper(const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res);
43  template<typename CallCriteria, int N, typename Context, typename Container>
44  friend typename std::enable_if<(N > 0)>::type after_handlers_call_helper(const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res);
45 
46  template<typename CallCriteria, int N, typename Context, typename Container>
47  friend typename std::enable_if<(N < std::tuple_size<typename std::remove_reference<Container>::type>::value), bool>::type
48  middleware_call_helper(const CallCriteria& cc, Container& middlewares, request& req, response& res, Context& ctx);
49 
50  template<typename T>
51  typename T::context& get()
52  {
53  return static_cast<typename T::context&>(*this);
54  }
55 
56  template<int N>
57  using partial = typename partial_context<Middlewares...>::template partial<N>;
58  };
59  } // namespace detail
60 } // namespace crow
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition: middleware_context.h:40
Definition: middleware_context.h:15
An HTTP request.
Definition: http_request.h:36
HTTP response.
Definition: http_response.h:34