Skip to content

Commit

Permalink
added auto-response
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Nov 29, 2023
1 parent af070fa commit 091c8ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions examples/06_custom/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ int main() {
tl::engine myEngine("tcp", THALLIUM_SERVER_MODE);
std::cout << "Server running at address " << myEngine.self() << std::endl;

std::function<void(const tl::request&, const point&, const point&)> dot_product =
std::function<void(const tl::request&, const point&, const point&)> dot_product =
[&myEngine](const tl::request& req, const point& p, const point& q) {
req.respond(p*q);
point pq;
tl::auto_respond<point> response{req, pq};
pq = p*q;
myEngine.finalize();
};

Expand Down
28 changes: 28 additions & 0 deletions include/thallium/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,34 @@ class request_with_context {

using request = request_with_context<>;

/**
* @brief auto_respond is a helper class that prevents
* users from forgetting to call req.respond() by
* having this auto_respond object call req.respond()
* on destruction.
*/
template<typename ResponseType>
class auto_respond {

public:

auto_respond(const request& req, ResponseType& data)
: m_request{req}
, m_data{data} {}

auto_respond(auto_respond&&) = delete;
auto_respond(const auto_respond&) = delete;

~auto_respond() {
m_request.respond(m_data);
}

private:

const request& m_request;
ResponseType& m_data;
};

} // namespace thallium

#include <thallium/endpoint.hpp>
Expand Down

0 comments on commit 091c8ee

Please sign in to comment.