[Orxonox-commit 5882] r10542 - in code/branches/core7/src/libraries: core core/class core/command core/commandline core/module core/singleton network

landauf at orxonox.net landauf at orxonox.net
Sun Jun 7 14:16:55 CEST 2015


Author: landauf
Date: 2015-06-07 14:16:55 +0200 (Sun, 07 Jun 2015)
New Revision: 10542

Modified:
   code/branches/core7/src/libraries/core/Core.cc
   code/branches/core7/src/libraries/core/CoreStaticInitializationHandler.cc
   code/branches/core7/src/libraries/core/class/IdentifierManager.cc
   code/branches/core7/src/libraries/core/class/IdentifierManager.h
   code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc
   code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
   code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc
   code/branches/core7/src/libraries/core/commandline/CommandLineParser.h
   code/branches/core7/src/libraries/core/module/StaticInitializationManager.cc
   code/branches/core7/src/libraries/core/module/StaticInitializationManager.h
   code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
   code/branches/core7/src/libraries/core/singleton/ScopeManager.h
   code/branches/core7/src/libraries/network/NetworkFunctionManager.cc
   code/branches/core7/src/libraries/network/NetworkFunctionManager.h
   code/branches/core7/src/libraries/network/NetworkStaticInitializationHandler.cc
Log:
clean and explicit setup/shutdown of singletons that are used by statically initialized instances

Modified: code/branches/core7/src/libraries/core/Core.cc
===================================================================
--- code/branches/core7/src/libraries/core/Core.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/Core.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -57,7 +57,6 @@
 #include "util/output/LogWriter.h"
 #include "util/output/OutputManager.h"
 #include "core/singleton/Scope.h"
-#include "core/singleton/ScopedSingletonIncludes.h"
 #include "ApplicationPaths.h"
 #include "ConfigurablePaths.h"
 #include "commandline/CommandLineIncludes.h"
@@ -65,11 +64,9 @@
 #include "GameMode.h"
 #include "GraphicsManager.h"
 #include "GUIManager.h"
-#include "class/Identifier.h"
 #include "Language.h"
 #include "Loader.h"
 #include "LuaState.h"
-#include "command/ConsoleCommandManager.h"
 #include "command/IOConsole.h"
 #include "command/TclBind.h"
 #include "command/TclThreadManager.h"
@@ -126,12 +123,7 @@
         this->dynLibManager_ = new DynLibManager();
 
         // TODO: initialize Root-Context
-        // TODO: initialize IdentifierManager here
-        // TODO: initialize ScopeManager here
-        // TODO: initialize CommandLineParser here
-        // TODO: initialize ConsoleCommandManager here
-        // TODO: initialize NetworkFunctionManager here
-        // TODO: initialize StaticInitializationManager
+        new StaticInitializationManager(); // create singleton
         this->staticInitHandler_ = new CoreStaticInitializationHandler();
         StaticInitializationManager::getInstance().addHandler(this->staticInitHandler_);
 
@@ -261,6 +253,7 @@
         Context::setRootContext(NULL);
         safeObjectDelete(&rootModule_);
         safeObjectDelete(&staticInitHandler_);
+        delete &StaticInitializationManager::getInstance();
         safeObjectDelete(&dynLibManager_);
         safeObjectDelete(&configurablePaths_);
         safeObjectDelete(&applicationPaths_);

Modified: code/branches/core7/src/libraries/core/CoreStaticInitializationHandler.cc
===================================================================
--- code/branches/core7/src/libraries/core/CoreStaticInitializationHandler.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/CoreStaticInitializationHandler.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -30,19 +30,29 @@
 
 #include "CoreIncludes.h"
 #include "module/ModuleInstance.h"
+#include "object/Iterator.h"
 #include "class/IdentifierManager.h"
