Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8423


Ignore:
Timestamp:
May 9, 2011, 5:06:49 AM (14 years ago)
Author:
rgrieder
Message:

Added and incorporated new class DestructionHelper: instead of doing the destruction of certain singletons implicitly via scoped_ptrs and ScopeGuards, use a method named destroy() that essentially achieves the same, but makes the destruction sequence obvious.

Location:
code/trunk/src/libraries
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/libraries/core/Core.cc

    r8366 r8423  
    9090
    9191    Core::Core(const std::string& cmdLine)
    92         // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands)
    93         : identifierDestroyer_(Identifier::destroyAllIdentifiers)
    94         // Cleanup guard for external console commands that don't belong to an Identifier
    95         , consoleCommandDestroyer_(ConsoleCommand::destroyAll)
     92        : pathConfig_(NULL)
     93        , dynLibManager_(NULL)
     94        , signalHandler_(NULL)
     95        , configFileManager_(NULL)
     96        , languageInstance_(NULL)
     97        , ioConsole_(NULL)
     98        , tclBind_(NULL)
     99        , tclThreadManager_(NULL)
     100        , rootScope_(NULL)
     101        , graphicsManager_(NULL)
     102        , inputManager_(NULL)
     103        , guiManager_(NULL)
     104        , graphicsScope_(NULL)
    96105        , bGraphicsLoaded_(false)
    97106        , bStartIOConsole_(true)
     
    99108        , ogreConfigTimestamp_(0)
    100109        , bDevMode_(false)
     110        , destructionHelper_(this)
    101111    {
    102112        // Set the hard coded fixed paths
    103         this->pathConfig_.reset(new PathConfig());
     113        this->pathConfig_ = new PathConfig();
    104114
    105115        // Create a new dynamic library manager
    106         this->dynLibManager_.reset(new DynLibManager());
     116        this->dynLibManager_ = new DynLibManager();
    107117
    108118        // Load modules
     
    128138        // create a signal handler (only active for Linux)
    129139        // This call is placed as soon as possible, but after the directories are set
    130         this->signalHandler_.reset(new SignalHandler());
     140        this->signalHandler_ = new SignalHandler();
    131141        this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log");
    132142
     
    147157
    148158        // Manage ini files and set the default settings file (usually orxonox.ini)
    149         this->configFileManager_.reset(new ConfigFileManager());
     159        this->configFileManager_ = new ConfigFileManager();
    150160        this->configFileManager_->setFilename(ConfigFileType::Settings,
    151161            CommandLineParser::getValue("settingsFile").getString());
    152162
    153163        // Required as well for the config values
    154         this->languageInstance_.reset(new Language());
     164        this->languageInstance_ = new Language();
    155165
    156166        // Do this soon after the ConfigFileManager has been created to open up the
    157167        // possibility to configure everything below here
    158         ClassIdentifier<Core>::getIdentifier("Core")->initialiseObject(this, "Core", true);
     168        RegisterRootObject(Core);
    159169        this->setConfigValues();
    160170
     
    166176        }
    167177        if (this->bStartIOConsole_)
    168             this->ioConsole_.reset(new IOConsole());
     178            this->ioConsole_ = new IOConsole();
    169179#endif
    170180
     
    173183
    174184        // Load OGRE excluding the renderer and the render window
    175         this->graphicsManager_.reset(new GraphicsManager(false));
     185        this->graphicsManager_ = new GraphicsManager(false);
    176186
    177187        // initialise Tcl
    178         this->tclBind_.reset(new TclBind(PathConfig::getDataPathString()));
    179         this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter()));
     188        this->tclBind_ = new TclBind(PathConfig::getDataPathString());
     189        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
    180190
    181191        // Create singletons that always exist (in other libraries)
    182         this->rootScope_.reset(new Scope<ScopeID::Root>());
     192        this->rootScope_ = new Scope<ScopeID::Root>();
    183193
    184194        // Generate documentation instead of normal run?
     
    198208    }
    199209
    200     /**
    201     @brief
    202         All destruction code is handled by scoped_ptrs and ScopeGuards.
    203     */
    204     Core::~Core()
     210    void Core::destroy()
    205211    {
    206212        // Remove us from the object lists again to avoid problems when destroying them
    207213        this->unregisterObject();
     214
     215        safeObjectDelete(&graphicsScope_);
     216        safeObjectDelete(&guiManager_);
     217        safeObjectDelete(&inputManager_);
     218        safeObjectDelete(&graphicsManager_);
     219        safeObjectDelete(&rootScope_);
     220        safeObjectDelete(&tclThreadManager_);
     221        safeObjectDelete(&tclBind_);
     222        safeObjectDelete(&ioConsole_);
     223        safeObjectDelete(&languageInstance_);
     224        safeObjectDelete(&configFileManager_);
     225        ConsoleCommand::destroyAll();
     226        Identifier::destroyAllIdentifiers();
     227        safeObjectDelete(&signalHandler_);
     228        safeObjectDelete(&dynLibManager_);
     229        safeObjectDelete(&pathConfig_);
    208230    }
    209231
     
    285307
    286308        // Calls the InputManager which sets up the input devices.
    287         inputManager_.reset(new InputManager());
     309        inputManager_ = new InputManager();
    288310
    289311        // Load the CEGUI interface
    290         guiManager_.reset(new GUIManager(inputManager_->getMousePosition()));
     312        guiManager_ = new GUIManager(inputManager_->getMousePosition());
    291313
    292314        bGraphicsLoaded_ = true;
     
    297319
    298320        // Create singletons associated with graphics (in other libraries)
    299         graphicsScope_.reset(new Scope<ScopeID::Graphics>());
     321        graphicsScope_ = new Scope<ScopeID::Graphics>();
    300322
    301323        unloader.Dismiss();
     
    304326    void Core::unloadGraphics()
    305327    {
    306         this->graphicsScope_.reset();
    307         this->guiManager_.reset();
    308         this->inputManager_.reset();
    309         this->graphicsManager_.reset();
     328        safeObjectDelete(&graphicsScope_);
     329        safeObjectDelete(&guiManager_);
     330        safeObjectDelete(&inputManager_);
     331        safeObjectDelete(&graphicsManager_);
    310332
    311333        // Load Ogre::Root again, but without the render system
    312334        try
    313             { this->graphicsManager_.reset(new GraphicsManager(false)); }
     335            { this->graphicsManager_ = new GraphicsManager(false); }
    314336        catch (...)
    315337        {
  • code/trunk/src/libraries/core/Core.h

    r8366 r8423  
    4545
    4646#include <string>
    47 #include <boost/scoped_ptr.hpp>
    4847#include <loki/ScopeGuard.h>
    4948
     49#include "util/DestructionHelper.h"
    5050#include "util/Singleton.h"
    5151#include "OrxonoxClass.h"
     
    6161    class _CoreExport Core : public Singleton<Core>, public OrxonoxClass
    6262    {
    63         typedef Loki::ScopeGuardImpl0<void (*)()> SimpleScopeGuard;
    6463        friend class Singleton<Core>;
    6564        friend class Game;
     
    7473            */
    7574            Core(const std::string& cmdLine);
    76             ~Core();
     75
     76            /// Leave empty and use destroy() instead
     77            ~Core() {}
     78            /// Destructor that also executes when the object fails to construct
     79            void destroy();
    7780
    7881            void setConfigValues();
     
    108111
    109112            void setThreadAffinity(int limitToCPU);
    110             // MANAGED SINGLETONS/OBJECTS
    111             // Mind the order for the destruction!
    112             scoped_ptr<PathConfig>        pathConfig_;
    113             scoped_ptr<DynLibManager>     dynLibManager_;
    114             scoped_ptr<SignalHandler>     signalHandler_;
    115             SimpleScopeGuard              identifierDestroyer_;
    116             SimpleScopeGuard              consoleCommandDestroyer_;
    117             scoped_ptr<ConfigFileManager> configFileManager_;
    118             scoped_ptr<Language>          languageInstance_;
    119             scoped_ptr<IOConsole>         ioConsole_;
    120             scoped_ptr<TclBind>           tclBind_;
    121             scoped_ptr<TclThreadManager>  tclThreadManager_;
    122             scoped_ptr<Scope<ScopeID::Root> > rootScope_;
     113
     114            PathConfig*               pathConfig_;
     115            DynLibManager*            dynLibManager_;
     116            SignalHandler*            signalHandler_;
     117            ConfigFileManager*        configFileManager_;
     118            Language*                 languageInstance_;
     119            IOConsole*                ioConsole_;
     120            TclBind*                  tclBind_;
     121            TclThreadManager*         tclThreadManager_;
     122            Scope<ScopeID::Root>*     rootScope_;
    123123            // graphical
    124             scoped_ptr<GraphicsManager>   graphicsManager_;     //!< Interface to OGRE
    125             scoped_ptr<InputManager>      inputManager_;        //!< Interface to OIS
    126             scoped_ptr<GUIManager>        guiManager_;          //!< Interface to GUI
    127             scoped_ptr<Scope<ScopeID::Graphics> > graphicsScope_;
     124            GraphicsManager*          graphicsManager_;            //!< Interface to OGRE
     125            InputManager*             inputManager_;               //!< Interface to OIS
     126            GUIManager*               guiManager_;                 //!< Interface to GUI
     127            Scope<ScopeID::Graphics>* graphicsScope_;
    128128
    129             bool                          bGraphicsLoaded_;
    130             int                           softDebugLevelLogFile_;      //!< The debug level for the log file (belongs to OutputHandler)
    131             std::string                   language_;                   //!< The language
    132             bool                          bInitRandomNumberGenerator_; //!< If true, srand(time(0)) is called
    133             bool                          bStartIOConsole_;            //!< Set to false if you don't want to use the IOConsole
    134             long long                     lastLevelTimestamp_;         ///< Timestamp when the last level was started
    135             long long                     ogreConfigTimestamp_;        ///< Timestamp wehen the ogre config level was modified
    136             bool                          bDevMode_;                   //!< Developers bit. If set to false, some options are not available as to not confuse the normal user.
     129            bool                      bGraphicsLoaded_;
     130            int                       softDebugLevelLogFile_;      //!< The debug level for the log file (belongs to OutputHandler)
     131            std::string               language_;                   //!< The language
     132            bool                      bInitRandomNumberGenerator_; //!< If true, srand(time(0)) is called
     133            bool                      bStartIOConsole_;            //!< Set to false if you don't want to use the IOConsole
     134            long long                 lastLevelTimestamp_;         ///< Timestamp when the last level was started
     135            long long                 ogreConfigTimestamp_;        ///< Timestamp wehen the ogre config level was modified
     136            bool                      bDevMode_;                   //!< Developers bit. If set to false, some options are not available as to not confuse the normal user.
    137137
    138             static Core*                  singletonPtr_s;
     138            /// Helper object that executes the surrogate destructor destroy()
     139            DestructionHelper<Core>   destructionHelper_;
     140
     141            static Core*              singletonPtr_s;
    139142    };
    140143}
  • code/trunk/src/libraries/core/GUIManager.cc

    r8419 r8423  
    162162    */
    163163    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
    164         : destroyer_(*this, &GUIManager::cleanup)
    165         , guiRenderer_(NULL)
     164        : guiRenderer_(NULL)
    166165        , resourceProvider_(NULL)
    167166#ifndef ORXONOX_OLD_CEGUI
     
    177176        , menuRootWindow_(NULL)
    178177        , camera_(NULL)
     178        , destructionHelper_(this)
    179179    {
    180180        RegisterRootObject(GUIManager);
     
    258258    }
    259259
    260     void GUIManager::cleanup()
     260    void GUIManager::destroy()
    261261    {
    262262        using namespace CEGUI;
    263263
    264264#ifdef ORXONOX_OLD_CEGUI
    265         delete guiSystem_;
    266         delete guiRenderer_;
    267         delete scriptModule_;
     265        safeObjectDelete(&guiSystem_);
     266        safeObjectDelete(&guiRenderer_);
     267        safeObjectDelete(&scriptModule_);
    268268#else
    269269        System::destroy();
     
    272272        OgreRenderer::destroy(*guiRenderer_);
    273273        LuaScriptModule::destroy(*scriptModule_);
    274         delete ceguiLogger_;
    275         delete rqListener_;
    276 #endif
    277         delete luaState_;
     274        safeObjectDelete(&ceguiLogger_);
     275        safeObjectDelete(&rqListener_);
     276#endif
     277        safeObjectDelete(&luaState_);
    278278    }
    279279
     
    285285    void GUIManager::changedGUIScheme(void)
    286286    {
    287 
    288287    }
    289288
  • code/trunk/src/libraries/core/GUIManager.h

    r8411 r8423  
    4444#include <CEGUIVersion.h>
    4545#include <boost/shared_ptr.hpp>
    46 #include <loki/ScopeGuard.h>
    4746
     47#include "util/DestructionHelper.h"
    4848#include "util/OgreForwardRefs.h"
    4949#include "util/TriBool.h"
     
    8383    public:
    8484        GUIManager(const std::pair<int, int>& mousePosition);
     85
    8586        //! Leave empty and use cleanup() instead
    8687        ~GUIManager() {}
     88        /// Destructor that also executes when object fails to construct
     89        void destroy();
    8790
    8891        void setConfigValues(void);
     
    134137        GUIManager(const GUIManager& instance); //!< private and undefined copy c'tor (this is a singleton class)
    135138
    136         /// Destructor that also executes when object fails to construct
    137         void cleanup();
    138 
    139139        void executeCode(const std::string& str);
    140140
     
    156156        virtual void windowResized(unsigned int newWidth, unsigned int newHeight);
    157157        virtual void windowFocusChanged(bool bFocus);
    158 
    159         /// Surrogate for the destructor
    160         Loki::ObjScopeGuardImpl0<GUIManager, void (GUIManager::*)()> destroyer_;
    161158
    162159#ifdef ORXONOX_OLD_CEGUI
     
    180177        Ogre::Camera*                        camera_;           //!< Camera used to render the scene with the GUI
    181178
     179        /// Helper object that executes the surrogate destructor destroy()
     180        DestructionHelper<GUIManager>        destructionHelper_;
     181
    182182        static GUIManager*                   singletonPtr_s;    //!< Singleton reference to GUIManager
    183183
  • code/trunk/src/libraries/core/Game.cc

    r8351 r8423  
    7878
    7979    Game::Game(const std::string& cmdLine)
    80         // Destroy factories before the Core!
    81         : gsFactoryDestroyer_(Game::GameStateFactory::getFactories(), &std::map<std::string, shared_ptr<GameStateFactory> >::clear)
    82     {
    83         this->bAbort_ = false;
    84         bChangingState_ = false;
    85 
     80        : gameClock_(NULL)
     81        , core_(NULL)
     82        , bChangingState_(false)
     83        , bAbort_(false)
     84        , destructionHelper_(this)
     85    {
    8686#ifdef ORXONOX_PLATFORM_WINDOWS
    8787        minimumSleepTime_ = 1000/*us*/;
     
    103103
    104104        // Set up a basic clock to keep time
    105         this->gameClock_.reset(new Clock());
     105        this->gameClock_ = new Clock();
    106106
    107107        // Create the Core
    108         this->core_.reset(new Core(cmdLine));
     108        this->core_ = new Core(cmdLine);
    109109
    110110        // Do this after the Core creation!
     
    127127    }
    128128
    129     /**
    130     @brief
    131         All destruction code is handled by scoped_ptrs and SimpleScopeGuards.
    132     */
    133     Game::~Game()
     129    void Game::destroy()
    134130    {
    135131        // Remove us from the object lists again to avoid problems when destroying them
    136132        this->unregisterObject();
     133
     134        assert(loadedStates_.size() <= 1); // Just empty root GameState
     135        // Destroy all GameStates (shared_ptrs take care of actual destruction)
     136        constructedStates_.clear();
     137
     138        GameStateFactory::getFactories().clear();
     139        safeObjectDelete(&core_);
     140        safeObjectDelete(&gameClock_);
    137141    }
    138142
  • code/trunk/src/libraries/core/Game.h

    r8418 r8423  
    4545#include <vector>
    4646#include <boost/shared_ptr.hpp>
    47 #include <boost/scoped_ptr.hpp>
    4847#include <boost/preprocessor/cat.hpp>
    49 #include <loki/ScopeGuard.h>
    5048
    5149#include "util/Debug.h"
     50#include "util/DestructionHelper.h"
    5251#include "util/Singleton.h"
    5352#include "OrxonoxClass.h"
     
    5655@brief
    5756    Adds a new GameState to the Game. The second parameter is the name as string
    58     and every following paramter is a constructor argument (which is usually non existent)
     57    and every following parameter is a constructor argument (which is usually non existent)
    5958*/
    6059#define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
     
    9291    public:
    9392        Game(const std::string& cmdLine);
    94         ~Game();
     93
     94        //! Leave empty and use cleanup() instead
     95        ~Game() {}
     96        /// Destructor that also executes when object fails to construct
     97        void destroy();
    9598
    9699        void setConfigValues();
     
    138141                { return shared_ptr<GameState>(new T(info)); }
    139142        };
    140         // For the factory destruction
    141         typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard;
    142143
    143144        struct StatisticsTickInfo
     
    166167        void resetChangingState() { this->bChangingState_ = false; }
    167168
    168         scoped_ptr<Clock>                  gameClock_;
    169         scoped_ptr<Core>                   core_;
    170         ObjScopeGuard                      gsFactoryDestroyer_;
     169        Clock*                             gameClock_;
     170        Core*                              core_;
    171171
    172172        GameStateMap                       constructedStates_;
     
    194194        unsigned int                       fpsLimit_;
    195195
     196        /// Helper object that executes the surrogate destructor destroy()
     197        DestructionHelper<Game>            destructionHelper_;
     198
    196199        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
    197200        static Game* singletonPtr_s;        //!< Pointer to the Singleton
  • code/trunk/src/libraries/core/GraphicsManager.cc

    r8366 r8423  
    9494    GraphicsManager* GraphicsManager::singletonPtr_s = 0;
    9595
    96     /**
    97     @brief
    98         Non-initialising constructor.
    99     */
    10096    GraphicsManager::GraphicsManager(bool bLoadRenderer)
    10197        : ogreWindowEventListener_(new OgreWindowEventListener())
     
    104100        , lastFrameStartTime_(0.0f)
    105101        , lastFrameEndTime_(0.0f)
     102        , destructionHelper_(this)
    106103    {
    107104        RegisterObject(GraphicsManager);
     
    134131    }
    135132
    136     /**
    137     @brief
    138         Destruction is done by the member scoped_ptrs.
    139     */
    140     GraphicsManager::~GraphicsManager()
     133    void GraphicsManager::destroy()
    141134    {
    142135        Loader::unload(debugOverlay_.get());
    143136
    144         Ogre::WindowEventUtilities::removeWindowEventListener(renderWindow_, ogreWindowEventListener_.get());
     137        Ogre::WindowEventUtilities::removeWindowEventListener(renderWindow_, ogreWindowEventListener_);
    145138        ModifyConsoleCommand(__CC_printScreen_name).resetFunction();
    146139        ModifyConsoleCommand(__CC_GraphicsManager_group, __CC_setScreenResolution_name).resetFunction();
     
    151144        Loader::unload(resources_.get());
    152145        Loader::unload(extResources_.get());
     146
     147        safeObjectDelete(&ogreRoot_);
     148        safeObjectDelete(&ogreLogger_);
     149        safeObjectDelete(&ogreWindowEventListener_);
    153150    }
    154151
     
    218215        // create a new logManager
    219216        // Ogre::Root will detect that we've already created a Log
    220         ogreLogger_.reset(new Ogre::LogManager());
     217        ogreLogger_ = new Ogre::LogManager();
    221218        COUT(4) << "Ogre LogManager created" << std::endl;
    222219
     
    241238
    242239        // Leave plugins file empty. We're going to do that part manually later
    243         ogreRoot_.reset(new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string()));
     240        ogreRoot_ = new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string());
    244241
    245242        COUT(3) << "Ogre set up done." << std::endl;
     
    300297        this->ogreWindowEventListener_->windowResized(renderWindow_);
    301298
    302         Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_.get());
     299        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_);
    303300
    304301        // create a full screen default viewport
  • code/trunk/src/libraries/core/GraphicsManager.h

    r8351 r8423  
    4848#include <string>
    4949#include <OgreLog.h>
    50 #include <boost/scoped_ptr.hpp>
    5150#include <boost/shared_ptr.hpp>
    5251
     52#include "util/DestructionHelper.h"
    5353#include "util/Singleton.h"
    5454#include "OrxonoxClass.h"
     
    6868    public:
    6969        GraphicsManager(bool bLoadRenderer = true);
    70         ~GraphicsManager();
     70
     71        //! Leave empty and use cleanup() instead
     72        ~GraphicsManager() {}
     73        /// Destructor that also executes when object fails to construct
     74        void destroy();
    7175
    7276        void setConfigValues();
     
    113117        std::string setVSync(bool vsync);
    114118
    115         scoped_ptr<OgreWindowEventListener> ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
    116         scoped_ptr<Ogre::LogManager>        ogreLogger_;
    117         scoped_ptr<Ogre::Root>              ogreRoot_;                //!< Ogre's root
     119        OgreWindowEventListener* ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
     120        Ogre::LogManager*        ogreLogger_;
     121        Ogre::Root*              ogreRoot_;                //!< Ogre's root
    118122        Ogre::RenderWindow* renderWindow_;             //!< the one and only render window
    119123        Ogre::Viewport*     viewport_;                 //!< default full size viewport
     
    134138        int                 ogreLogLevelCritical_;     //!< Corresponding Orxonox debug level for LL_CRITICAL
    135139
     140        /// Helper object that executes the surrogate destructor destroy()
     141        DestructionHelper<GraphicsManager> destructionHelper_;
     142
    136143        static GraphicsManager* singletonPtr_s;        //!< Pointer to the Singleton
    137144// tolua_begin
Note: See TracChangeset for help on using the changeset viewer.