[Orxonox-commit 5800] r10460 - in code/branches/core7: src/libraries/core/singleton test/core/singleton

landauf at orxonox.net landauf at orxonox.net
Sun May 24 23:19:16 CEST 2015


Author: landauf
Date: 2015-05-24 23:19:15 +0200 (Sun, 24 May 2015)
New Revision: 10460

Added:
   code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc
Modified:
   code/branches/core7/src/libraries/core/singleton/CMakeLists.txt
   code/branches/core7/src/libraries/core/singleton/Scope.h
   code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.h
   code/branches/core7/src/libraries/core/singleton/ScopedSingletonWrapper.h
   code/branches/core7/test/core/singleton/ScopeTest.cc
Log:
StaticallyInitializedScopedSingletonWrapper registers ScopedSingletonWrapper in ScopeManager

Modified: code/branches/core7/src/libraries/core/singleton/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/singleton/CMakeLists.txt	2015-05-24 11:48:18 UTC (rev 10459)
+++ code/branches/core7/src/libraries/core/singleton/CMakeLists.txt	2015-05-24 21:19:15 UTC (rev 10460)
@@ -1,3 +1,4 @@
 ADD_SOURCE_FILES(CORE_SRC_FILES
   Scope.cc
+  ScopedSingletonIncludes.cc
 )

Modified: code/branches/core7/src/libraries/core/singleton/Scope.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/Scope.h	2015-05-24 11:48:18 UTC (rev 10459)
+++ code/branches/core7/src/libraries/core/singleton/Scope.h	2015-05-24 21:19:15 UTC (rev 10460)
@@ -75,7 +75,7 @@
     {
         template <ScopeID::Value scope>
         friend class Scope;
-        friend class ScopeListener;
+        friend class StaticallyInitializedScopedSingletonWrapper;
 
         private:
             static std::map<ScopeID::Value, int>& getInstanceCounts();                  //!< Counts the number of active instances (>0 means active) for a scope
@@ -94,18 +94,18 @@
         friend class Scope;
 
         protected:
-            //! Constructor: Registers the instance.
-            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false)
-                { ScopeManager::getListeners()[this->scope_].insert(this); }
-            //! Destructor: Unregisters the instance.
-            virtual ~ScopeListener()
-                { ScopeManager::getListeners()[this->scope_].erase(this); }
+            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) { }
+            virtual ~ScopeListener() { }
 
             //! Gets called if the scope is activated
             virtual void activated() = 0;
             //! Gets called if the scope is deactivated
             virtual void deactivated() = 0;
 
+        public:
+            inline ScopeID::Value getScope() const
+                { return this->scope_; }
+
         private:
             ScopeID::Value scope_; //!< Store the scope to unregister on destruction
             bool bActivated_;

Added: code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc	                        (rev 0)
+++ code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.cc	2015-05-24 21:19:15 UTC (rev 10460)
@@ -0,0 +1,42 @@
+/*
+ *   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:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "ScopedSingletonIncludes.h"
+
+namespace orxonox
+{
+    void StaticallyInitializedScopedSingletonWrapper::load()
+    {
+        ScopeManager::getListeners()[this->wrapper_->getScope()].insert(this->wrapper_);
+    }
+
+    void StaticallyInitializedScopedSingletonWrapper::unload()
+    {
+        ScopeManager::getListeners()[this->wrapper_->getScope()].erase(this->wrapper_);
+    }
+}

Modified: code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.h	2015-05-24 11:48:18 UTC (rev 10459)
+++ code/branches/core7/src/libraries/core/singleton/ScopedSingletonIncludes.h	2015-05-24 21:19:15 UTC (rev 10460)
@@ -82,8 +82,8 @@
         public:
             StaticallyInitializedScopedSingletonWrapper(ScopedSingletonWrapper* wrapper) : wrapper_(wrapper) {}
 
-            virtual void load() {}
-            virtual void unload() {}
+            virtual void load();
+            virtual void unload();
 
             inline ScopedSingletonWrapper& getWrapper()
                 { return *this->wrapper_; }

Modified: code/branches/core7/src/libraries/core/singleton/ScopedSingletonWrapper.h
===================================================================
--- code/branches/core7/src/libraries/core/singleton/ScopedSingletonWrapper.h	2015-05-24 11:48:18 UTC (rev 10459)
+++ code/branches/core7/src/libraries/core/singleton/ScopedSingletonWrapper.h	2015-05-24 21:19:15 UTC (rev 10460)
@@ -56,19 +56,18 @@
     /**
         @brief Base class of ClassScopedSingletonWrapper.
     */
