[Orxonox-commit 3743] r8423 - in code/trunk/src/libraries: core util

rgrieder at orxonox.net rgrieder at orxonox.net
Mon May 9 05:06:49 CEST 2011


Author: rgrieder
Date: 2011-05-09 05:06:49 +0200 (Mon, 09 May 2011)
New Revision: 8423

Added:
   code/trunk/src/libraries/util/DestructionHelper.h
Modified:
   code/trunk/src/libraries/core/Core.cc
   code/trunk/src/libraries/core/Core.h
   code/trunk/src/libraries/core/GUIManager.cc
   code/trunk/src/libraries/core/GUIManager.h
   code/trunk/src/libraries/core/Game.cc
   code/trunk/src/libraries/core/Game.h
   code/trunk/src/libraries/core/GraphicsManager.cc
   code/trunk/src/libraries/core/GraphicsManager.h
Log:
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.

Modified: code/trunk/src/libraries/core/Core.cc
===================================================================
--- code/trunk/src/libraries/core/Core.cc	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/Core.cc	2011-05-09 03:06:49 UTC (rev 8423)
@@ -89,21 +89,31 @@
 #endif
 
     Core::Core(const std::string& cmdLine)
-        // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands)
-        : identifierDestroyer_(Identifier::destroyAllIdentifiers)
-        // Cleanup guard for external console commands that don't belong to an Identifier
-        , consoleCommandDestroyer_(ConsoleCommand::destroyAll)
+        : pathConfig_(NULL)
+        , dynLibManager_(NULL)
+        , signalHandler_(NULL)
+        , configFileManager_(NULL)
+        , languageInstance_(NULL)
+        , ioConsole_(NULL)
+        , tclBind_(NULL)
+        , tclThreadManager_(NULL)
+        , rootScope_(NULL)
+        , graphicsManager_(NULL)
+        , inputManager_(NULL)
+        , guiManager_(NULL)
+        , graphicsScope_(NULL)
         , bGraphicsLoaded_(false)
         , bStartIOConsole_(true)
         , lastLevelTimestamp_(0)
         , ogreConfigTimestamp_(0)
         , bDevMode_(false)
+        , destructionHelper_(this)
     {
         // Set the hard coded fixed paths
-        this->pathConfig_.reset(new PathConfig());
+        this->pathConfig_ = new PathConfig();
 
         // Create a new dynamic library manager
-        this->dynLibManager_.reset(new DynLibManager());
+        this->dynLibManager_ = new DynLibManager();
 
         // Load modules
         const std::vector<std::string>& modulePaths = this->pathConfig_->getModulePaths();
@@ -127,7 +137,7 @@
 
         // create a signal handler (only active for Linux)
         // This call is placed as soon as possible, but after the directories are set
-        this->signalHandler_.reset(new SignalHandler());
+        this->signalHandler_ = new SignalHandler();
         this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log");
 
         // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used
@@ -146,16 +156,16 @@
 #endif
 
         // Manage ini files and set the default settings file (usually orxonox.ini)
-        this->configFileManager_.reset(new ConfigFileManager());
+        this->configFileManager_ = new ConfigFileManager();
         this->configFileManager_->setFilename(ConfigFileType::Settings,
             CommandLineParser::getValue("settingsFile").getString());
 
         // Required as well for the config values
-        this->languageInstance_.reset(new Language());
+        this->languageInstance_ = new Language();
 
         // Do this soon after the ConfigFileManager has been created to open up the
         // possibility to configure everything below here
-        ClassIdentifier<Core>::getIdentifier("Core")->initialiseObject(this, "Core", true);
+        RegisterRootObject(Core);
         this->setConfigValues();
 
 #if !defined(ORXONOX_PLATFORM_APPLE) && !defined(ORXONOX_USE_WINMAIN)
@@ -165,21 +175,21 @@
             ModifyConfigValue(bStartIOConsole_, tset, false);
         }
         if (this->bStartIOConsole_)
-            this->ioConsole_.reset(new IOConsole());
+            this->ioConsole_ = new IOConsole();
 #endif
 
         // creates the class hierarchy for all classes with factories
         Identifier::createClassHierarchy();
 
         // Load OGRE excluding the renderer and the render window
