Skip to content

Instantly share code, notes, and snippets.

@MacDada
Created March 11, 2023 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacDada/d2deb3cebd93cf310f84b760b028395d to your computer and use it in GitHub Desktop.
Save MacDada/d2deb3cebd93cf310f84b760b028395d to your computer and use it in GitHub Desktop.
void setupRouting() {
registerRoute("/api/furnace", HTTP_GET, [&] (Request& request) {
furnaceController.apiGetAction(request);
});
/* and many more like this one */
}
// OPTION A: copy callback to function + move to lambda
void registerRoute(
const char* const uri,
const HttpMethod method,
std::function<void(Request& request)> callback
) {
server.on(uri, method, [&, callback = std::move(callback)] (Request* const request) {
/* some stuff here */
callback(*request);
});
}
// OPTION B: reference callback to function + copy to lambda
void registerRoute(
const char* const uri,
const HttpMethod method,
const std::function<void(Request& request)>& callback
) {
server.on(uri, method, [&, callback] (Request* const request) {
/* some stuff here */
callback(*request);
});
}
// OPTION C: forward callback to function + move to lambda
void registerRoute(
const char* const uri,
const HttpMethod method,
std::function<void(Request& request)>&& callback
) {
server.on(uri, method, [&, callback = std::move(callback)] (Request* const request) {
/* some stuff here */
callback(*request);
});
}
// ~~OPTION D~~: reference callback to function, reference/move to lambda
// => does NOT work -> as the passed–in callback has to outlive registerRoute function
// OPTION E: copy callback to function, copy to lambda
// => works, but is wasteful I guess ;-) IDE suggests to move or reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment