Skip to content

Commit

Permalink
handler can receive response as argument without request being anothe…
Browse files Browse the repository at this point in the history
…r argument.

PR ipkn#312 ukseong
  • Loading branch information
rustyx committed Feb 8, 2019
1 parent 157760e commit b892260
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/crow/http_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ namespace crow
}
}

void end(const int &_code)
{
code = _code;
end();
}

void end(const std::string& body_part)
{
body += body_part;
Expand Down
27 changes: 26 additions & 1 deletion include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,32 @@ namespace crow
template <typename Func>
typename std::enable_if<
!black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value,
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value &&
black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value,
void>::type
operator()(Func&& f) {
static_assert(black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value
,
"Handler type is mismatched with URL parameters");
static_assert(std::is_same<void, decltype(f(std::declval<crow::response&>(), std::declval<Args>()...))>::value,
"Handler function with response argument should have void return type");
handler_ = (
#ifdef CROW_CAN_USE_CPP14
[f = std::move(f)]
#else
[f]
#endif
(const crow::request& req, crow::response& res, Args ... args){
f(res, args...);
});
}

template <typename Func>
typename std::enable_if<
!black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value,
void>::type
operator()(Func&& f)
{
Expand Down

0 comments on commit b892260

Please sign in to comment.