Unverified Commit 2d222394 authored by Christophe Bedard's avatar Christophe Bedard Committed by GitHub

Add version of bind() that can bind to shared_ptrs (#707)

* Add version of bind() that can bind to shared_ptrs

* Expand binding call macros

* Add test for std::shared_ptr bind()
parent 02ade86c
......@@ -40,13 +40,18 @@ template <typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...), Obj obj) {
details::static_checks<Args...>();
#define CALL_MEMBER_FN(obj, pmf) ((obj)->*(pmf))
return [=](const Rest::Request &request, Http::ResponseWriter response) {
CALL_MEMBER_FN(obj, func)(request, std::move(response));
(obj->*func)(request, std::move(response));
};
}
#undef CALL_MEMBER_FN
template <typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...), std::shared_ptr<Obj> objPtr) {
details::static_checks<Args...>();
return [=](const Rest::Request &request, Http::ResponseWriter response) {
(objPtr.get()->*func)(request, std::move(response));
};
}
template <typename Result, typename... Args>
......
......@@ -325,15 +325,22 @@ template <typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...), Obj obj) {
details::static_checks<Args...>();
#define CALL_MEMBER_FN(obj, pmf) ((obj)->*(pmf))
return [=](const Rest::Request &request, Http::ResponseWriter response) {
CALL_MEMBER_FN(obj, func)(request, std::move(response));
(obj->*func)(request, std::move(response));
return Route::Result::Ok;
};
}
template <typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...), std::shared_ptr<Obj> objPtr) {
details::static_checks<Args...>();
#undef CALL_MEMBER_FN
return [=](const Rest::Request &request, Http::ResponseWriter response) {
(objPtr.get()->*func)(request, std::move(response));
return Route::Result::Ok;
};
}
template <typename Result, typename... Args>
......
......@@ -230,3 +230,51 @@ TEST(router_test, test_route_head_request) {
endpoint->shutdown();
}
class MyHandler {
public:
MyHandler()
: count_(0)
{}
~MyHandler() {}
void handle(
const Pistache::Rest::Request &,
Pistache::Http::ResponseWriter response) {
count_++;
response.send(Pistache::Http::Code::Ok);
}
int getCount() { return count_; }
private:
int count_;
};
TEST(router_test, test_bind_shared_ptr) {
Address addr(Ipv4::any(), 0);
auto endpoint = std::make_shared<Http::Endpoint>(addr);
auto opts = Http::Endpoint::options().threads(1).maxRequestSize(4096);
endpoint->init(opts);
std::shared_ptr<MyHandler> sharedPtr = std::make_shared<MyHandler>();
Rest::Router router;
Routes::Head(router, "/tinkywinky", Routes::bind(&MyHandler::handle, sharedPtr));
endpoint->setHandler(router.handler());
endpoint->serveThreaded();
const auto bound_port = endpoint->getPort();
httplib::Client client("localhost", bound_port);
ASSERT_EQ(sharedPtr->getCount(), 0);
client.Head("/tinkywinky");
ASSERT_EQ(sharedPtr->getCount(), 1);
endpoint->shutdown();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment