Description
By using std::function, you allow C++ clients of your libs to bind C++ instance-member functions to your callbacks.
std::function is flexible enough to support both static-style and instance-member functions so you can get the best of both worlds. The static/instance invocation nature is encapsulated, so invocation via a std::function is just the normal ( ) semantics.
I, like you, have been doing quite a bit of C++ with ESP32. As such, I have adopted the std::function style for all my callbacks. It's very liberating (and natural OO) to use my instance-member functions as callbacks since those callbacks enjoy access to the state of the instance they belong to.
I have attached HttpServer[.h/cpp] and standalone example HttpServer example showing class static and instance member functions being used as callbacks. The files have ".txt" extensions so they would be able to be added to this note.
See the HttpServer::addPathHandler(...) and related code for references and handling of the std::function.
I left the argument and variable names in HttpServer the same, for less code changes. But, since std::function is not a pointer, then the naming convention of pXXX in the code no longer applies.
The rationale for putting http_req_resp_cb_t in PathHandler was due to declaration order/dependency and I didn't want to change more than the min to show the std::function working. One perspective is http_req_resp_cb_t should live on HttpServer as this is the main type clients will interact with and the PathHandler is really a behind-the-scenes support type for HttpServer.
ex_kolban_ws_w_std_func.cpp.txt
HttpServer.cpp.txt
HttpServer.h.txt
The example web requests are:
http://assigned-ip/c_style_cb
http://assigned-ip/static_cb
http://assigned-ip/instance_cb
Thanks again for your contributions and have good holidays!