Skip to content

Commit

Permalink
Merge pull request #1127 from tyler92/bind_const_memfn
Browse files Browse the repository at this point in the history
Add the ability to use Routes::bind with constant member functions
  • Loading branch information
kiplingw committed Mar 25, 2023
2 parents 7f6bb27 + fa6e5da commit a68ad09
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
24 changes: 24 additions & 0 deletions include/pistache/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ namespace Pistache::Rest
};
}

template <typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...) const, Obj obj)
{
details::static_checks<details::BindChecks, Args...>();

return [=](const Rest::Request& request, Http::ResponseWriter 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)
{
Expand All @@ -428,6 +440,18 @@ namespace Pistache::Rest
};
}

template <typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...) const, std::shared_ptr<Obj> objPtr)
{
details::static_checks<details::BindChecks, Args...>();

return [=](const Rest::Request& request, Http::ResponseWriter response) {
(objPtr.get()->*func)(request, std::move(response));

return Route::Result::Ok;
};
}

template <typename Result, typename... Args>
Route::Handler bind(Result (*func)(Args...))
{
Expand Down
20 changes: 16 additions & 4 deletions tests/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,23 @@ class MyHandler
const Pistache::Rest::Request&,
Pistache::Http::ResponseWriter response)
{
count_++;
(*count_)++;
response.send(Pistache::Http::Code::Ok);
}

int getCount() { return count_; }
void handleConst(
const Pistache::Rest::Request&,
Pistache::Http::ResponseWriter response) const
{
(*count_)++;
response.send(Pistache::Http::Code::Ok);
}


int getCount() { return *count_; }

private:
int count_ = 0;
std::unique_ptr<int> count_ = std::make_unique<int>(0);
};

TEST(router_test, test_bind_shared_ptr)
Expand All @@ -280,6 +289,7 @@ TEST(router_test, test_bind_shared_ptr)
Rest::Router router;

Routes::Head(router, "/tinkywinky", Routes::bind(&MyHandler::handle, sharedPtr));
Routes::Head(router, "/checkconst", Routes::bind(&MyHandler::handleConst, sharedPtr));

endpoint->setHandler(router.handler());
endpoint->serveThreaded();
Expand All @@ -289,6 +299,8 @@ TEST(router_test, test_bind_shared_ptr)
ASSERT_EQ(sharedPtr->getCount(), 0);
client.Head("/tinkywinky");
ASSERT_EQ(sharedPtr->getCount(), 1);
client.Head("/checkconst");
ASSERT_EQ(sharedPtr->getCount(), 2);

endpoint->shutdown();
}
Expand Down Expand Up @@ -382,7 +394,7 @@ TEST(router_test, test_auth_middleware)
router.addMiddleware(Routes::middleware(&fill_auth_header));
router.addMiddleware(Routes::middleware(&HandlerWithAuthMiddleware::do_auth, &handler));

Routes::Head(router, "/tinkywinky", Routes::bind(&HandlerWithAuthMiddleware::handle, &handler));
Routes::Head(router, "/tinkywinky", Routes::bind(&HandlerWithAuthMiddleware::handleConst, &handler));
endpoint->setHandler(router.handler());
endpoint->serveThreaded();

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1.20230219
0.1.1.20230324

0 comments on commit a68ad09

Please sign in to comment.