Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Dv::Cgi::Dispatcher< Application > Class Template Reference

A class template framework to dispatch the control of a Cgi program based on a key in the formdata. More...

#include <dispatch.h>

Collaboration diagram for Dv::Cgi::Dispatcher< Application >:

Collaboration graph
[legend]
List of all members.

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 []

Detailed Description

template<class Application>
class Dv::Cgi::Dispatcher< Application >

A class template framework to dispatch the control of a Cgi program based on a key in the formdata.

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");
     }
 };
See also:
Dv::Cgi::Cgi

Definition at line 68 of file dispatch.h.


Member Typedef Documentation

template<class Application>
typedef bool(Application::* Dv::Cgi::Dispatcher< Application >::Handler)(const Dv::Cgi::Cgi& cgi, const std::string& value)
 

Definition at line 70 of file dispatch.h.


Member Function Documentation

template<class Application>
bool Dv::Cgi::Dispatcher< Application >::handle Application &  c,
Dv::Cgi::Cgi cgi,
const std::string &  key
[inline, static]
 

Definition at line 73 of file dispatch.h.

References Props::find(), Dv::Cgi::Entry::h, Dv::Cgi::Entry::key, and Dv::Cgi::Cgi::props().


Member Data Documentation

template<class Application>
Entry Dv::Cgi::Dispatcher< Application >::table[] [static]
 

Definition at line 72 of file dispatch.h.


The documentation for this class was generated from the following file:
dvcgi-0.5.9 [10 August, 2003]