-        this->graphicsManager_.reset(new GraphicsManager(false));
+        this->graphicsManager_ = new GraphicsManager(false);
 
         // initialise Tcl
-        this->tclBind_.reset(new TclBind(PathConfig::getDataPathString()));
-        this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter()));
+        this->tclBind_ = new TclBind(PathConfig::getDataPathString());
+        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
 
         // Create singletons that always exist (in other libraries)
-        this->rootScope_.reset(new Scope<ScopeID::Root>());
+        this->rootScope_ = new Scope<ScopeID::Root>();
 
         // Generate documentation instead of normal run?
         std::string docFilename;
@@ -197,14 +207,26 @@
         }
     }
 
-    /**
-    @brief
-        All destruction code is handled by scoped_ptrs and ScopeGuards.
-    */
-    Core::~Core()
+    void Core::destroy()
     {
         // Remove us from the object lists again to avoid problems when destroying them
         this->unregisterObject();
+
+        safeObjectDelete(&graphicsScope_);
+        safeObjectDelete(&guiManager_);
+        safeObjectDelete(&inputManager_);
+        safeObjectDelete(&graphicsManager_);
+        safeObjectDelete(&rootScope_);
+        safeObjectDelete(&tclThreadManager_);
+        safeObjectDelete(&tclBind_);
+        safeObjectDelete(&ioConsole_);
+        safeObjectDelete(&languageInstance_);
+        safeObjectDelete(&configFileManager_);
+        ConsoleCommand::destroyAll();
+        Identifier::destroyAllIdentifiers();
+        safeObjectDelete(&signalHandler_);
+        safeObjectDelete(&dynLibManager_);
+        safeObjectDelete(&pathConfig_);
     }
 
     //! Function to collect the SetConfigValue-macro calls.
@@ -284,10 +306,10 @@
         }
 
         // Calls the InputManager which sets up the input devices.
-        inputManager_.reset(new InputManager());
+        inputManager_ = new InputManager();
 
         // Load the CEGUI interface
-        guiManager_.reset(new GUIManager(inputManager_->getMousePosition()));
+        guiManager_ = new GUIManager(inputManager_->getMousePosition());
 
         bGraphicsLoaded_ = true;
         GameMode::bShowsGraphics_s = true;
@@ -296,21 +318,21 @@
         graphicsManager_->loadDebugOverlay();
 
         // Create singletons associated with graphics (in other libraries)
