| 1 | /**
|
|---|
| 2 | * @file command_factory.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Thu May 19 16:48:40 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Class to store possible commands.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifdef HAVE_CONFIG_H
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | #include <cstdio>
|
|---|
| 15 |
|
|---|
| 16 | #include "base/command.hpp"
|
|---|
| 17 | #include "base/command_factory.hpp"
|
|---|
| 18 | #include "base/defs.hpp"
|
|---|
| 19 | #include "base/proxy.hpp"
|
|---|
| 20 |
|
|---|
| 21 | using namespace VMG;
|
|---|
| 22 |
|
|---|
| 23 | CommandFactory::CommandFactory()
|
|---|
| 24 | {
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | CommandFactory::~CommandFactory()
|
|---|
| 28 | {
|
|---|
| 29 | for (std::map<std::string, CommandProxyBase*>::iterator iter=proxy_map.begin(); iter!=proxy_map.end(); ++iter)
|
|---|
| 30 | delete iter->second;
|
|---|
| 31 |
|
|---|
| 32 | for (std::map<std::string, Command*>::iterator iter=instance_map.begin(); iter!=instance_map.end(); ++iter)
|
|---|
| 33 | delete iter->second;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void CommandFactory::Register(CommandProxyBase* object)
|
|---|
| 37 | {
|
|---|
| 38 | proxy_map.insert(std::make_pair(object->Name(), object));
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | Command* CommandFactory::Get(const std::string& id)
|
|---|
| 42 | {
|
|---|
| 43 | std::map<std::string, Command*>::iterator iter_instance = instance_map.find(id);
|
|---|
| 44 | if (iter_instance != instance_map.end())
|
|---|
| 45 | return iter_instance->second;
|
|---|
| 46 |
|
|---|
| 47 | std::map<std::string, CommandProxyBase*>::iterator iter_proxy = proxy_map.find(id);
|
|---|
| 48 | if (iter_proxy != proxy_map.end()) {
|
|---|
| 49 | Command *command = iter_proxy->second->CreateCommand();
|
|---|
| 50 | instance_map.insert(std::make_pair(id, command));
|
|---|
| 51 | return command;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | assert(0 == "This command is not registered.");
|
|---|
| 55 |
|
|---|
| 56 | return NULL;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void CommandFactory::Delete(const std::string& id)
|
|---|
| 60 | {
|
|---|
| 61 | std::map<std::string, CommandProxyBase*>::iterator iter_proxy = proxy_map.find(id);
|
|---|
| 62 |
|
|---|
| 63 | if (iter_proxy != proxy_map.end()) {
|
|---|
| 64 | delete iter_proxy->second;
|
|---|
| 65 | proxy_map.erase(iter_proxy);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | std::map<std::string, Command*>::iterator iter_instance = instance_map.find(id);
|
|---|
| 69 |
|
|---|
| 70 | if (iter_instance != instance_map.end()) {
|
|---|
| 71 | delete iter_instance->second;
|
|---|
| 72 | instance_map.erase(iter_instance);
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | bool CommandFactory::CheckNumberOfArguments(const std::string& id, const int& num_arguments)
|
|---|
| 78 | {
|
|---|
| 79 | std::map<std::string, CommandProxyBase*>::iterator iter = proxy_map.find(id);
|
|---|
| 80 |
|
|---|
| 81 | assert(iter != proxy_map.end());
|
|---|
| 82 | assert(iter->second->Arguments() == num_arguments);
|
|---|
| 83 |
|
|---|
| 84 | return iter->second->Arguments() == num_arguments;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | void CommandFactory::PrintAvailableCommands()
|
|---|
| 89 | {
|
|---|
| 90 | for (std::map<std::string, CommandProxyBase*>::iterator iter=proxy_map.begin(); iter!=proxy_map.end(); ++iter)
|
|---|
| 91 | printf("%s\n", iter->second->Name());
|
|---|
| 92 | }
|
|---|