From 74bf578bda4c9adcc70ce4cda7d4617a9b95267c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 1 Jan 2005 19:19:06 +0000 Subject: 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 --- common/singleton.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'common/singleton.h') 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 +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(); return *_singleton; } protected: Singleton() { } - ~Singleton() { } -}; + virtual ~Singleton() { } + + typedef T SingletonBaseType; +}; #define DECLARE_SINGLETON(T) template<> T* Common::Singleton::_singleton=0 -- cgit v1.2.3