-        graphicsScope_.reset(new Scope<ScopeID::Graphics>());
+        graphicsScope_ = new Scope<ScopeID::Graphics>();
 
         unloader.Dismiss();
     }
 
     void Core::unloadGraphics()
     {
-        this->graphicsScope_.reset();
-        this->guiManager_.reset();
-        this->inputManager_.reset();
-        this->graphicsManager_.reset();
+        safeObjectDelete(&graphicsScope_);
+        safeObjectDelete(&guiManager_);
+        safeObjectDelete(&inputManager_);
+        safeObjectDelete(&graphicsManager_);
 
         // Load Ogre::Root again, but without the render system
         try
-            { this->graphicsManager_.reset(new GraphicsManager(false)); }
+            { this->graphicsManager_ = new GraphicsManager(false); }
         catch (...)
         {
             COUT(0) << "An exception occurred during 'unloadGraphics':" << Exception::handleMessage() << std::endl

Modified: code/trunk/src/libraries/core/Core.h
===================================================================
--- code/trunk/src/libraries/core/Core.h	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/Core.h	2011-05-09 03:06:49 UTC (rev 8423)
@@ -44,9 +44,9 @@
 #include "CorePrereqs.h"
 
 #include <string>
-#include <boost/scoped_ptr.hpp>
 #include <loki/ScopeGuard.h>
 
+#include "util/DestructionHelper.h"
 #include "util/Singleton.h"
 #include "OrxonoxClass.h"
 
@@ -60,7 +60,6 @@
     */
     class _CoreExport Core : public Singleton<Core>, public OrxonoxClass
     {
-        typedef Loki::ScopeGuardImpl0<void (*)()> SimpleScopeGuard;
         friend class Singleton<Core>;
         friend class Game;
 
@@ -73,8 +72,12 @@
                 GeneralException
             */
             Core(const std::string& cmdLine);
-            ~Core();
 
+            /// Leave empty and use destroy() instead
+            ~Core() {}
+            /// Destructor that also executes when the object fails to construct
+            void destroy();
+
             void setConfigValues();
 
             //! Returns the configured language.
@@ -107,35 +110,35 @@
             void unloadGraphics();
 
             void setThreadAffinity(int limitToCPU);
-            // MANAGED SINGLETONS/OBJECTS
-            // Mind the order for the destruction!
-            scoped_ptr<PathConfig>        pathConfig_;
-            scoped_ptr<DynLibManager>     dynLibManager_;
-            scoped_ptr<SignalHandler>     signalHandler_;
-            SimpleScopeGuard              identifierDestroyer_;
-            SimpleScopeGuard              consoleCommandDestroyer_;
-            scoped_ptr<ConfigFileManager> configFileManager_;
-            scoped_ptr<Language>          languageInstance_;
-            scoped_ptr<IOConsole>         ioConsole_;
-            scoped_ptr<TclBind>           tclBind_;
-            scoped_ptr<TclThreadManager>  tclThreadManager_;
-            scoped_ptr<Scope<ScopeID::Root> > rootScope_;
+
+            PathConfig*               pathConfig_;
+            DynLibManager*            dynLibManager_;
+            SignalHandler*            signalHandler_;
+            ConfigFileManager*        configFileManager_;
+            Language*                 languageInstance_;
+            IOConsole*                ioConsole_;
+            TclBind*                  tclBind_;
+            TclThreadManager*         tclThreadManager_;
+            Scope<ScopeID::Root>*     rootScope_;
             // graphical
-            scoped_ptr<GraphicsManager>   graphicsManager_;     //!< Interface to OGRE
-            scoped_ptr<InputManager>      inputManager_;        //!< Interface to OIS
-            scoped_ptr<GUIManager>        guiManager_;          //!< Interface to GUI
-            scoped_ptr<Scope<ScopeID::Graphics> > graphicsScope_;
+            GraphicsManager*          graphicsManager_;            //!< Interface to OGRE
+            InputManager*             inputManager_;               //!< Interface to OIS
+            GUIManager*               guiManager_;                 //!< Interface to GUI
+            Scope<ScopeID::Graphics>* graphicsScope_;
 
-            bool                          bGraphicsLoaded_;
-            int                           softDebugLevelLogFile_;      //!< The debug level for the log file (belongs to OutputHandler)
-            std::string                   language_;                   //!< The language
-            bool                          bInitRandomNumberGenerator_; //!< If true, srand(time(0)) is called
-            bool                          bStartIOConsole_;            //!< Set to false if you don't want to use the IOConsole
-            long long                     lastLevelTimestamp_;         ///< Timestamp when the last level was started
-            long long                     ogreConfigTimestamp_;        ///< Timestamp wehen the ogre config level was modified
-            bool                          bDevMode_;                   //!< Developers bit. If set to false, some options are not available as to not confuse the normal user.
+            bool                      bGraphicsLoaded_;
+            int                       softDebugLevelLogFile_;      //!< The debug level for the log file (belongs to OutputHandler)
+            std::string               language_;                   //!< The language
+            bool                      bInitRandomNumberGenerator_; //!< If true, srand(time(0)) is called
+            bool                      bStartIOConsole_;            //!< Set to false if you don't want to use the IOConsole
+            long long                 lastLevelTimestamp_;         ///< Timestamp when the last level was started
+            long long                 ogreConfigTimestamp_;        ///< Timestamp wehen the ogre config level was modified
+            bool                      bDevMode_;                   //!< Developers bit. If set to false, some options are not available as to not confuse the normal user.
 
-            static Core*                  singletonPtr_s;
+            /// Helper object that executes the surrogate destructor destroy()
+            DestructionHelper<Core>   destructionHelper_;
+
+            static Core*              singletonPtr_s;
     };
 }
 

Modified: code/trunk/src/libraries/core/GUIManager.cc
===================================================================
--- code/trunk/src/libraries/core/GUIManager.cc	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/GUIManager.cc	2011-05-09 03:06:49 UTC (rev 8423)
@@ -161,8 +161,7 @@
     @return true if success, otherwise false
     */
     GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
-        : destroyer_(*this, &GUIManager::cleanup)
-        , guiRenderer_(NULL)
+        : guiRenderer_(NULL)
         , resourceProvider_(NULL)
 #ifndef ORXONOX_OLD_CEGUI
         , rqListener_(NULL)
@@ -176,6 +175,7 @@
         , hudRootWindow_(NULL)
         , menuRootWindow_(NULL)
         , camera_(NULL)
+        , destructionHelper_(this)
     {
         RegisterRootObject(GUIManager);
         this->setConfigValues();
@@ -257,24 +257,24 @@
         this->luaState_->doFile("SheetManager.lua");
     }
 
-    void GUIManager::cleanup()
+    void GUIManager::destroy()
     {
         using namespace CEGUI;
 
 #ifdef ORXONOX_OLD_CEGUI
-        delete guiSystem_;
-        delete guiRenderer_;
-        delete scriptModule_;
+        safeObjectDelete(&guiSystem_);
+        safeObjectDelete(&guiRenderer_);
+        safeObjectDelete(&scriptModule_);
 #else
         System::destroy();
         OgreRenderer::destroyOgreResourceProvider(*resourceProvider_);
         OgreRenderer::destroyOgreImageCodec(*imageCodec_);
         OgreRenderer::destroy(*guiRenderer_);
         LuaScriptModule::destroy(*scriptModule_);
-        delete ceguiLogger_;
-        delete rqListener_;
+        safeObjectDelete(&ceguiLogger_);
+        safeObjectDelete(&rqListener_);
 #endif
-        delete luaState_;
+        safeObjectDelete(&luaState_);
     }
 
     void GUIManager::setConfigValues(void)
@@ -284,7 +284,6 @@
 
     void GUIManager::changedGUIScheme(void)
     {
-
     }
 
     /**

Modified: code/trunk/src/libraries/core/GUIManager.h
===================================================================
--- code/trunk/src/libraries/core/GUIManager.h	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/GUIManager.h	2011-05-09 03:06:49 UTC (rev 8423)
@@ -43,8 +43,8 @@
 #include <CEGUIForwardRefs.h>
 #include <CEGUIVersion.h>
 #include <boost/shared_ptr.hpp>
-#include <loki/ScopeGuard.h>
 
+#include "util/DestructionHelper.h"
 #include "util/OgreForwardRefs.h"
 #include "util/TriBool.h"
 #include "util/Singleton.h"
@@ -82,8 +82,11 @@
         friend class Singleton<GUIManager>;
     public:
         GUIManager(const std::pair<int, int>& mousePosition);
+
         //! Leave empty and use cleanup() instead
         ~GUIManager() {}
+        /// Destructor that also executes when object fails to construct
+        void destroy();
 
         void setConfigValues(void);
         void changedGUIScheme(void);
@@ -133,9 +136,6 @@
     private:
         GUIManager(const GUIManager& instance); //!< private and undefined copy c'tor (this is a singleton class)
 
-        /// Destructor that also executes when object fails to construct
-        void cleanup();
-
         void executeCode(const std::string& str);
 
         template <typename FunctionType>
@@ -156,9 +156,6 @@
         virtual void windowResized(unsigned int newWidth, unsigned int newHeight);
         virtual void windowFocusChanged(bool bFocus);
 
-        /// Surrogate for the destructor
-        Loki::ObjScopeGuardImpl0<GUIManager, void (GUIManager::*)()> destroyer_;
-
 #ifdef ORXONOX_OLD_CEGUI
         CEGUI::OgreCEGUIRenderer*            guiRenderer_;      //!< CEGUI's interface to the Ogre Engine
         CEGUI::ResourceProvider*             resourceProvider_; //!< CEGUI's resource provider
@@ -179,6 +176,9 @@
         std::map<std::string, PlayerInfo*>   players_;          //!< Stores the player (owner) for each GUI
         Ogre::Camera*                        camera_;           //!< Camera used to render the scene with the GUI
 
+        /// Helper object that executes the surrogate destructor destroy()
+        DestructionHelper<GUIManager>        destructionHelper_;
+
         static GUIManager*                   singletonPtr_s;    //!< Singleton reference to GUIManager
 
         // The used CEGUI scheme.

Modified: code/trunk/src/libraries/core/Game.cc
===================================================================
--- code/trunk/src/libraries/core/Game.cc	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/Game.cc	2011-05-09 03:06:49 UTC (rev 8423)
@@ -77,12 +77,12 @@
     };
 
     Game::Game(const std::string& cmdLine)
-        // Destroy factories before the Core!
-        : gsFactoryDestroyer_(Game::GameStateFactory::getFactories(), &std::map<std::string, shared_ptr<GameStateFactory> >::clear)
+        : gameClock_(NULL)
+        , core_(NULL)
+        , bChangingState_(false)
+        , bAbort_(false)
+        , destructionHelper_(this)
     {
-        this->bAbort_ = false;
-        bChangingState_ = false;
-
 #ifdef ORXONOX_PLATFORM_WINDOWS
         minimumSleepTime_ = 1000/*us*/;
 #else
@@ -102,10 +102,10 @@
         this->declareGameState<GameState>("GameState", "emptyRootGameState", true, false);
 
         // Set up a basic clock to keep time
-        this->gameClock_.reset(new Clock());
+        this->gameClock_ = new Clock();
 
         // Create the Core
-        this->core_.reset(new Core(cmdLine));
+        this->core_ = new Core(cmdLine);
 
         // Do this after the Core creation!
         ClassIdentifier<Game>::getIdentifier("Game")->initialiseObject(this, "Game", true);
@@ -126,14 +126,18 @@
         this->loadedStates_.push_back(this->getState(rootStateNode_->name_));
     }
 
