#include <dispatch.h>
Collaboration diagram for Dv::Cgi::Dispatcher< Application >:
Public Types | |
typedef bool(Application::* | Handler )(const Dv::Cgi::Cgi &cgi, const std::string &value) |
Static Public Member Functions | |
bool | handle (Application &c, Dv::Cgi::Cgi &cgi, const std::string &key) |
Static Public Attributes | |
Entry | table [] |
We assume that a cgi application has two main objects: a Dv::Cgi::Cgi input object and an application object of class Application.
The main function would construct both
int main(int, char**) { .. // get configuration info Application application(config-info) Dv::Cgi::Cgi cgi(..) if (application.main(cgi)) return 0; else { cgi.header().content_type("text/plain"); cgi << "Error: " << ... << std::endl; return 1; } }
We also assume that there is a single formdata key that determines the action to take, e.g. a key "opcode". Moreover, for each relevant key value, there should be a member function
Application::handle_value(Dv::Cgi::Cgi& cgi, const std::string& value);
The class template below makes it easy to dispatch to the correct handler-function, based on the value of the specified formdata key.
To use, an instance should define Dispatcher<Application>::table as in the example below.
Dispatcher<Application>::Entry Dispatcher<Application>::table[] = { { "", &Application::default_handler }, { "a", &Application::handle_a }, { "b", &Application::handle_b }, { "c", &Application::handle_c }, { "", &Application::default_handler } // if "opcode" not defined, should be last };
The main function in the Application could use a Dispatcher as shown below.
class Application { public: bool main(const Dv::Cgi::Cgi& cgi); return Dispatcher<Application>::handle(*this, cgi, "opcode"); } };
Definition at line 68 of file dispatch.h.
|
Definition at line 70 of file dispatch.h. |
|
Definition at line 73 of file dispatch.h. References Props::find(), Dv::Cgi::Entry::h, Dv::Cgi::Entry::key, and Dv::Cgi::Cgi::props(). |
|
Definition at line 72 of file dispatch.h. |
dvcgi-0.5.9 | [10 August, 2003] |