[Orxonox-commit 917] r5640 - code/branches/libraries/src/util
landauf at orxonox.net
landauf at orxonox.net
Thu Aug 13 01:01:47 CEST 2009
Author: landauf
Date: 2009-08-13 01:01:47 +0200 (Thu, 13 Aug 2009)
New Revision: 5640
Added:
code/branches/libraries/src/util/Scope.cc
code/branches/libraries/src/util/Scope.h
code/branches/libraries/src/util/ScopedSingleton.h
Modified:
code/branches/libraries/src/util/CMakeLists.txt
Log:
Added a new utility "ScopedSingleton". A ScopedSingleton has a scope which is represented by a Scope template. ScopedSingletons are allowed to exist (= get created automatically after the first call to getInstance()) as soon as at least one instance of the corresponding Scope exists. If all those instances of Scope are deleted, the corresponding ScopedSingletons are destroyed too.
This allows us to create Singletons (for example the QuestManager) which may exist in a plugin and still get created and destroyed in parallel with GSLevel (or whatever Scope you like). All we have to do is to create and destroy one instance of Scope in GSLevel (or whoever defines the Scope). Therefore we don't have to link the singleton into GSLevel which is quite handy if we want to build independent plugins.
Btw, this can be expanded to other constructs, not just singletons. Just inherit from ScopeListener to get the full load of features.
(Note to Reto: I had to add a "ScopeManager" which ensures static linkage of the scope counters and listeners. Otherwise we'd end up with different counts in each library which is quite bad. Maybe you have a better idea? Or maybe you want to make the ScopeManager a singleton too? ;))
Modified: code/branches/libraries/src/util/CMakeLists.txt
===================================================================
--- code/branches/libraries/src/util/CMakeLists.txt 2009-08-12 17:26:24 UTC (rev 5639)
+++ code/branches/libraries/src/util/CMakeLists.txt 2009-08-12 23:01:47 UTC (rev 5640)
@@ -26,6 +26,7 @@
MultiType.cc
OutputBuffer.cc
OutputHandler.cc
+ Scope.cc
SignalHandler.cc
Sleep.cc
StringUtils.cc
Added: code/branches/libraries/src/util/Scope.cc
===================================================================
--- code/branches/libraries/src/util/Scope.cc (rev 0)
+++ code/branches/libraries/src/util/Scope.cc 2009-08-12 23:01:47 UTC (rev 5640)
@@ -0,0 +1,35 @@
+/*
+ * 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 "Scope.h"
+
+namespace orxonox
+{
+ std::map<ScopeID::Value, int> ScopeManager::instanceCounts_s;
+ std::map<ScopeID::Value, std::set<ScopeListener*> > ScopeManager::listeners_s;
+}
Property changes on: code/branches/libraries/src/util/Scope.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/libraries/src/util/Scope.h
===================================================================
--- code/branches/libraries/src/util/Scope.h (rev 0)
+++ code/branches/libraries/src/util/Scope.h 2009-08-12 23:01:47 UTC (rev 5640)
@@ -0,0 +1,139 @@
+/*
+ * 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:
+ * ...
+ *
+ */
+
+#ifndef __Util_Scope_H__
+#define __Util_Scope_H__
+
+#include "UtilPrereqs.h"
+#include <cassert>
+#include <set>
+#include <map>
+#include "Debug.h"
+
+namespace orxonox
+{
+ namespace ScopeID
+ {
+ /**
+ @brief A list of available scopes for the Scope template.
+ */
+ enum Value
+ {
+ GSRoot,
+ GSGraphics,
+ GSLevel
+ };
+ }
+
+ /**
+ @brief The ScopeManager stores the variables of the scope templates in a statically linked context.
+ */
+ class _UtilExport ScopeManager
+ {
+ template <ScopeID::Value scope>
+ friend class Scope;
+ friend class ScopeListener;
+
+ private:
+ static std::map<ScopeID::Value, int> instanceCounts_s; //!< Counts the number of active instances (>0 means active) for a scope
+ static std::map<ScopeID::Value, std::set<ScopeListener*> > listeners_s; //!< Stores all listeners for a scope
+ };
+
+ /**
+ @brief ScopeListeners register themselves in the corresponding scope and wait for notifications.
+ */
+ class _UtilExport ScopeListener
+ {
+ template <ScopeID::Value scope>
+ friend class Scope;
+
+ protected:
+ //! Constructor: Registers the instance.
+ ScopeListener(ScopeID::Value scope) : scope_(scope)
+ { ScopeManager::listeners_s[this->scope_].insert(this); }
+ //! Destructor: Unregisters the instance.
+ virtual ~ScopeListener()
+ { ScopeManager::listeners_s[this->scope_].erase(this); }
+
+ //! Gets called if the scope is activated
+ virtual void activated() = 0;
+ //! Gets called if the scope is deactivated
+ virtual void deactivated() = 0;
+
+ private:
+ ScopeID::Value scope_; //!< Store the scope to unregister on destruction
+ };
+
+ /**
+ @brief A scope for a given template argument is either active or not.
+
+ Objects inheriting from a ScopeListener are registered in a list (different for each scope).
+ If the scope gets activated or deactivated, all objects in this list are notified.
+ */
+ template <ScopeID::Value scope>
+ class Scope
+ {
+ public:
+ //! Constructor: Increases the instance counter and activates the scope if the count went from 0 to 1. Counts >1 don't change anything.
+ Scope()
+ {
+ ScopeManager::instanceCounts_s[scope]++;
+ assert(ScopeManager::instanceCounts_s[scope] > 0);
+ if (ScopeManager::instanceCounts_s[scope] == 1)
+ {
+ for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
+ (*(it++))->activated();
+ }
+ }
+
+ //! Destructor: Decreases the instance counter and deactivates the scope if the count went from 1 to 0. Counts >0 don't change anything.
+ ~Scope()
+ {
+ ScopeManager::instanceCounts_s[scope]--;
+
+ // This shouldn't happen but just to be sure: check if the count is positive
+ assert(ScopeManager::instanceCounts_s[scope] >= 0);
+ if (ScopeManager::instanceCounts_s[scope] < 0)
+ ScopeManager::instanceCounts_s[scope] = 0;
+
+ if (ScopeManager::instanceCounts_s[scope] == 0)
+ {
+ for (typename std::set<ScopeListener*>::iterator it = ScopeManager::listeners_s[scope].begin(); it != ScopeManager::listeners_s[scope].end(); )
+ (*(it++))->deactivated();
+ }
+ }
+
+ //! Returns true if the scope is active.
+ static bool isActive()
+ {
+ return (ScopeManager::instanceCounts_s[scope] > 0);
+ }
+ };
+}
+
+#endif /* __Util_Scope_H__ */
Property changes on: code/branches/libraries/src/util/Scope.h
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/libraries/src/util/ScopedSingleton.h
===================================================================
--- code/branches/libraries/src/util/ScopedSingleton.h (rev 0)
+++ code/branches/libraries/src/util/ScopedSingleton.h 2009-08-12 23:01:47 UTC (rev 5640)
@@ -0,0 +1,102 @@
+/*
+ * 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:
+ * ...
+ *
+ */
+
+#ifndef __Util_ScopedSingleton_H__
+#define __Util_ScopedSingleton_H__
+
+#include "UtilPrereqs.h"
+#include <cassert>
+
+#include "Scope.h"
+
+namespace orxonox
+{
+ /**
+ @brief
+ Base for scoped singleton classes.
+ A Scoped singleton creates itself if the scope is active and getInstance() is called.
+ Destroys itself if the scope is deactivated.
+
+ Usage:
+ Inherit publicly from ScopedSingleton<MyClass, scope> and provide access to
+ MyClass::singletonPtr_s.
+ This can easily be done with a friend declaration.
+
+ See @ref UtilPrereqs.h for a list of scopes (ScopeID::Value).
+ */
+ template <class T, ScopeID::Value scope>
+ class ScopedSingleton : public ScopeListener
+ {
+ public:
+ //! Returns a reference to the singleton instance
+ static T& getInstance()
+ {
+ assert(Scope<scope>::isActive());
+
+ if (!T::singletonPtr_s && Scope<scope>::isActive())
+ T::singletonPtr_s = new T();
+
+ return *T::singletonPtr_s;
+ }
+
+ protected:
+ //! Constructor sets the singleton instance pointer
+ ScopedSingleton() : ScopeListener(scope)
+ {
+ assert(T::singletonPtr_s == 0);
+ T::singletonPtr_s = static_cast<T*>(this);
+ }
+
+ //! Constructor resets the singleton instance pointer
+ ~ScopedSingleton()
+ {
+ assert(T::singletonPtr_s != 0);
+ T::singletonPtr_s = 0;
+ }
+
+ private:
+ //! Called if the Scope of this Singleton gets active (no instance should be active then)
+ void activated()
+ {
+ // The ScopedSingleton shouldn't be active bevor the scope is activated -> always assertion failed
+ assert(T::singletonPtr_s == 0 && false);
+ }
+
+ //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
+ void deactivated()
+ {
+ if (T::singletonPtr_s)
+ {
+ delete T::singletonPtr_s;
+ T::singletonPtr_s = 0;
+ }
+ }
+ };
+}
+
+#endif /* __Util_ScopedSingleton_H__ */
Property changes on: code/branches/libraries/src/util/ScopedSingleton.h
___________________________________________________________________
Added: svn:eol-style
+ native
More information about the Orxonox-commit
mailing list