-    /**
-    @brief
-        All destruction code is handled by scoped_ptrs and SimpleScopeGuards.
-    */
-    Game::~Game()
+    void Game::destroy()
     {
         // Remove us from the object lists again to avoid problems when destroying them
         this->unregisterObject();
+
+        assert(loadedStates_.size() <= 1); // Just empty root GameState
+        // Destroy all GameStates (shared_ptrs take care of actual destruction)
+        constructedStates_.clear();
+
+        GameStateFactory::getFactories().clear();
+        safeObjectDelete(&core_);
+        safeObjectDelete(&gameClock_);
     }
 
     void Game::setConfigValues()

Modified: code/trunk/src/libraries/core/Game.h
===================================================================
--- code/trunk/src/libraries/core/Game.h	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/Game.h	2011-05-09 03:06:49 UTC (rev 8423)
@@ -44,18 +44,17 @@
 #include <string>
 #include <vector>
 #include <boost/shared_ptr.hpp>
-#include <boost/scoped_ptr.hpp>
 #include <boost/preprocessor/cat.hpp>
-#include <loki/ScopeGuard.h>
 
 #include "util/Debug.h"
+#include "util/DestructionHelper.h"
 #include "util/Singleton.h"
 #include "OrxonoxClass.h"
 
 /**
 @brief
     Adds a new GameState to the Game. The second parameter is the name as string
-    and every following paramter is a constructor argument (which is usually non existent)
+    and every following parameter is a constructor argument (which is usually non existent)
 */
 #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
     static bool BOOST_PP_CAT(bGameStateDummy_##className, __UNIQUE_NUMBER__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
@@ -91,8 +90,12 @@
 
     public:
         Game(const std::string& cmdLine);
-        ~Game();
 
+        //! Leave empty and use cleanup() instead
+        ~Game() {}
+        /// Destructor that also executes when object fails to construct
+        void destroy();
+
         void setConfigValues();
 
         void setStateHierarchy(const std::string& str);
@@ -137,8 +140,6 @@
             shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
                 { return shared_ptr<GameState>(new T(info)); }
         };
