| [48b662] | 1 | /**
|
|---|
| 2 | * @file factory.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Tue Apr 5 20:40:05 2011
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifdef HAVE_CONFIG_H
|
|---|
| 8 | #include <config.h>
|
|---|
| 9 | #endif
|
|---|
| 10 |
|
|---|
| 11 | #include <cassert>
|
|---|
| 12 | #include <cstdio>
|
|---|
| 13 | #include <iostream>
|
|---|
| 14 |
|
|---|
| 15 | #include "base/command.hpp"
|
|---|
| 16 | #include "base/discretization.hpp"
|
|---|
| 17 | #include "base/factory.hpp"
|
|---|
| 18 | #include "base/object.hpp"
|
|---|
| 19 | #include "comm/comm.hpp"
|
|---|
| 20 | #include "grid/multigrid.hpp"
|
|---|
| 21 | #include "grid/tempgrid.hpp"
|
|---|
| 22 | #include "level/level_operator.hpp"
|
|---|
| 23 | #include "smoother/smoother.hpp"
|
|---|
| 24 | #include "solver/solver.hpp"
|
|---|
| 25 | #include "mg.hpp"
|
|---|
| 26 |
|
|---|
| 27 | using namespace VMG;
|
|---|
| 28 |
|
|---|
| 29 | Factory::Factory()
|
|---|
| 30 | {
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | Factory::~Factory()
|
|---|
| 34 | {
|
|---|
| 35 | Destroy();
|
|---|
| 36 |
|
|---|
| 37 | for (Factory::command_iterator iter=command_map.begin(); iter!=command_map.end(); ++iter)
|
|---|
| 38 | delete iter->second;
|
|---|
| 39 |
|
|---|
| 40 | command_map.clear();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void Factory::Destroy()
|
|---|
| 44 | {
|
|---|
| 45 | for (Factory::instance_iterator iter=command_instance_map.begin(); iter!=command_instance_map.end(); ++iter)
|
|---|
| 46 | delete iter->second;
|
|---|
| 47 |
|
|---|
| 48 | for (Factory::object_iterator iter=object_map.begin(); iter!=object_map.end(); ++iter)
|
|---|
| 49 | delete iter->second;
|
|---|
| 50 |
|
|---|
| 51 | command_instance_map.clear();
|
|---|
| 52 | object_map.clear();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | void Factory::RegisterCommand(CommandProxyBase* object)
|
|---|
| 56 | {
|
|---|
| 57 | if (command_map.find(object->Name()) == command_map.end())
|
|---|
| 58 | command_map.insert(std::pair<std::string, CommandProxyBase*>(object->Name(), object));
|
|---|
| 59 | else
|
|---|
| 60 | assert(0 == "A command with the same unique name has already been registered.");
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void Factory::RegisterObject(Object* object)
|
|---|
| 64 | {
|
|---|
| 65 | Factory::object_iterator iter = object_map.find(object->Name());
|
|---|
| 66 |
|
|---|
| 67 | if (iter != object_map.end()) {
|
|---|
| 68 | delete iter->second;
|
|---|
| 69 | object_map.erase(iter);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | object_map.insert(std::pair<std::string, Object*>(object->Name(), object));
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | Command* Factory::GetCommand(std::string id)
|
|---|
| 76 | {
|
|---|
| 77 | MG::Instance();
|
|---|
| 78 | #ifdef DEBUG
|
|---|
| 79 | //std::cout << id << std::endl;
|
|---|
| 80 | #endif
|
|---|
| 81 |
|
|---|
| 82 | Factory::instance_iterator iter1 = command_instance_map.find(id);
|
|---|
| 83 | if (iter1 != command_instance_map.end())
|
|---|
| 84 | return iter1->second;
|
|---|
| 85 |
|
|---|
| 86 | Factory::command_iterator iter2 = command_map.find(id);
|
|---|
| 87 | if (iter2 != command_map.end()) {
|
|---|
| 88 | Command *command = iter2->second->CreateCommand();
|
|---|
| 89 | command_instance_map.insert(std::pair<std::string, Command*>(id, command));
|
|---|
| 90 | return command;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | assert(0 == "This command is not registered.");
|
|---|
| 94 |
|
|---|
| 95 | return NULL;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | bool Factory::CheckNumberOfArguments(std::string id, const int& num_arguments)
|
|---|
| 99 | {
|
|---|
| 100 | Factory::command_iterator iter = command_map.find(id);
|
|---|
| 101 |
|
|---|
| 102 | assert(iter != command_map.end());
|
|---|
| 103 | assert(iter->second->Arguments() == num_arguments);
|
|---|
| 104 |
|
|---|
| 105 | return iter->second->Arguments() == num_arguments;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | Object* Factory::GetObject(std::string id)
|
|---|
| 109 | {
|
|---|
| 110 | MG::Instance();
|
|---|
| 111 |
|
|---|
| 112 | Factory::object_iterator iter = object_map.find(id);
|
|---|
| 113 | if (iter != object_map.end())
|
|---|
| 114 | return iter->second;
|
|---|
| 115 |
|
|---|
| 116 | assert(0 == "This object is not registered.");
|
|---|
| 117 |
|
|---|
| 118 | return NULL;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void Factory::DeleteCommand(std::string id)
|
|---|
| 122 | {
|
|---|
| 123 | Factory::command_iterator iter1 = command_map.find(id);
|
|---|
| 124 |
|
|---|
| 125 | if (iter1 != command_map.end()) {
|
|---|
| 126 | delete iter1->second;
|
|---|
| 127 | command_map.erase(iter1);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | Factory::instance_iterator iter2 = command_instance_map.find(id);
|
|---|
| 131 |
|
|---|
| 132 | if (iter2 != command_instance_map.end()) {
|
|---|
| 133 | delete iter2->second;
|
|---|
| 134 | command_instance_map.erase(iter2);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | void Factory::DeleteObject(std::string id)
|
|---|
| 139 | {
|
|---|
| 140 | Factory::object_iterator iter = object_map.find(id);
|
|---|
| 141 |
|
|---|
| 142 | if (iter != object_map.end()) {
|
|---|
| 143 | delete iter->second;
|
|---|
| 144 | object_map.erase(iter);
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | void Factory::PrintAvailableCommands()
|
|---|
| 149 | {
|
|---|
| 150 | for (Factory::command_iterator iter=command_map.begin(); iter!=command_map.end(); ++iter)
|
|---|
| 151 | printf("%s\n", iter->second->Name());
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void Factory::PrintAvailableObjects()
|
|---|
| 155 | {
|
|---|
| 156 | for (Factory::object_iterator iter=object_map.begin(); iter!=object_map.end(); ++iter)
|
|---|
| 157 | printf("%s\n", iter->second->Name().c_str());
|
|---|
| 158 | }
|
|---|