A class template framework to dispatch control based on some value. More...
#include <dispatch.h>
Classes | |
struct | Entry |
Type of entry in dispatch table: if value is key, h will be called. More... | |
Public Types | |
typedef bool(Application::* | Handler )(Input &input, const Value &value) |
Handler type. | |
Static Public Member Functions | |
static bool | handle (Application &a, Input &input, const Value &value) |
Main handler function. | |
Static Public Attributes | |
static Entry | table [] |
Actual dispatch table. |
A class template framework to dispatch control based on some value.
The framework assumes that there are two objects:
Input input; // represents input for application Application application; // represents application
The Application class has a number of handler functions
Application::handle_value_1(Input& input, const Value& value) Application::handle_value_2(Input& input, const Value& value)
one of which should be called. Which one to call depends on a certain available value.
The class template below makes it easy to dispatch to the correct handler-function, based on the given value.
To use, an instance should define Dispatcher<Application,Input,Value>::table as in the example below.
typedef std::string Value; Dispatcher<Application,Input,Value>::Entry Dispatcher<Application,Input,Value>::table[] = { { "", &Application::default_handler }, { "a", &Application::handle_a }, { "b", &Application::handle_b }, { "c", &Application::handle_c }, { "", 0 } // sentinel };
The Application class could then define an Application::main function as follows.
class Application { public: bool main(Input& input, const Value& value) { return Dispatcher<Application,Input,Value>::handle(*this, input, value); } };
Definition at line 56 of file dispatch.h.
typedef bool(Application::* Dv::Util::Dispatcher< Application, Input, Value >::Handler)(Input &input, const Value &value) |
Handler type.
Definition at line 59 of file dispatch.h.
static bool Dv::Util::Dispatcher< Application, Input, Value >::handle | ( | Application & | a, | |
Input & | input, | |||
const Value & | value | |||
) | [inline, static] |
Main handler function.
It will use value to dispatch a call to the appropriate handler function. An actual example can be found in test-dispatch.C in the source directory.
a | application defining handler functions. | |
input | object. | |
value | used to select the handler function. |
Definition at line 84 of file dispatch.h.
References Dv::Util::Dispatcher< Application, Input, Value >::table.
Entry Dv::Util::Dispatcher< Application, Input, Value >::table[] [static] |
Actual dispatch table.
Define as in the example below.
Dispatcher<Application,Input,std::string>::Entry Dispatcher<Application,Input,std::string>::table[] = { { "", &Application::default_handler }, { "a", &Application::handle_a }, { "b", &Application::handle_b }, { "c", &Application::handle_c }, { "", 0 } // sentinel
Definition at line 75 of file dispatch.h.
Referenced by Dv::Util::Dispatcher< Application, Input, Value >::handle().
dvutil-1.0.10 | [ 5 December, 2009] |