-        // For the factory destruction
-        typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard;
 
         struct StatisticsTickInfo
         {
@@ -165,9 +166,8 @@
         // ScopeGuard helper function
         void resetChangingState() { this->bChangingState_ = false; }
 
-        scoped_ptr<Clock>                  gameClock_;
-        scoped_ptr<Core>                   core_;
-        ObjScopeGuard                      gsFactoryDestroyer_;
+        Clock*                             gameClock_;
+        Core*                              core_;
 
         GameStateMap                       constructedStates_;
         GameStateVector                    loadedStates_;
@@ -193,6 +193,9 @@
         unsigned int                       statisticsAvgLength_;
         unsigned int                       fpsLimit_;
 
+        /// Helper object that executes the surrogate destructor destroy()
+        DestructionHelper<Game>            destructionHelper_;
+
         static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
         static Game* singletonPtr_s;        //!< Pointer to the Singleton
     }; //tolua_export

Modified: code/trunk/src/libraries/core/GraphicsManager.cc
===================================================================
--- code/trunk/src/libraries/core/GraphicsManager.cc	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/GraphicsManager.cc	2011-05-09 03:06:49 UTC (rev 8423)
@@ -93,16 +93,13 @@
 
     GraphicsManager* GraphicsManager::singletonPtr_s = 0;
 
