Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 3 | #include "http_request.h" |
| 4 | #include "http_response.h" |
| 5 | #include "utility.h" |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 6 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | namespace crow |
| 8 | { |
| 9 | namespace detail |
| 10 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | template <typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 12 | struct PartialContext |
| 13 | : public black_magic::PopBack<Middlewares...>::template rebind< |
| 14 | PartialContext>, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | public black_magic::LastElementType<Middlewares...>::type::Context |
| 16 | { |
| 17 | using parent_context = typename black_magic::PopBack< |
| 18 | Middlewares...>::template rebind<::crow::detail::PartialContext>; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 19 | template <size_t N> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | using partial = typename std::conditional< |
| 21 | N == sizeof...(Middlewares) - 1, PartialContext, |
| 22 | typename parent_context::template partial<N>>::type; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | template <typename T> typename T::Context& get() |
| 25 | { |
| 26 | return static_cast<typename T::Context&>(*this); |
| 27 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | template <> struct PartialContext<> |
| 31 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 32 | template <size_t> using partial = PartialContext; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 35 | template <size_t N, typename Context, typename Container, typename CurrentMW, |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 36 | typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 37 | bool middlewareCallHelper(Container& middlewares, Request& req, Response& res, |
| 38 | Context& ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 39 | |
| 40 | template <typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 41 | struct Context : private PartialContext<Middlewares...> |
| 42 | // struct Context : private Middlewares::context... // simple but less type-safe |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 43 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 44 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | friend typename std::enable_if<(N == 0)>::type |
| 46 | afterHandlersCallHelper(Container& middlewares, Context& ctx, |
| 47 | Request& req, Response& res); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 48 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | friend typename std::enable_if<(N > 0)>::type |
| 50 | afterHandlersCallHelper(Container& middlewares, Context& ctx, |
| 51 | Request& req, Response& res); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 52 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 53 | template <size_t N, typename Context, typename Container, |
| 54 | typename CurrentMW, typename... Middlewares2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | friend bool middlewareCallHelper(Container& middlewares, Request& req, |
| 56 | Response& res, Context& ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 57 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | template <typename T> typename T::Context& get() |
| 59 | { |
| 60 | return static_cast<typename T::Context&>(*this); |
| 61 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 62 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 63 | template <size_t N> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | using partial = |
| 65 | typename PartialContext<Middlewares...>::template partial<N>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 66 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 67 | } // namespace detail |
| 68 | } // namespace crow |