[Orxonox-commit 854] r3364 - branches/resource/src/util
rgrieder at orxonox.net
rgrieder at orxonox.net
Wed Jul 29 18:26:22 CEST 2009
Author: rgrieder
Date: 2009-07-29 18:26:22 +0200 (Wed, 29 Jul 2009)
New Revision: 3364
Added:
branches/resource/src/util/Singleton.h
Log:
Added Singleton class template. It doesn't spare much of the typing but at least it enforces that the singleton pointer's value is consistent (especially with exceptions being thrown).
Note that even if you're inheriting from Singleton<MyClass>, you still need to declare "static MyClass* singletonRef_s" to avoid linker problems.
Added: branches/resource/src/util/Singleton.h
===================================================================
--- branches/resource/src/util/Singleton.h (rev 0)
+++ branches/resource/src/util/Singleton.h 2009-07-29 16:26:22 UTC (rev 3364)
@@ -0,0 +1,75 @@
+/*
+ * 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 __Util_Singleton_H__
+#define __Util_Singleton_H__
+
+#include "UtilPrereqs.h"
+
+namespace orxonox
+{
+ /**
+ @brief
+ Base for singleton classes.
+
+ Usage:
+ Inherit publicly from Singleton<MyClass> and provide access to
+ MyClass::singletonRef_s.
+ This can be done with a friend declaration.
+ */
+ template <class T>
+ class Singleton
+ {
+ public:
+ //! Returns a reference to the singleton instance
+ static T& getInstance()
+ {
+ assert(T::singletonRef_s != NULL);
+ return *T::singletonRef_s;
+ }
+
+ protected:
+ // Constructor sets the singleton instance pointer
+ Singleton()
+ {
+ assert(T::singletonRef_s == NULL);
+ T::singletonRef_s = static_cast<T*>(this);
+ }
+ // Constructor resets the singleton instance pointer
+ ~Singleton()
+ {
+ assert(T::singletonRef_s != NULL);
+ T::singletonRef_s = NULL;
+ }
+
+ private:
+ Singleton(const Singleton& rhs); //!< Don't use (undefined)
+ };
+}
+
+#endif /* __Util_Singleton_H__ */
Property changes on: branches/resource/src/util/Singleton.h
___________________________________________________________________
Added: svn:eol-style
+ native
More information about the Orxonox-commit
mailing list