Created
March 11, 2023 20:33
-
-
Save MacDada/d2deb3cebd93cf310f84b760b028395d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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