[Scummvm-cvs-logs] CVS: scummvm/common config-manager.h,1.20,1.21 singleton.h,1.8,1.9 system.cpp,1.15,1.16 system.h,1.77,1.78

Max Horn fingolfin at users.sourceforge.net
Sat Jan 1 11:20:10 CET 2005


Update of /cvsroot/scummvm/scummvm/common
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4920/common

Modified Files:
	config-manager.h singleton.h system.cpp system.h 
Log Message:
Changed the singleton code to allow for custom object factories; this allowed me to change OSystem to use the singleton base class, too

Index: config-manager.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/config-manager.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- config-manager.h	1 Jan 2005 16:08:49 -0000	1.20
+++ config-manager.h	1 Jan 2005 19:19:05 -0000	1.21
@@ -38,9 +38,6 @@
  * @todo Implement the callback based notification system (outline below)
  *       which sends out notifications to interested parties whenever the value
  *       of some specific (or any) configuration key changes.
- * @todo Preserve the order of the entries in the config file. Maybe even add
- *       an API to query/modify that order, which could be used by the launcher
- *       to allow arranging the game targets.
  */
 class ConfigManager : public Singleton<ConfigManager> {
 	struct IgnoreCaseComparator {
@@ -117,7 +114,7 @@
 */
 
 private:
-	friend class Singleton<ConfigManager>;
+	friend SingletonBaseType *makeInstance<>();
 	ConfigManager();
 
 	void			loadFile(const String &filename);

Index: singleton.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/singleton.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- singleton.h	1 Jan 2005 16:08:50 -0000	1.8
+++ singleton.h	1 Jan 2005 19:19:05 -0000	1.9
@@ -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 @@
 		// 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
 

Index: system.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/system.cpp,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- system.cpp	1 Jan 2005 18:53:47 -0000	1.15
+++ system.cpp	1 Jan 2005 19:19:05 -0000	1.16
@@ -31,9 +31,10 @@
 #include "common/config-manager.h"
 #include "common/system.h"
 
-static OSystem *s_system = 0;
+DECLARE_SINGLETON(OSystem);
 
-static OSystem *createSystem() {
+template <>
+OSystem *makeInstance<>() {
 	// Attention: Do not call parseGraphicsMode() here, nor any other function
 	// which needs to access the OSystem instance, else you get stuck in an
 	// endless loop.
@@ -58,13 +59,6 @@
 #endif
 }
 
-OSystem &OSystem::instance() {
-	if (!s_system)
-		s_system = createSystem();
-	return *s_system;
-}
-
-
 bool OSystem::setGraphicsMode(const char *name) {
 	if (!name)
 		return false;

Index: system.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/system.h,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -d -r1.77 -r1.78
--- system.h	1 Jan 2005 18:53:47 -0000	1.77
+++ system.h	1 Jan 2005 19:19:05 -0000	1.78
@@ -27,6 +27,16 @@
 #include "common/util.h"
 #include "common/rect.h"
 #include "common/savefile.h"
+#include "common/singleton.h"
+
+class OSystem;
+
+/**
+ * Custome object factory for OSystem.
+ */
+template <>
+OSystem *makeInstance<>();
+
 
 /**
  * Interface for ScummVM backends. If you want to port ScummVM to a system
@@ -38,22 +48,8 @@
  * methods to create timers, to handle user input events,
  * control audio CD playback, and sound output.
  */
-class OSystem {
-public:
-	/**
-	 * Returns a pointer to the (singleton) OSystem instance, that is, to the
-	 * active backend.
-	 * This is not quite a "proper" singleton, since OSystem is an interface
-	 * not a real class (and thus it isn't based on our Singleton template).
-	 * @return	the pointer to the (singleton) OSystem instance
-	 */
-	static OSystem &instance();
-
+class OSystem : public Common::Singleton<OSystem> {
 public:
-
-	/** Empty virtual destructor. DO NOT REMOVE! */
-	virtual ~OSystem() {}
-
 	
 	/** @name Feature flags */
 	//@{





More information about the Scummvm-git-logs mailing list