#include <list>
#include <map>
#include "Types/Module.hpp"
#include "Types/Format.hpp"
class DispatchInterface {
public:
virtual void dispatch( Command *command ) = 0;
};
class ModuleMapper {
public:
void addModule( Module *module )
{
modules.push_back(module);
}
void addMapping( Address address, DispatchInterface *dispatcher )
{
dispatchAddressMap[address] = dispatcher;
}
Module *findModuleWithInputFormat( Format format )
{
for ( std::list<Module *>::iterator it = modules.begin(); it != modules.end(); ++it ) {
if ( (*it)->inputFormat() == format ) {
return (*it);
}
}
return 0;
}
Module *findModuleWithOutputFormat( Format format )
{
for ( std::list<Module *>::iterator it = modules.begin(); it != modules.end(); ++it ) {
if ( (*it)->outputFormat() == format ) {
return (*it);
}
}
}
DispatchInterface *lookup( Address address )
{
return dispatchAddressMap[address];
}
void dispatchCommand( Address address, Commands command, const void *arg )
{
Command *cmd = new Command;
cmd->command = command;
cmd->arg = arg;
cmd->address = address;
// lookup( cmd->address )->dispatch( cmd );
address->command( cmd->command, cmd->arg );
}
private:
std::list<Module*> modules;
std::map<Address,DispatchInterface*> dispatchAddressMap;
std::multimap<Format,Module*> inputFormatModuleMap;
std::multimap<Format,Module*> outputFormatModuleMap;
};
ModuleMapper *moduleMapper()
{
static ModuleMapper *staticModuleMapper = 0;
return staticModuleMapper ? staticModuleMapper : staticModuleMapper = new ModuleMapper;
}