Listing 1
struct InputObj {};
class ProcessorMgr
{
private:
typedef void (*processingFunction) (InputObj);
std::map<std::string, processingFunction> my_processors;
public:
void addProcessor(std::string input_category, processingFunction func)
{ my_processors[input_category] = func; }
void processInput(std::string input_category, InputObj i)
{ (my_processors.find(input_category)->second)(i); }
};
void processFoo(InputObj i) { /* ... */ }
void processBar(InputObj i) { /* ... */ }
int main()
{
ProcessorMgr mgr;
mgr.addProcessor("foo", processFoo);
mgr.addProcessor("bar", processBar);
InputObj x1, x2;
mgr.processInput("foo", x1);
mgr.processInput("bar", x2);
return 0;
}