2014-09-06 16:24:45 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-09-21 14:11:06 +00:00
|
|
|
#include "crow/utility.h"
|
|
|
|
#include "crow/http_request.h"
|
|
|
|
#include "crow/http_response.h"
|
2014-09-06 16:24:45 +00:00
|
|
|
|
|
|
|
namespace crow
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
2020-11-18 22:13:57 +00:00
|
|
|
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<typename... Middlewares>
|
|
|
|
struct partial_context : public black_magic::pop_back<Middlewares...>::template rebind<partial_context>, public black_magic::last_element_type<Middlewares...>::type::context
|
2014-09-06 16:24:45 +00:00
|
|
|
{
|
2014-09-07 22:07:53 +00:00
|
|
|
using parent_context = typename black_magic::pop_back<Middlewares...>::template rebind<::crow::detail::partial_context>;
|
2021-11-25 11:45:38 +00:00
|
|
|
template<int N>
|
|
|
|
using partial = typename std::conditional<N == sizeof...(Middlewares) - 1, partial_context, typename parent_context::template partial<N>>::type;
|
2014-09-07 22:07:53 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<typename T>
|
2014-09-07 22:07:53 +00:00
|
|
|
typename T::context& get()
|
|
|
|
{
|
|
|
|
return static_cast<typename T::context&>(*this);
|
|
|
|
}
|
2014-09-06 16:24:45 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 22:13:57 +00:00
|
|
|
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<>
|
2014-09-06 16:24:45 +00:00
|
|
|
struct partial_context<>
|
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
template<int>
|
2014-09-07 22:07:53 +00:00
|
|
|
using partial = partial_context;
|
2014-09-06 16:24:45 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 22:13:57 +00:00
|
|
|
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<int N, typename Context, typename Container, typename CurrentMW, typename... Middlewares>
|
2014-09-07 22:07:53 +00:00
|
|
|
bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx);
|
|
|
|
|
2020-11-18 22:13:57 +00:00
|
|
|
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<typename... Middlewares>
|
2014-09-06 16:24:45 +00:00
|
|
|
struct context : private partial_context<Middlewares...>
|
|
|
|
//struct context : private Middlewares::context... // simple but less type-safe
|
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
template<int N, typename Context, typename Container>
|
|
|
|
friend typename std::enable_if<(N == 0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res);
|
|
|
|
template<int N, typename Context, typename Container>
|
|
|
|
friend typename std::enable_if<(N > 0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res);
|
2014-09-07 22:07:53 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<int N, typename Context, typename Container, typename CurrentMW, typename... Middlewares2>
|
2014-09-07 22:07:53 +00:00
|
|
|
friend bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx);
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<typename T>
|
2014-09-06 16:24:45 +00:00
|
|
|
typename T::context& get()
|
|
|
|
{
|
|
|
|
return static_cast<typename T::context&>(*this);
|
|
|
|
}
|
2014-09-07 22:07:53 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<int N>
|
2014-09-07 22:07:53 +00:00
|
|
|
using partial = typename partial_context<Middlewares...>::template partial<N>;
|
2014-09-06 16:24:45 +00:00
|
|
|
};
|
2021-11-25 11:45:38 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace crow
|