I have a question about a 'technique' that is used in cpp-netlib to implement an HTTP server and handler requests.
The documentation states:
As with the HTTP client, the HTTP server that is provided with cpp-netlib is extensible through the tag mechanism and is embeddable. The template class declaration of basic_server is given below:
namespace boost { namespace network { namespace http {
template <class Tag, class RequestHandler> basic_server;
}}}
The second template argument is used to specify the request handler type. The request handler type is a functor type which should overload the function call operator (RequestHandler::operator() should be overloaded) that takes two parameters: the first one being a reference to a const basic_request and the second being a reference to a basic_response instance.
So in my code I create a handler (which declares the required functions) and use it to construct an options object:
boost::network::http::server<HttpRequestHandler>::options options(*handler);
and then create the server using the options and handler tag (I think that's the right terminology):
boost::network::http::server<HttpRequestHandler> server(options);
As required, my handler class provides the function call operator. It all works just fine - i.e. the handler is called when a message is received.
My question is this: how is it that the library has access to the complete type of my handler so that it can call the function when it hasn't included it?
I suppose the library instantiates the object of your class, which is passed as template parameter. Then it just uses function call operator on it, which you have overloaded as per library's request.
In the innards of the library happens something along the following lines:
template <class Functor>
void doStuff() {
auto callback = Functor();
callback();
}
It doesn't need to #include
your class in it's header file because the function you are calling is a template function - the exact definition of the function you are actually calling is generated during compilation.