Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 29, 2009, 10:27:10 PM (15 years ago)
Author:
rgrieder
Message:

Derived all singletons implemented in a usual manner from orxonox::Singleton<T>.
This resolves inconsistencies with the singletonPtr_s variable in case of exceptions (asserts were being triggered then).
And while at it replaced singletonRef_s with singletonPtr_s for it to be less misleading (as fabian has already pointed out).

Location:
code/branches/resource/src/core
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource/src/core/Clock.h

    r3196 r3366  
    2626 *
    2727 */
    28 
    29 /**
    30     @file
    31     @brief Declaration of the Core class.
    32 
    33     The Core class is a singleton, only used to configure some variables
    34     in the core through the config-file.
    35 */
    3628
    3729#ifndef _Clock_H__
  • code/branches/resource/src/core/ConfigFileManager.cc

    r3301 r3366  
    4242    const char* const DEFAULT_CONFIG_FILE = "default.ini";
    4343
    44     ConfigFileManager* ConfigFileManager::singletonRef_s = 0;
     44    ConfigFileManager* ConfigFileManager::singletonPtr_s = 0;
    4545
    4646    SetConsoleCommandShortcutExtern(config).argumentCompleter(0, autocompletion::configvalueclasses()).argumentCompleter(1, autocompletion::configvalues()).argumentCompleter(2, autocompletion::configvalue());
     
    482482         : mininmalFreeType_(ConfigFileType::numberOfReservedTypes)
    483483    {
    484         assert(singletonRef_s == 0);
    485         singletonRef_s = this;
    486484    }
    487485
     
    490488        for(std::map<ConfigFileType, ConfigFile*>::const_iterator it = this->configFiles_.begin(); it != this->configFiles_.end(); )
    491489            delete (it++)->second;
    492 
    493         assert(singletonRef_s != 0);
    494         singletonRef_s = 0;
    495490    }
    496491
  • code/branches/resource/src/core/ConfigFileManager.h

    r3196 r3366  
    3838
    3939#include "util/OrxEnum.h"
     40#include "util/Singleton.h"
    4041
    4142namespace orxonox
     
    267268    // ConfigFileManager //
    268269    ///////////////////////
    269     class _CoreExport ConfigFileManager
    270     {
     270    class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager>
     271    {
     272        friend class Singleton<ConfigFileManager>;
    271273        public:
    272274            ConfigFileManager();
     
    305307            void updateConfigValues(ConfigFileType type);
    306308
    307             static ConfigFileManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    308 
    309309        private:
    310310            ConfigFileManager(const ConfigFileManager&);
     
    315315            unsigned int mininmalFreeType_;
    316316
    317             static ConfigFileManager* singletonRef_s;
     317            static ConfigFileManager* singletonPtr_s;
    318318    };
    319319}
  • code/branches/resource/src/core/Core.cc

    r3363 r3366  
    8383{
    8484    //! Static pointer to the singleton
    85     Core* Core::singletonRef_s  = 0;
     85    Core* Core::singletonPtr_s  = 0;
    8686
    8787    SetCommandLineArgument(mediaPath, "").information("Path to the media/data files");
     
    139139                .callback(this, &CoreConfiguration::debugLevelChanged);
    140140
    141             SetConfigValue(language_, Language::getLanguage().defaultLanguage_)
     141            SetConfigValue(language_, Language::getInstance().defaultLanguage_)
    142142                .description("The language of the ingame text")
    143143                .callback(this, &CoreConfiguration::languageChanged);
     
    179179        {
    180180            // Read the translation file after the language was configured
    181             Language::getLanguage().readTranslatedLanguageFile();
     181            Language::getInstance().readTranslatedLanguageFile();
    182182        }
    183183
     
    252252        // Cleanup guard for external console commands that don't belong to an Identifier
    253253        , consoleCommandDestroyer_(CommandExecutor::destroyExternalCommands)
     254        , configuration_(new CoreConfiguration()) // Don't yet create config values!
    254255        , bDevRun_(false)
    255256        , bGraphicsLoaded_(false)
    256         , configuration_(new CoreConfiguration()) // Don't yet create config values!
    257     {
    258         if (singletonRef_s != 0)
    259         {
    260             COUT(0) << "Error: The Core singleton cannot be recreated! Shutting down." << std::endl;
    261             abort();
    262         }
    263         Core::singletonRef_s = this;
    264 
     257    {
    265258        // Parse command line arguments first
    266259        CommandLine::parseCommandLine(cmdLine);
     
    328321    Core::~Core()
    329322    {
    330         // Don't assign singletonRef_s with NULL! Recreation is not supported
    331         // The is quite simply because of the pre-main code that uses heap allocation
    332         // And we correctly deallocate these resources in this destructor.
    333323    }
    334324
  • code/branches/resource/src/core/Core.h

    r3363 r3366  
    4646#include "util/OutputHandler.h"
    4747#include "util/ScopeGuard.h"
     48#include "util/Singleton.h"
    4849
    4950namespace orxonox
     
    5859        The class provides information about the media, config and log path.
    5960        It determines those by the use of platform specific functions.
     61    @remark
     62        You should only create this singleton once because it destroys the identifiers!
    6063    */
    61     class _CoreExport Core
     64    class _CoreExport Core : public Singleton<Core>
    6265    {
    6366        typedef Loki::ScopeGuardImpl0<void (*)()> SimpleScopeGuard;
     67        friend class Singleton<Core>;
    6468
    6569        public:
     
    8185            void loadGraphics();
    8286            void unloadGraphics();
    83 
    84             static Core& getInstance() { assert(Core::singletonRef_s); return *Core::singletonRef_s; }
    8587
    8688            static int   getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All);
     
    136138            bool                          bGraphicsLoaded_;
    137139
    138             static Core* singletonRef_s;
     140            static Core* singletonPtr_s;
    139141    };
    140142}
  • code/branches/resource/src/core/GUIManager.cc

    r3346 r3366  
    8787    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
    8888
    89     GUIManager* GUIManager::singletonRef_s = 0;
     89    GUIManager* GUIManager::singletonPtr_s = 0;
    9090
    9191    /**
     
    105105        , resourceProvider_(0)
    106106    {
    107         assert(singletonRef_s == 0);
    108         singletonRef_s = this;
    109 
    110107        using namespace CEGUI;
    111108
     
    161158        // destroy our own tolua interfaces
    162159        LuaBind::getInstance().closeToluaInterfaces(this->luaState_);
    163 
    164         singletonRef_s = 0;
    165160    }
    166161
  • code/branches/resource/src/core/GUIManager.h

    r3346 r3366  
    4545
    4646#include "util/OgreForwardRefs.h"
     47#include "util/Singleton.h"
    4748#include "input/InputHandler.h"
    4849
     
    6061        Those input events are then injected into CEGUI in Lua.
    6162    */
    62     class _CoreExport GUIManager : public InputHandler
     63    class _CoreExport GUIManager : public Singleton<GUIManager>, public InputHandler
    6364    {
     65        friend class Singleton<GUIManager>;
    6466    public:
    6567        GUIManager(Ogre::RenderWindow* renderWindow);
     
    7375        void setCamera(Ogre::Camera* camera);
    7476
    75         static GUIManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
    76         static GUIManager* getInstancePtr() { return singletonRef_s; }
     77        static GUIManager* getInstancePtr() { return singletonPtr_s; }
    7778
    7879    private:
     
    99100        lua_State*               luaState_;         //!< Lua state, access point to the Lua engine
    100101
    101         static GUIManager*       singletonRef_s;    //!< Singleton reference to GUIManager
     102        static GUIManager*       singletonPtr_s;    //!< Singleton reference to GUIManager
    102103
    103104    };
  • code/branches/resource/src/core/Game.cc

    r3363 r3366  
    6262
    6363    std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s;
    64     Game* Game::singletonRef_s = 0;
     64    Game* Game::singletonPtr_s = 0;
    6565
    6666
     
    114114    Game::Game(const std::string& cmdLine)
    115115    {
    116         if (singletonRef_s != 0)
    117         {
    118             COUT(0) << "Error: The Game singleton cannot be recreated! Shutting down." << std::endl;
    119             abort();
    120         }
    121         singletonRef_s = this;
    122 
    123116        this->bAbort_ = false;
    124117        bChangingState_ = false;
     
    163156    Game::~Game()
    164157    {
    165         // Don't assign singletonRef_s with NULL! Recreation is not supported
    166158    }
    167159
  • code/branches/resource/src/core/Game.h

    r3363 r3366  
    4848
    4949#include "util/Debug.h"
     50#include "util/Singleton.h"
    5051
    5152/**
     
    7576    @brief
    7677        Main class responsible for running the game.
     78    @remark
     79        You should only create this singleton once because it owns the Core class! (see remark there)
    7780    */
    78     class _CoreExport Game
     81    class _CoreExport Game : public Singleton<Game>
    7982    {
     83        friend class Singleton<Game>;
    8084        typedef std::vector<shared_ptr<GameState> > GameStateVector;
    8185        typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
     
    104108        template <class T>
    105109        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
    106         static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    107110
    108111    private:
     
    175178
    176179        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
    177         static Game* singletonRef_s;        //!< Pointer to the Singleton
     180        static Game* singletonPtr_s;        //!< Pointer to the Singleton
    178181    };
    179182
  • code/branches/resource/src/core/GraphicsManager.cc

    r3349 r3366  
    8383    };
    8484
    85     GraphicsManager* GraphicsManager::singletonRef_s = 0;
     85    GraphicsManager* GraphicsManager::singletonPtr_s = 0;
    8686
    8787    /**
     
    9898        RegisterObject(GraphicsManager);
    9999
    100         assert(singletonRef_s == 0);
    101         singletonRef_s = this;
    102 
    103100        this->setConfigValues();
    104101
     
    154151
    155152        delete this->ogreWindowEventListener_;
    156 
    157         assert(singletonRef_s);
    158         singletonRef_s = 0;
    159153    }
    160154
  • code/branches/resource/src/core/GraphicsManager.h

    r3346 r3366  
    4242#include <string>
    4343#include <OgreLog.h>
     44#include "util/Singleton.h"
    4445#include "OrxonoxClass.h"
    4546
     
    5051        Graphics engine manager class
    5152    */
    52     class _CoreExport GraphicsManager : public OrxonoxClass, public Ogre::LogListener
     53    class _CoreExport GraphicsManager : public Singleton<GraphicsManager>, public OrxonoxClass, public Ogre::LogListener
    5354    {
     55        friend class Singleton<GraphicsManager>;
    5456    public:
    5557        GraphicsManager();
     
    6668
    6769        void setCamera(Ogre::Camera* camera);
    68 
    69         inline static GraphicsManager& getInstance()
    70             { assert(singletonRef_s); return *singletonRef_s; }
    7170
    7271    private:
     
    107106        ConsoleCommand*     ccPrintScreen_;
    108107
    109         static GraphicsManager* singletonRef_s;        //!< Pointer to the Singleton
     108        static GraphicsManager* singletonPtr_s;        //!< Pointer to the Singleton
    110109    };
    111110}
  • code/branches/resource/src/core/Language.cc

    r3280 r3366  
    8989    // ###############################
    9090
    91     Language* Language::singletonRef_s = 0;
     91    Language* Language::singletonPtr_s = 0;
    9292
    9393    /**
     
    9696    Language::Language()
    9797    {
    98         assert(singletonRef_s == 0);
    99         singletonRef_s = this;
    100 
    10198        this->defaultLanguage_ = "default";
    10299        this->defaultLocalisation_ = "ERROR: LANGUAGE ENTRY DOESN'T EXIST!";
     
    113110        for (std::map<std::string, LanguageEntry*>::iterator it = this->languageEntries_.begin(); it != this->languageEntries_.end(); ++it)
    114111            delete (it->second);
    115 
    116         assert(singletonRef_s);
    117         singletonRef_s = 0;
    118112    }
    119113
  • code/branches/resource/src/core/Language.h

    r3280 r3366  
    3737    Usage:
    3838     - Set the entry with the default string:
    39        Language::getLanguage()->addEntry("label of the entry", "the string to translate");
     39       Language::getInstance()->addEntry("label of the entry", "the string to translate");
    4040
    4141     - Get the localisation of the entry in the configured language:
    42        std::cout << Language::getLanguage()->getLocalisation("name of the entry") << std::endl;
     42       std::cout << Language::getInstance()->getLocalisation("name of the entry") << std::endl;
    4343*/
    4444
     
    5151#include <string>
    5252#include <cassert>
     53#include "util/Singleton.h"
    5354
    5455#define AddLanguageEntry(label, fallbackstring) \
    55     orxonox::Language::getLanguage().addEntry(label, fallbackstring)
     56    orxonox::Language::getInstance().addEntry(label, fallbackstring)
    5657
    5758#define GetLocalisation(label) \
    58     orxonox::Language::getLanguage().getLocalisation(label)
     59    orxonox::Language::getInstance().getLocalisation(label)
    5960
    6061
     
    112113    // ###############################
    113114    //! The Language class manges the language files and entries and stores the LanguageEntry objects in a map.
    114     class _CoreExport Language
     115    class _CoreExport Language : public Singleton<Language>
    115116    {
     117        friend class Singleton<Language>;
    116118        friend class CoreConfiguration;
    117119
     
    120122            ~Language();
    121123
    122             static Language& getLanguage() { assert(singletonRef_s); return *singletonRef_s; }
    123124            void addEntry(const LanguageEntryLabel& label, const std::string& entry);
    124125            const std::string& getLocalisation(const LanguageEntryLabel& label) const;
     
    137138            std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels
    138139
    139             static Language* singletonRef_s;
     140            static Language* singletonPtr_s;
    140141    };
    141142}
  • code/branches/resource/src/core/LuaBind.cc

    r3340 r3366  
    4343namespace orxonox
    4444{
    45   LuaBind* LuaBind::singletonRef_s = NULL;
     45  LuaBind* LuaBind::singletonPtr_s = NULL;
    4646
    4747  LuaBind::LuaBind()
    4848  {
    49     assert(LuaBind::singletonRef_s == 0);
    50     LuaBind::singletonRef_s = this;
    51 
    5249    this->includePath_ = Core::getMediaPathString();
    5350
     
    7572  {
    7673    this->closeToluaInterfaces(luaState_);
    77 
    78     assert(singletonRef_s);
    79     LuaBind::singletonRef_s = NULL;
    8074  };
    8175
  • code/branches/resource/src/core/LuaBind.h

    r3359 r3366  
    4545}
    4646
     47#include "util/Singleton.h"
     48
    4749// tolua_begin
    4850namespace orxonox
    4951{
    50   class _CoreExport LuaBind
     52  class _CoreExport LuaBind : public Singleton<LuaBind>
    5153  {
     54// tolua_end
     55    friend class Singleton<LuaBind>;
    5256
    53 // tolua_end
    5457    struct LoadS {
    5558      const char *s;
     
    6164      ~LuaBind();
    6265
    63       inline static LuaBind& getInstance() { assert(singletonRef_s); return *LuaBind::singletonRef_s; } // tolua_export
     66      static LuaBind& getInstance() { return Singleton<LuaBind>::getInstance(); } // tolua_export
    6467
    6568    void loadFile(const std::string& filename, bool luaTags);
     
    8992
    9093    private:
    91       static LuaBind* singletonRef_s;
     94      static LuaBind* singletonPtr_s;
    9295
    9396      std::string luaSource_;
  • code/branches/resource/src/core/Shell.cc

    r3301 r3366  
    5151    SetConsoleCommandShortcut(OutputHandler, debug);
    5252
    53     Shell* Shell::singletonRef_s = 0;
     53    Shell* Shell::singletonPtr_s = 0;
    5454
    5555    Shell::Shell()
    5656    {
    57         assert(singletonRef_s == 0);
    58         singletonRef_s = this;
    59 
    6057        int level = Core::getSoftDebugLevel(OutputHandler::LD_Shell);
    6158        Core::setSoftDebugLevel(OutputHandler::LD_Shell, -1);
     
    9289        if (this->inputBuffer_)
    9390            delete this->inputBuffer_;
    94         singletonRef_s = 0;
    9591    }
    9692
  • code/branches/resource/src/core/Shell.h

    r3280 r3366  
    6060    };
    6161
    62     class _CoreExport Shell : virtual public OrxonoxClass, public OutputBufferListener
     62    class _CoreExport Shell : public Singleton<Shell>, virtual public OrxonoxClass, public OutputBufferListener
    6363    {
     64        friend class Singleton<Shell>;
    6465        public:
    6566            Shell();
    6667            virtual ~Shell();
    67 
    68             static Shell& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    6968
    7069            static void clearShell();
     
    148147            ConfigFileType commandHistoryConfigFileType_;
    149148
    150             static Shell* singletonRef_s;
     149            static Shell* singletonPtr_s;
    151150    };
    152151}
  • code/branches/resource/src/core/TclBind.cc

    r3360 r3366  
    4646    SetConsoleCommandShortcut(TclBind, bgerror);
    4747
    48     TclBind* TclBind::singletonRef_s = 0;
     48    TclBind* TclBind::singletonPtr_s = 0;
    4949
    5050    TclBind::TclBind(const std::string& datapath)
    5151    {
    52         assert(singletonRef_s == 0);
    53         singletonRef_s = this;
    5452        this->interpreter_ = 0;
    5553        this->bSetTclDataPath_ = false;
     
    6159        if (this->interpreter_)
    6260            delete this->interpreter_;
    63         singletonRef_s = 0;
    6461    }
    6562
  • code/branches/resource/src/core/TclBind.h

    r3360 r3366  
    3434#include <cassert>
    3535#include <string>
     36#include "util/Singleton.h"
    3637
    3738namespace orxonox
    3839{
    39     class _CoreExport TclBind
     40    class _CoreExport TclBind : public Singleton<TclBind>
    4041    {
     42        friend class Singleton<TclBind>;
    4143        public:
    4244            TclBind(const std::string& datapath);
    4345            ~TclBind();
    44 
    45             static TclBind& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    4646
    4747            static std::string tcl(const std::string& tclcode);
     
    6868            bool bSetTclDataPath_;
    6969
    70             static TclBind* singletonRef_s;
     70            static TclBind* singletonPtr_s;
    7171    };
    7272}
  • code/branches/resource/src/core/TclThreadManager.cc

    r3361 r3366  
    9191        RegisterRootObject(TclThreadManager);
    9292
    93         assert(TclThreadManager::singletonPtr_s == 0);
    94         TclThreadManager::singletonPtr_s = this;
    95 
    9693        this->numInterpreterBundles_ = 0;
    9794
     
    116113    TclThreadManager::~TclThreadManager()
    117114    {
    118         TclThreadManager::singletonPtr_s = 0;
    119 
    120115        delete this->interpreterBundlesMutex_;
    121116//        delete this->mainInterpreterMutex_; // <-- temporary disabled to avoid crash if a thread is still actively queriyng
  • code/branches/resource/src/core/TclThreadManager.h

    r3361 r3366  
    3737#include <string>
    3838
     39#include "util/Singleton.h"
    3940#include "OrxonoxClass.h"
    4041
     
    4344namespace orxonox
    4445{
    45     class _CoreExport TclThreadManager : public OrxonoxClass
     46    class _CoreExport TclThreadManager : public Singleton<TclThreadManager>, public OrxonoxClass
    4647    {
     48        friend class Singleton<TclThreadManager>;
    4749        friend class TclBind;
    4850        friend _CoreExport void tclThread(TclInterpreterBundle* bundle, std::string command);
     
    5355            TclThreadManager(Tcl::interpreter* interpreter);
    5456            virtual ~TclThreadManager();
    55 
    56             static TclThreadManager& getInstance() { assert(TclThreadManager::singletonPtr_s); return *TclThreadManager::singletonPtr_s; }
    5757
    5858            static unsigned int      create();
  • code/branches/resource/src/core/input/InputManager.cc

    r3327 r3366  
    6464    InputHandler InputHandler::EMPTY;
    6565
    66     InputManager* InputManager::singletonRef_s = 0;
     66    InputManager* InputManager::singletonPtr_s = 0;
    6767
    6868    //! Defines the |= operator for easier use.
     
    9393        RegisterRootObject(InputManager);
    9494
    95         assert(singletonRef_s == 0);
    96         singletonRef_s = this;
    97 
    9895        CCOUT(4) << "Constructing..." << std::endl;
    9996
     
    138135        }
    139136
     137        CCOUT(4) << "Construction complete." << std::endl;
    140138        internalState_ = Nothing;
    141         CCOUT(4) << "Construction complete." << std::endl;
    142139    }
    143140
     
    297294
    298295        CCOUT(4) << "Destruction complete." << std::endl;
    299         singletonRef_s = 0;
    300296    }
    301297
  • code/branches/resource/src/core/input/InputManager.h

    r3327 r3366  
    3737#include <vector>
    3838
     39#include "util/Singleton.h"
    3940#include "core/WindowEventListener.h"
    4041#include "InputState.h"
     
    6263          If the OIS::InputManager or the Keyboard fail, an exception is thrown.
    6364    */
    64     class _CoreExport InputManager : public WindowEventListener
     65    class _CoreExport InputManager : public Singleton<InputManager>, public WindowEventListener
    6566    {
     67        friend class Singleton<InputManager>;
    6668    public:
    6769        //! Represents internal states of the InputManager.
     
    168170            { return this->oisInputManager_; }
    169171
    170         //! Returns a reference to the singleton instance
    171         static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    172 
    173172    private: // functions
    174173        // don't mess with a Singleton
     
    211210        std::set<InputState*>               stateDestroyRequests_; //!< Requests to destroy a state
    212211
    213         static InputManager*                singletonRef_s;        //!< Pointer reference to the singleton
     212        static InputManager*                singletonPtr_s;        //!< Pointer reference to the singleton
    214213    };
    215214}
Note: See TracChangeset for help on using the changeset viewer.