-    /**
-    @brief
-        Non-initialising constructor.
-    */
     GraphicsManager::GraphicsManager(bool bLoadRenderer)
         : ogreWindowEventListener_(new OgreWindowEventListener())
         , renderWindow_(0)
         , viewport_(0)
         , lastFrameStartTime_(0.0f)
         , lastFrameEndTime_(0.0f)
+        , destructionHelper_(this)
     {
         RegisterObject(GraphicsManager);
 
@@ -133,15 +130,11 @@
         }
     }
 
-    /**
-    @brief
-        Destruction is done by the member scoped_ptrs.
-    */
-    GraphicsManager::~GraphicsManager()
+    void GraphicsManager::destroy()
     {
         Loader::unload(debugOverlay_.get());
 
-        Ogre::WindowEventUtilities::removeWindowEventListener(renderWindow_, ogreWindowEventListener_.get());
+        Ogre::WindowEventUtilities::removeWindowEventListener(renderWindow_, ogreWindowEventListener_);
         ModifyConsoleCommand(__CC_printScreen_name).resetFunction();
         ModifyConsoleCommand(__CC_GraphicsManager_group, __CC_setScreenResolution_name).resetFunction();
         ModifyConsoleCommand(__CC_GraphicsManager_group, __CC_setFSAA_name).resetFunction();
@@ -150,6 +143,10 @@
         // Undeclare the resources
         Loader::unload(resources_.get());
         Loader::unload(extResources_.get());
+
+        safeObjectDelete(&ogreRoot_);
+        safeObjectDelete(&ogreLogger_);
+        safeObjectDelete(&ogreWindowEventListener_);
     }
 
     void GraphicsManager::setConfigValues()
@@ -217,7 +214,7 @@
 
         // create a new logManager
         // Ogre::Root will detect that we've already created a Log
-        ogreLogger_.reset(new Ogre::LogManager());
+        ogreLogger_ = new Ogre::LogManager();
         COUT(4) << "Ogre LogManager created" << std::endl;
 
         // create our own log that we can listen to
@@ -240,7 +237,7 @@
         }
 
         // Leave plugins file empty. We're going to do that part manually later
-        ogreRoot_.reset(new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string()));
+        ogreRoot_ = new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string());
 
         COUT(3) << "Ogre set up done." << std::endl;
     }
@@ -299,7 +296,7 @@
         // Propagate the size of the new winodw
         this->ogreWindowEventListener_->windowResized(renderWindow_);
 
-        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_.get());
+        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_);
 
         // create a full screen default viewport
         // Note: This may throw when adding a viewport with an existing z-order!

Modified: code/trunk/src/libraries/core/GraphicsManager.h
===================================================================
--- code/trunk/src/libraries/core/GraphicsManager.h	2011-05-08 21:45:32 UTC (rev 8422)
+++ code/trunk/src/libraries/core/GraphicsManager.h	2011-05-09 03:06:49 UTC (rev 8423)
@@ -47,9 +47,9 @@
 #include <cassert>
 #include <string>
 #include <OgreLog.h>
