Changeset dfed1c for src/base/factory.cpp
- Timestamp:
- Nov 22, 2011, 9:22:10 PM (14 years ago)
- Children:
- facba0
- Parents:
- 66f24d
- File:
-
- 1 edited
-
src/base/factory.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/base/factory.cpp
r66f24d rdfed1c 33 33 Factory::~Factory() 34 34 { 35 Destroy(); 36 37 for (Factory::command_iterator iter=command_map.begin(); iter!=command_map.end(); ++iter) 35 for (std::map<std::string, Object*>::iterator iter=object_map.begin(); iter!=object_map.end(); ++iter) 38 36 delete iter->second; 39 40 command_map.clear();41 37 } 42 38 43 void Factory:: Destroy()39 void Factory::Register(Object* object) 44 40 { 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(); 41 Delete(object->Name()); 42 object_map.insert(std::make_pair(object->Name(), object)); 53 43 } 54 44 55 void Factory::RegisterCommand(CommandProxyBase* object)45 Object* Factory::Get(std::string id) 56 46 { 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 } 47 std::map<std::string, Object*>::iterator iter = object_map.find(id); 62 48 63 void Factory::RegisterObject(Object* object) 64 { 65 Factory::object_iterator iter = object_map.find(object->Name()); 49 if (iter != object_map.end()) 50 return iter->second; 66 51 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."); 52 MG::GetComm()->PrintStringOnce("Error: Object %s is not registered", id.c_str()); 53 assert(0); 94 54 95 55 return NULL; 96 56 } 97 57 98 bool Factory::CheckNumberOfArguments(std::string id, const int& num_arguments)58 void Factory::Delete(std::string id) 99 59 { 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); 60 std::map<std::string, Object*>::iterator iter = object_map.find(id); 141 61 142 62 if (iter != object_map.end()) { … … 146 66 } 147 67 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 68 void Factory::PrintAvailableObjects() 155 69 { 156 for (Factory::object_iterator iter=object_map.begin(); iter!=object_map.end(); ++iter) 157 printf("%s\n", iter->second->Name().c_str()); 70 MG::GetComm()->PrintStringOnce("Registered objects:"); 71 for (std::map<std::string, Object*>::iterator iter=object_map.begin(); iter!=object_map.end(); ++iter) 72 MG::GetComm()->PrintStringOnce("%s", iter->second->Name().c_str()); 158 73 }
Note:
See TracChangeset
for help on using the changeset viewer.