-    class _CoreExport ScopedSingletonWrapper
+    class _CoreExport ScopedSingletonWrapper : public ScopeListener
     {
         public:
             /// Constructor: Initializes all the values
             ScopedSingletonWrapper(const std::string& className, ScopeID::Value scope)
-                : className_(className)
-                , scope_(scope)
+                : ScopeListener(scope)
+                , className_(className)
             { }
             virtual ~ScopedSingletonWrapper() { }
 
         protected:
             const std::string className_;   ///< The name of the scoped singleton class that is managed by this object
-            const ScopeID::Value scope_;    ///< The scope of the singleton that is managed by this object
     };
 
     /**
@@ -88,13 +87,12 @@
         @see Singleton
     */
     template <class T, ScopeID::Value scope, bool allowedToFail>
-    class ClassScopedSingletonWrapper : public ScopedSingletonWrapper, public ScopeListener
+    class ClassScopedSingletonWrapper : public ScopedSingletonWrapper
     {
     public:
         //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonWrapper and ScopeListener
         ClassScopedSingletonWrapper(const std::string& className)
             : ScopedSingletonWrapper(className, scope)
-            , ScopeListener(scope)
             , singletonPtr_(NULL)
         {
         }
@@ -144,13 +142,12 @@
         See @ref ClassScopedSingletonWrapper for a full documentation of the basis template.
     */
     template <class T, ScopeID::Value scope>
-    class ClassScopedSingletonWrapper<T, scope, true> : public ScopedSingletonWrapper, public ScopeListener
+    class ClassScopedSingletonWrapper<T, scope, true> : public ScopedSingletonWrapper
     {
     public:
         //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonWrapper and ScopeListener
         ClassScopedSingletonWrapper(const std::string& className)
             : ScopedSingletonWrapper(className, scope)
-            , ScopeListener(scope)
             , singletonPtr_(NULL)
         {
         }

Modified: code/branches/core7/test/core/singleton/ScopeTest.cc
===================================================================
--- code/branches/core7/test/core/singleton/ScopeTest.cc	2015-05-24 11:48:18 UTC (rev 10459)
+++ code/branches/core7/test/core/singleton/ScopeTest.cc	2015-05-24 21:19:15 UTC (rev 10460)
@@ -1,5 +1,6 @@
 #include <gtest/gtest.h>
 #include "core/singleton/ScopedSingletonIncludes.h"
+#include "core/module/ModuleInstance.h"
 
 namespace orxonox
 {
@@ -18,21 +19,36 @@
 
         ManageScopedSingleton(TestSingletonRoot, ScopeID::Root, false);
         ManageScopedSingleton(TestSingletonGraphics, ScopeID::Graphics, false);
+
+        // Fixture
+        class ScopeTest : public ::testing::Test
+        {
+            public:
+                virtual void SetUp()
+                {
+                    ModuleInstance::getCurrentModuleInstance()->loadAllStaticallyInitializedInstances();
+                }
+
+                virtual void TearDown()
+                {
+                    ModuleInstance::getCurrentModuleInstance()->unloadAllStaticallyInitializedInstances();
+                }
+        };
     }
 
-    TEST(Scope, ScopesDoNotExist)
+    TEST_F(ScopeTest, ScopesDoNotExist)
     {
         EXPECT_FALSE(Scope<ScopeID::Root>::isActive());
         EXPECT_FALSE(Scope<ScopeID::Graphics>::isActive());
     }
 
-    TEST(Scope, SingletonsDoNotExist)
+    TEST_F(ScopeTest, SingletonsDoNotExist)
     {
         EXPECT_FALSE(TestSingletonRoot::exists());
         EXPECT_FALSE(TestSingletonGraphics::exists());
     }
 
-    TEST(Scope, RootScope)
+    TEST_F(ScopeTest, RootScope)
     {
         EXPECT_FALSE(Scope<ScopeID::Root>::isActive());
         {   // create root scope
@@ -42,7 +58,7 @@
         EXPECT_FALSE(Scope<ScopeID::Root>::isActive());
     }
 
-    TEST(DISABLED_Scope, RootAndGraphicsScope)
+    TEST_F(ScopeTest, DISABLED_RootAndGraphicsScope)
     {
         EXPECT_FALSE(Scope<ScopeID::Graphics>::isActive());
         {   // create root scope
@@ -57,7 +73,7 @@
         EXPECT_FALSE(Scope<ScopeID::Graphics>::isActive());
     }
 
-    TEST(Scope, RootSingleton)
+    TEST_F(ScopeTest, RootSingleton)
     {
         EXPECT_FALSE(TestSingletonRoot::exists());
         {   // create root scope
@@ -67,7 +83,7 @@
         EXPECT_FALSE(TestSingletonRoot::exists());
     }
 
-    TEST(DISABLED_Scope, RootAndGraphicsSingleton)
+    TEST_F(ScopeTest, DISABLED_RootAndGraphicsSingleton)
     {
         EXPECT_FALSE(TestSingletonGraphics::exists());
         {   // create root scope




More information about the Orxonox-commit mailing list