-#include <boost/scoped_ptr.hpp>
 #include <boost/shared_ptr.hpp>
 
+#include "util/DestructionHelper.h"
 #include "util/Singleton.h"
 #include "OrxonoxClass.h"
 
@@ -67,8 +67,12 @@
         friend class Singleton<GraphicsManager>;
     public:
         GraphicsManager(bool bLoadRenderer = true);
-        ~GraphicsManager();
 
+        //! Leave empty and use cleanup() instead
+        ~GraphicsManager() {}
+        /// Destructor that also executes when object fails to construct
+        void destroy();
+
         void setConfigValues();
 
         void postUpdate(const Clock& time);
@@ -112,9 +116,9 @@
         std::string setFSAA(const std::string& mode);
         std::string setVSync(bool vsync);
 
-        scoped_ptr<OgreWindowEventListener> ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
-        scoped_ptr<Ogre::LogManager>        ogreLogger_;
-        scoped_ptr<Ogre::Root>              ogreRoot_;                //!< Ogre's root
+        OgreWindowEventListener* ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
+        Ogre::LogManager*        ogreLogger_;
+        Ogre::Root*              ogreRoot_;                //!< Ogre's root
         Ogre::RenderWindow* renderWindow_;             //!< the one and only render window
         Ogre::Viewport*     viewport_;                 //!< default full size viewport
         float               lastFrameStartTime_;       //!< Time stamp of the beginning of the last frame
@@ -133,6 +137,9 @@
         int                 ogreLogLevelNormal_;       //!< Corresponding Orxonox debug level for LL_NORMAL
         int                 ogreLogLevelCritical_;     //!< Corresponding Orxonox debug level for LL_CRITICAL
 
+        /// Helper object that executes the surrogate destructor destroy()
+        DestructionHelper<GraphicsManager> destructionHelper_;
+
         static GraphicsManager* singletonPtr_s;        //!< Pointer to the Singleton
 // tolua_begin
     };

Added: code/trunk/src/libraries/util/DestructionHelper.h
===================================================================
--- code/trunk/src/libraries/util/DestructionHelper.h	                        (rev 0)
+++ code/trunk/src/libraries/util/DestructionHelper.h	2011-05-09 03:06:49 UTC (rev 8423)
@@ -0,0 +1,95 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef __DestructionHelper_H__
+#define __DestructionHelper_H__
+
+#include "UtilPrereqs.h"
+
+namespace orxonox
+{
+    /** Deletes an object and resets the pointer
+    @param object
+        Pointer to an object. Handing over NULL is safe.
+    */
+    template <class T>
+    void safeObjectDelete(T** object)
+    {
+        delete *object;
+        *object = NULL;
+    }
+
+    /** Utility class that helps to create a special kind of destructor that
+        also executes if the destruction fails.
+        This helps a lot to improve exception handling. <br>
+        <br>
+        Example: <br>
+        @code
+        class MyClass
+        {
+        public:
+            MyClass() : destructionHelper_(this)
+            {
+                // Do something that might throw
+            }
+            ~MyClass()
+            {
+                // Keep empty!
+            }
+            void destroy()
+            {
+                // Place your destruction code here instead
+            }
+
+        private:
+            orxonox::DestructionHelper<MyClass> destructionHelper_;
+        };
+        @endcode
+    */
+    template <class T>
+    class DestructionHelper
+    {
+    public:
+        DestructionHelper(T* object)
+            : object_(object)
+        {
+        }
+
+        ~DestructionHelper()
+        {
+            object_->destroy();
+        }
+
+    private:
+        DestructionHelper(const DestructionHelper&); //!< Don't use (undefined)
+
+        T* object_;
+    };
+}
+
+#endif /* __DestructionHelper_H__ */


Property changes on: code/trunk/src/libraries/util/DestructionHelper.h
___________________________________________________________________
Added: svn:eol-style
   + native




More information about the Orxonox-commit mailing list