Crow/include/crow/middleware_context.h

61 lines
2.4 KiB
C
Raw Normal View History

#pragma once
#include "crow/utility.h"
#include "crow/http_request.h"
#include "crow/http_response.h"
namespace crow
{
namespace detail
{
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-07 22:07:53 +00:00
using parent_context = typename black_magic::pop_back<Middlewares...>::template rebind<::crow::detail::partial_context>;
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
template<typename T>
2014-09-07 22:07:53 +00:00
typename T::context& get()
{
return static_cast<typename T::context&>(*this);
}
};
template<>
struct partial_context<>
{
template<int>
2014-09-07 22:07:53 +00:00
using partial = partial_context;
};
template<typename... Middlewares>
struct context : private partial_context<Middlewares...>
//struct context : private Middlewares::context... // simple but less type-safe
{
2022-02-01 13:31:05 +00:00
template<template<typename QueryMW> class CallCriteria, 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);
2022-02-01 13:31:05 +00:00
template<template<typename QueryMW> class CallCriteria, 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
2022-02-01 13:31:05 +00:00
template<template<typename QueryMW> class CallCriteria, int N, typename Context, typename Container>
friend typename std::enable_if<(N < std::tuple_size<typename std::remove_reference<Container>::type>::value), bool>::type
middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx);
2014-09-07 22:07:53 +00:00
template<typename T>
typename T::context& get()
{
return static_cast<typename T::context&>(*this);
}
2014-09-07 22:07:53 +00:00
template<int N>
2014-09-07 22:07:53 +00:00
using partial = typename partial_context<Middlewares...>::template partial<N>;
};
} // namespace detail
} // namespace crow