Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/CoreStaticInitializationHandler.cc @ 11689

Last change on this file since 11689 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
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
31#include "CoreIncludes.h"
32#include "module/ModuleInstance.h"
33#include "object/Iterator.h"
34#include "class/IdentifierManager.h"
35#include "singleton/ScopeManager.h"
36#include "command/ConsoleCommandManager.h"
37#include "commandline/CommandLineParser.h"
38
39namespace orxonox
40{
41    void CoreStaticInitializationHandler::setupHandler()
42    {
43        // initialize singletons
44        new IdentifierManager();
45        new ScopeManager();
46        new CommandLineParser();
47        new ConsoleCommandManager();
48    }
49
50    void CoreStaticInitializationHandler::shutdownHandler()
51    {
52        delete &ConsoleCommandManager::getInstance();
53        delete &CommandLineParser::getInstance();
54        delete &ScopeManager::getInstance();
55        delete &IdentifierManager::getInstance();
56    }
57
58    void CoreStaticInitializationHandler::loadModule(ModuleInstance* module)
59    {
60        this->loadInstances(module);
61        if (this->bInitInstances_)
62            this->initInstances(module);
63    }
64
65    void CoreStaticInitializationHandler::loadInstances(ModuleInstance* module)
66    {
67        // the order of initialization is important: handlers > identifiers > singletons > everything else
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    {
77        IdentifierManager::getInstance().createClassHierarchy();
78    }
79
80    void CoreStaticInitializationHandler::unloadModule(ModuleInstance* module)
81    {
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);
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
92        module->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
93        module->unloadAllStaticallyInitializedInstances(StaticInitialization::STATIC_INITIALIZATION_HANDLER);
94    }
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);
101        for (StaticallyInitializedInstance* instance : instances)
102            identifiers.insert(&static_cast<StaticallyInitializedIdentifier*>(instance)->getIdentifier());
103
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.
108        for (Identifier* identifier : identifiers)
109            identifier->destroyObjects();
110
111        // check if all objects were really destroyed. this is not the case if an object is referenced by a strong pointer from another
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).
114        for (Identifier* identifier : identifiers)
115        {
116            ObjectListBase* objectList = Context::getRootContext()->getObjectList(identifier);
117            if (objectList->size() > 0)
118            {
119                orxout(internal_error) << "There are still " << objectList->size() << " objects of type " << identifier->getName()
120                    << " after unloading the Identifier. This may lead to a crash" << endl;
121            }
122        }
123
124        // destroy object-lists in all contexts
125        for (Identifier* identifier : identifiers)
126        {
127            // only do this if the Identifier is not a Context itself; otherwise we delete the list we're iterating over
128            if (!identifier->isExactlyA(Class(Context)))
129            {
130                // iterate over all contexts
131                for (Context* context : ObjectList<Context>())
132                    context->destroyObjectList(identifier);
133            }
134        }
135    }
136}
Note: See TracBrowser for help on using the repository browser.