aboutsummaryrefslogtreecommitdiff
path: root/common/singleton.h
diff options
context:
space:
mode:
authorMax Horn2005-01-01 19:19:06 +0000
committerMax Horn2005-01-01 19:19:06 +0000
commit74bf578bda4c9adcc70ce4cda7d4617a9b95267c (patch)
treeb46f1664b5ffd69ebc5623f83444fd7f5de724e2 /common/singleton.h
parentc418282ec724d01e37b13f7372aa2d6b48f6cbe2 (diff)
downloadscummvm-rg350-74bf578bda4c9adcc70ce4cda7d4617a9b95267c.tar.gz
scummvm-rg350-74bf578bda4c9adcc70ce4cda7d4617a9b95267c.tar.bz2
scummvm-rg350-74bf578bda4c9adcc70ce4cda7d4617a9b95267c.zip
Changed the singleton code to allow for custom object factories; this allowed me to change OSystem to use the singleton base class, too
svn-id: r16404
Diffstat (limited to 'common/singleton.h')
-rw-r--r--common/singleton.h20
1 files changed, 17 insertions, 3 deletions
diff --git a/common/singleton.h b/common/singleton.h
index 4b21e929aa..a436d506b8 100644
--- a/common/singleton.h
+++ b/common/singleton.h
@@ -23,6 +23,18 @@
#ifndef COMMON_SINGLETON_H
#define COMMON_SINGLETON_H
+/**
+ * The default object factory used by the template class Singleton.
+ * By specialising this template function, one can make a singleton use a
+ * custom object factory. For example, to support encapsulation, your
+ * singleton class might be pure virtual (or "abstract" in Java terminology),
+ * and you specialise makeInstance to return an instance of a subclass.
+ */
+template <class T>
+T* makeInstance() {
+ return new T();
+}
+
namespace Common {
/**
@@ -47,13 +59,15 @@ public:
// order might become an issue. There are various approaches
// to solve that problem, but for now this is sufficient
if (!_singleton)
- _singleton = new T;
+ _singleton = makeInstance<T>();
return *_singleton;
}
protected:
Singleton<T>() { }
- ~Singleton<T>() { }
-};
+ virtual ~Singleton<T>() { }
+
+ typedef T SingletonBaseType;
+};
#define DECLARE_SINGLETON(T) template<> T* Common::Singleton<T>::_singleton=0