-#include "object/Iterator.h"
+#include "singleton/ScopeManager.h"
+#include "command/ConsoleCommandManager.h"
+#include "commandline/CommandLineParser.h"
 
 namespace orxonox
 {
     void CoreStaticInitializationHandler::setupHandler()
     {
-        // TODO
+        // initialize singletons
+        new IdentifierManager();
+        new ScopeManager();
+        new CommandLineParser();
+        new ConsoleCommandManager();
     }
 
     void CoreStaticInitializationHandler::shutdownHandler()
     {
-        // TODO
+        delete &ConsoleCommandManager::getInstance();
+        delete &CommandLineParser::getInstance();
+        delete &ScopeManager::getInstance();
+        delete &IdentifierManager::getInstance();
     }
 
     void CoreStaticInitializationHandler::loadModule(ModuleInstance* module)

Modified: code/branches/core7/src/libraries/core/class/IdentifierManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/class/IdentifierManager.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/class/IdentifierManager.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -43,11 +43,7 @@
 
 namespace orxonox
 {
-    /* static */ IdentifierManager& IdentifierManager::getInstance()
-    {
-        static IdentifierManager instance;
-        return instance;
-    }
+    IdentifierManager* IdentifierManager::singletonPtr_s = 0;
 
     IdentifierManager::IdentifierManager()
     {

Modified: code/branches/core7/src/libraries/core/class/IdentifierManager.h
===================================================================
--- code/branches/core7/src/libraries/core/class/IdentifierManager.h	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/class/IdentifierManager.h	2015-06-07 12:16:55 UTC (rev 10542)
@@ -41,12 +41,17 @@
 #include <list>
 #include <string>
 
+#include "util/Singleton.h"
+
 namespace orxonox
 {
-    class _CoreExport IdentifierManager
+    class _CoreExport IdentifierManager : public Singleton<IdentifierManager>
     {
+        friend class Singleton<IdentifierManager>;
+
         public:
-            static IdentifierManager& getInstance();
+            IdentifierManager();
+            ~IdentifierManager() {}
 
             void addIdentifier(Identifier* identifier);
             void removeIdentifier(Identifier* identifier);
@@ -87,9 +92,7 @@
                 { return this->identifierByNetworkId_; }
 
         private:
-            IdentifierManager();
-            IdentifierManager(const IdentifierManager&);
-            ~IdentifierManager() {}
+            IdentifierManager(const IdentifierManager&); // not implemented
 
             /// Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents.
             inline void startCreatingHierarchy()
@@ -109,6 +112,8 @@
             /// a consequence of this, e.g. nested member objects).
             std::map<Identifiable*, std::list<const Identifier*> > identifierTraceOfNewObject_;
             Identifier* recordTraceForIdentifier_; //!< The identifier for which we want to record the trace of identifiers during object creation. If null, no trace is recorded.
+
+            static IdentifierManager* singletonPtr_s;
     };
 }
 

Modified: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandManager.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -38,11 +38,7 @@
 
 namespace orxonox
 {
-    /* static */ ConsoleCommandManager& ConsoleCommandManager::getInstance()
-    {
-        static ConsoleCommandManager instance;
-        return instance;
-    }
+    ConsoleCommandManager* ConsoleCommandManager::singletonPtr_s = 0;
 
     /**
         @brief Returns the command with given group an name.

Modified: code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h
===================================================================
--- code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/command/ConsoleCommandManager.h	2015-06-07 12:16:55 UTC (rev 10542)
@@ -36,16 +36,18 @@
 
 #include "core/CorePrereqs.h"
 
+#include "util/Singleton.h"
+
 namespace orxonox
 {
     /**
      * A singleton that stores all existing ConsoleCommands.
      */
-    class _CoreExport ConsoleCommandManager
+    class _CoreExport ConsoleCommandManager : public Singleton<ConsoleCommandManager>
     {
+        friend class Singleton<ConsoleCommandManager>;
+
         public:
-            static ConsoleCommandManager& getInstance();
-
             void registerCommand(ConsoleCommand* command);
             void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command);
             void unregisterCommand(ConsoleCommand* command);
@@ -70,6 +72,8 @@
         private:
             std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap_;
             std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC_;
+
+            static ConsoleCommandManager* singletonPtr_s;
     };
 }
 

Modified: code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc
===================================================================
--- code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -39,6 +39,8 @@
 
 namespace orxonox
 {
+    CommandLineParser* CommandLineParser::singletonPtr_s = 0;
+
     /**
     @brief
         Parses a value string for a command line argument.
@@ -86,16 +88,6 @@
     {
     }
 
-    /**
-    @brief
-        Returns a unique instance (Meyers Singleton).
-    */
-    CommandLineParser& CommandLineParser::_getInstance()
-    {
-        static CommandLineParser instance;
-        return instance;
-    }
-
     /** Parses the command line string for arguments and stores these.
     @note
         The reason that you have to provide the string to be parsed as
@@ -259,7 +251,7 @@
 
     std::string CommandLineParser::getUsageInformation()
     {
-        CommandLineParser& inst = _getInstance();
+        CommandLineParser& inst = getInstance();
         std::ostringstream infoStr;
 
         // determine maximum name size
@@ -312,8 +304,8 @@
     */
     const CommandLineArgument* CommandLineParser::getArgument(const std::string& name)
     {
-        std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
-        if (it == _getInstance().cmdLineArgs_.end())
+        std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name);
+        if (it == getInstance().cmdLineArgs_.end())
         {
             ThrowException(Argument, "Could find command line argument '" + name + "'.");
         }
@@ -330,13 +322,13 @@
     */
     void CommandLineParser::addArgument(CommandLineArgument* argument)
     {
-        OrxAssert(!_getInstance().existsArgument(argument->getName()),
+        OrxAssert(!getInstance().existsArgument(argument->getName()),
             "Cannot add a command line argument with name '" + argument->getName() + "' twice.");
         OrxAssert(!argument->getDefaultValue().isType<bool>() || argument->getDefaultValue().get<bool>() != true,
                "Boolean command line arguments with positive default values are not supported." << endl
             << "Please use SetCommandLineSwitch and adjust your argument: " << argument->getName());
 
-        _getInstance().cmdLineArgs_[argument->getName()] = argument;
+        getInstance().cmdLineArgs_[argument->getName()] = argument;
     }
 
     /**
@@ -344,6 +336,6 @@
      */
     void CommandLineParser::removeArgument(CommandLineArgument* argument)
     {
-        _getInstance().cmdLineArgs_.erase(argument->getName());
+        getInstance().cmdLineArgs_.erase(argument->getName());
     }
 }

Modified: code/branches/core7/src/libraries/core/commandline/CommandLineParser.h
===================================================================
--- code/branches/core7/src/libraries/core/commandline/CommandLineParser.h	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/commandline/CommandLineParser.h	2015-06-07 12:16:55 UTC (rev 10542)
@@ -47,6 +47,7 @@
 #include <map>
 #include "util/OrxAssert.h"
 #include "util/MultiType.h"
+#include "util/Singleton.h"
 
 namespace orxonox
 {
@@ -133,13 +134,18 @@
     @see
         CommandLineArgument
     */
-    class _CoreExport CommandLineParser
+    class _CoreExport CommandLineParser : public Singleton<CommandLineParser>
     {
+        friend class Singleton<CommandLineParser>;
+
     public:
+        //! Constructor initialises bFirstTimeParse_ with true.
+        CommandLineParser() : bFirstTimeParse_(true) { }
+        ~CommandLineParser();
 
         //! Parse redirection to internal member method.
         static void parse(const std::string& cmdLine)
-        { _getInstance()._parse(cmdLine); }
+        { getInstance()._parse(cmdLine); }
 
         static std::string getUsageInformation();
 
@@ -156,21 +162,16 @@
 
         static bool existsArgument(const std::string& name)
         {
-            std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
-            return !(it == _getInstance().cmdLineArgs_.end());
+            std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name);
+            return !(it == getInstance().cmdLineArgs_.end());
         }
 
         static void generateDoc(std::ofstream& file);
 
     private:
-        //! Constructor initialises bFirstTimeParse_ with true.
-        CommandLineParser() : bFirstTimeParse_(true) { }
         //! Undefined copy constructor
         CommandLineParser(const CommandLineParser& instance);
-        ~CommandLineParser();
 
-        static CommandLineParser& _getInstance();
-
         void _parse(const std::string& cmdLine);
         void checkFullArgument(const std::string& name, const std::string& value);
         void checkShortcut(const std::string& shortcut, const std::string& value);
@@ -186,6 +187,8 @@
         std::map<std::string, CommandLineArgument*> cmdLineArgs_;
         //! Search map by shortcut for the arguments.
         std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
+
+        static CommandLineParser* singletonPtr_s;
     };
 
     template <>

Modified: code/branches/core7/src/libraries/core/module/StaticInitializationManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/module/StaticInitializationManager.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/module/StaticInitializationManager.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -32,20 +32,18 @@
 
 namespace orxonox
 {
-    /* static */ StaticInitializationManager& StaticInitializationManager::getInstance()
-    {
-        static StaticInitializationManager instance;
-        return instance;
-    }
+    StaticInitializationManager* StaticInitializationManager::singletonPtr_s = 0;
 
     void StaticInitializationManager::addHandler(StaticInitializationHandler* handler)
     {
+        handler->setupHandler();
         this->handlers_.push_back(handler);
     }
 
     void StaticInitializationManager::removeHandler(StaticInitializationHandler* handler)
     {
         this->handlers_.remove(handler);
+        handler->shutdownHandler();
     }
 
     void StaticInitializationManager::loadModule(ModuleInstance* module)

Modified: code/branches/core7/src/libraries/core/module/StaticInitializationManager.h
===================================================================
--- code/branches/core7/src/libraries/core/module/StaticInitializationManager.h	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/module/StaticInitializationManager.h	2015-06-07 12:16:55 UTC (rev 10542)
@@ -33,13 +33,15 @@
 
 #include <list>
 
+#include "util/Singleton.h"
+
 namespace orxonox
 {
-    class _CoreExport StaticInitializationManager
+    class _CoreExport StaticInitializationManager : public Singleton<StaticInitializationManager>
     {
+        friend class Singleton<StaticInitializationManager>;
+
         public:
-            static StaticInitializationManager& getInstance();
-
             StaticInitializationManager() {}
             virtual ~StaticInitializationManager() {}
 
@@ -51,6 +53,8 @@
 
         private:
             std::list<StaticInitializationHandler*> handlers_;
+
+            static StaticInitializationManager* singletonPtr_s;
     };
 }
 

Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -37,11 +37,7 @@
 
 namespace orxonox
 {
-    /* static */ ScopeManager& ScopeManager::getInstance()
-    {
-        static ScopeManager instance;
-        return instance;
-    }
+    ScopeManager* ScopeManager::singletonPtr_s = 0;
 
     void ScopeManager::addScope(ScopeID::Value scope)
     {

Modified: code/branches/core7/src/libraries/core/singleton/ScopeManager.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopeManager.h	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/core/singleton/ScopeManager.h	2015-06-07 12:16:55 UTC (rev 10542)
@@ -40,6 +40,8 @@
 #include <map>
 #include <set>
 
+#include "util/Singleton.h"
+
 namespace orxonox
 {
     /**
@@ -51,11 +53,11 @@
 
         @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
     */
-    class _CoreExport ScopeManager
+    class _CoreExport ScopeManager : public Singleton<ScopeManager>
     {
+        friend class Singleton<ScopeManager>;
+
         public:
-            static ScopeManager& getInstance();
-
             /** Adds a scope and activates all listeners which are registered for this scope */
             void addScope(ScopeID::Value scope);
             /** Removes a scope and deactivates all listeners which are registered for this scope */
@@ -77,6 +79,8 @@
 
             std::set<ScopeID::Value> activeScopes_;
             std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_; //!< Stores all listeners for a scope
+
+            static ScopeManager* singletonPtr_s;
     };
 }
 

Modified: code/branches/core7/src/libraries/network/NetworkFunctionManager.cc
===================================================================
--- code/branches/core7/src/libraries/network/NetworkFunctionManager.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/network/NetworkFunctionManager.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -31,11 +31,7 @@
 
 namespace orxonox
 {
-    /* static */NetworkFunctionManager& NetworkFunctionManager::getInstance()
-    {
-        static NetworkFunctionManager instance;
-        return instance;
-    }
+    NetworkFunctionManager* NetworkFunctionManager::singletonPtr_s = 0;
 
     void NetworkFunctionManager::registerFunction(NetworkFunctionBase* function)
     {

Modified: code/branches/core7/src/libraries/network/NetworkFunctionManager.h
===================================================================
--- code/branches/core7/src/libraries/network/NetworkFunctionManager.h	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/network/NetworkFunctionManager.h	2015-06-07 12:16:55 UTC (rev 10542)
@@ -35,13 +35,16 @@
 #include <map>
 #include <set>
 
+#include "util/Singleton.h"
+#include "NetworkFunction.h"
+
 namespace orxonox
 {
-    class _NetworkExport NetworkFunctionManager
+    class _NetworkExport NetworkFunctionManager : public Singleton<NetworkFunctionManager>
     {
+        friend class Singleton<NetworkFunctionManager>;
+
         public:
-            static NetworkFunctionManager& getInstance();
-
             void registerFunction(NetworkFunctionBase* function);
             void unregisterFunction(NetworkFunctionBase* function);
 
@@ -57,6 +60,8 @@
             std::map<std::string, NetworkFunctionBase*> nameMap_;
             std::map<NetworkFunctionPointer, NetworkFunctionBase*> functorMap_;
             std::map<uint32_t, NetworkFunctionBase*> idMap_;
+
+            static NetworkFunctionManager* singletonPtr_s;
     };
 }
 

Modified: code/branches/core7/src/libraries/network/NetworkStaticInitializationHandler.cc
===================================================================
--- code/branches/core7/src/libraries/network/NetworkStaticInitializationHandler.cc	2015-06-07 11:53:29 UTC (rev 10541)
+++ code/branches/core7/src/libraries/network/NetworkStaticInitializationHandler.cc	2015-06-07 12:16:55 UTC (rev 10542)
@@ -29,17 +29,19 @@
 #include "NetworkStaticInitializationHandler.h"
 
 #include "core/module/ModuleInstance.h"
+#include "NetworkFunctionManager.h"
 
 namespace orxonox
 {
     void NetworkStaticInitializationHandler::setupHandler()
     {
-        // TODO
+        // initialize singleton
+        new NetworkFunctionManager();
     }
 
     void NetworkStaticInitializationHandler::shutdownHandler()
     {
-        // TODO
+        delete &NetworkFunctionManager::getInstance();
     }
 
     void NetworkStaticInitializationHandler::loadModule(ModuleInstance* module)




More information about the Orxonox-commit mailing list