[10531] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "CoreStaticInitializationHandler.h" |
---|
| 30 | |
---|
[10539] | 31 | #include "CoreIncludes.h" |
---|
[10532] | 32 | #include "module/ModuleInstance.h" |
---|
[10542] | 33 | #include "object/Iterator.h" |
---|
[10532] | 34 | #include "class/IdentifierManager.h" |
---|
[10542] | 35 | #include "singleton/ScopeManager.h" |
---|
| 36 | #include "command/ConsoleCommandManager.h" |
---|
| 37 | #include "commandline/CommandLineParser.h" |
---|
[10532] | 38 | |
---|
[10531] | 39 | namespace orxonox |
---|
| 40 | { |
---|
[10532] | 41 | void CoreStaticInitializationHandler::setupHandler() |
---|
| 42 | { |
---|
[10542] | 43 | // initialize singletons |
---|
| 44 | new IdentifierManager(); |
---|
| 45 | new ScopeManager(); |
---|
| 46 | new CommandLineParser(); |
---|
| 47 | new ConsoleCommandManager(); |
---|
[10532] | 48 | } |
---|
| 49 | |
---|
| 50 | void CoreStaticInitializationHandler::shutdownHandler() |
---|
| 51 | { |
---|
[10542] | 52 | delete &ConsoleCommandManager::getInstance(); |
---|
| 53 | delete &CommandLineParser::getInstance(); |
---|
| 54 | delete &ScopeManager::getInstance(); |
---|
| 55 | delete &IdentifierManager::getInstance(); |
---|
[10532] | 56 | } |
---|
| 57 | |
---|
| 58 | void CoreStaticInitializationHandler::loadModule(ModuleInstance* module) |
---|
| 59 | { |
---|
[10535] | 60 | this->loadInstances(module); |
---|
| 61 | if (this->bInitInstances_) |
---|
| 62 | this->initInstances(module); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void CoreStaticInitializationHandler::loadInstances(ModuleInstance* module) |
---|
| 66 | { |
---|
[10539] | 67 | // the order of initialization is important: handlers > identifiers > singletons > everything else |
---|
[10535] | 68 | module->loadAllStaticallyInitializedInstances(StaticInitialization::STATIC_INITIALIZATION_HANDLER); |
---|
| 69 | module->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
| 70 | module->loadAllStaticallyInitializedInstances(StaticInitialization::SCOPED_SINGLETON_WRAPPER); |
---|
| 71 | module->loadAllStaticallyInitializedInstances(StaticInitialization::COMMAND_LINE_ARGUMENT); |
---|
| 72 | module->loadAllStaticallyInitializedInstances(StaticInitialization::CONSOLE_COMMAND); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void CoreStaticInitializationHandler::initInstances(ModuleInstance* module) |
---|
| 76 | { |
---|
[10532] | 77 | IdentifierManager::getInstance().createClassHierarchy(); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void CoreStaticInitializationHandler::unloadModule(ModuleInstance* module) |
---|
| 81 | { |
---|
[10535] | 82 | // inverted order of initialization |
---|
| 83 | module->unloadAllStaticallyInitializedInstances(StaticInitialization::CONSOLE_COMMAND); |
---|
| 84 | module->unloadAllStaticallyInitializedInstances(StaticInitialization::COMMAND_LINE_ARGUMENT); |
---|
| 85 | module->unloadAllStaticallyInitializedInstances(StaticInitialization::SCOPED_SINGLETON_WRAPPER); |
---|
[10539] | 86 | |
---|
| 87 | // until now every object (including singletons) of the unloaded identifiers should have been destroyed in a controlled manner. |
---|
| 88 | // every remaining object is now destroyed in random order. |
---|
| 89 | this->destroyObjects(module); |
---|
| 90 | |
---|
| 91 | // all objects are gone now and we can unload identifiers |
---|
[10535] | 92 | module->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER); |
---|
| 93 | module->unloadAllStaticallyInitializedInstances(StaticInitialization::STATIC_INITIALIZATION_HANDLER); |
---|
[10532] | 94 | } |
---|
[10539] | 95 | |
---|
| 96 | void CoreStaticInitializationHandler::destroyObjects(ModuleInstance* module) |
---|
| 97 | { |
---|
| 98 | // collect all identifiers that are about to be unloaded |
---|
| 99 | std::set<Identifier*> identifiers; |
---|
| 100 | const std::set<StaticallyInitializedInstance*>& instances = module->getInstances(StaticInitialization::IDENTIFIER); |
---|
[11071] | 101 | for (StaticallyInitializedInstance* instance : instances) |
---|
| 102 | identifiers.insert(&static_cast<StaticallyInitializedIdentifier*>(instance)->getIdentifier()); |
---|
[10539] | 103 | |
---|
[10555] | 104 | // destroy objects. some objects may survive this at first because they still have strong pointers pointing at them. this is |
---|
| 105 | // ok as long as those strong pointers are held by objects that are also about to be destroyed in the same loop. this means |
---|
| 106 | // that objects within one module may reference each other by strong pointers. but it is not allowed that objects from another |
---|
| 107 | // module (which is not unloaded) uses strong pointers to point at objects inside the unloaded module. this will lead to a crash. |
---|
[11071] | 108 | for (Identifier* identifier : identifiers) |
---|
| 109 | identifier->destroyObjects(); |
---|
[10539] | 110 | |
---|
[10555] | 111 | // check if all objects were really destroyed. this is not the case if an object is referenced by a strong pointer from another |
---|
[10539] | 112 | // module (or if two objects inside this module reference each other). this will lead to a crash and must be fixed (e.g. by |
---|
| 113 | // changing object dependencies; or by changing the logic that allows modules to be unloaded). |
---|
[11071] | 114 | for (Identifier* identifier : identifiers) |
---|
[10539] | 115 | { |
---|
[11071] | 116 | ObjectListBase* objectList = Context::getRootContext()->getObjectList(identifier); |
---|
[10539] | 117 | if (objectList->size() > 0) |
---|
| 118 | { |
---|
[11071] | 119 | orxout(internal_error) << "There are still " << objectList->size() << " objects of type " << identifier->getName() |
---|
[10539] | 120 | << " after unloading the Identifier. This may lead to a crash" << endl; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // destroy object-lists in all contexts |
---|
[11071] | 125 | for (Identifier* identifier : identifiers) |
---|
[10539] | 126 | { |
---|
| 127 | // only do this if the Identifier is not a Context itself; otherwise we delete the list we're iterating over |
---|
[11071] | 128 | if (!identifier->isExactlyA(Class(Context))) |
---|
[10539] | 129 | { |
---|
| 130 | // iterate over all contexts |
---|
[11071] | 131 | for (Context* context : ObjectList<Context>()) |
---|
| 132 | context->destroyObjectList(identifier); |
---|
[10539] | 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
[10531] | 136 | } |
---|