Singleton Template

A forum to store posts deemed exceptionally wise and useful
Post Reply
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Singleton Template

Post by mohaps »

Singleton.h (this is all you need)

Code: Select all

#ifndef _Singleton_h_
#define _Singleton_h_

template<class T> class Singleton : public T
{
public:
	static T* getInstance();
	static void deleteInstance();
protected:
	Singleton() : T(){}
	virtual ~Singleton(){}
private:
	static Singleton *sInstance;
};

template<class T> Singleton<T>* Singleton<T>::sInstance = 0;
template<class T> T* Singleton<T>::getInstance()
{
	if(sInstance == 0)
	{
		sInstance = new Singleton();
		atexit(deleteInstance);
	}
	return sInstance;
}
template<class T> void Singleton<T>::deleteInstance()
{
	if(sInstance)
	{
		delete sInstance;
	}
	sInstance = 0;
}


#define SINGLETON_CLS(cls)	Singleton<cls>
#define GET_INSTANCE(cls)	(SINGLETON_CLS(cls)::getInstance())
#endif
Singleton.cpp (a simple cpp main demonstrating how to use the Singleton template

Code: Select all


#include "Singleton.h"
#include <iostream>
#include <ctime>
class Clock
{
public:
	const unsigned int currentTimeMillis() const
	{
		return (unsigned int)time(0);
	}
protected:
	Clock(){}
	virtual ~Clock(){}
};

using std::cout;
using std::endl;
int main(int argc,char **argv)
{

	cout<<"Current Time : "<<GET_INSTANCE(Clock)->currentTimeMillis()<<endl;
}
have fun
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
cmoibenlepro
Posts: 237
Joined: Thu May 27, 2004 3:18 pm
Location: Canada

Post by cmoibenlepro »

sorry... but what is a singleton?
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Post by mohaps »

A Singleton is a design pattern where you just want to have one instance of a class and share it among the users.

take a look at the Clock class in the example..

Clock is a singleton.. the same instance is shared everywhere
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

It's a design pattern. A singleton class is a class where there is only once instance of. So when you use the getInstance() method you're always getting the same object (usefull for sharing data between classes etc.).

@mohaps, what's with the #define's ?
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Singelton is a "design pattern" that allows to make sure that only and exactly one instance of a specific class is ever created and used in the whole application regardles in which part of it and how often it gets requested.
It is like it is. And because it is like it is, things are like they are.
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

you where shootin' quicker bal :)
It is like it is. And because it is like it is, things are like they are.
buhatkj
Posts: 444
Joined: Fri Dec 12, 2003 4:53 am
Contact:

macros...

Post by buhatkj »

Very clever certainly, but macros are something I generally try to avoid....
I generally prefer cleaner inheritance relationships than using a template and macro as you have done here. I'm sure this is just my own personal style, so regardless, as I said, pretty clever :-)
-Ted
My irrlicht-based projects have gone underground for now, but if you want, check out my webcomic instead! http://brokenboomerang.net
schick
Posts: 230
Joined: Tue Oct 07, 2003 3:55 pm
Location: Germany
Contact:

Re: Singleton Template

Post by schick »

Sorry, obsolute.
Last edited by schick on Sat Mar 12, 2005 5:17 pm, edited 1 time in total.
Please send me an e-mail instead of a private message.
schick
Posts: 230
Joined: Tue Oct 07, 2003 3:55 pm
Location: Germany
Contact:

Re: macros...

Post by schick »

buhatkj wrote:Very clever certainly, but macros are something I generally try to avoid....
I generally prefer cleaner inheritance relationships than using a template and macro as you have done here. I'm sure this is just my own personal style, so regardless, as I said, pretty clever :-)
-Ted
I like the ::getInstance() singleton pattern.
Please send me an e-mail instead of a private message.
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Post by mohaps »

hey macros are pretty cool if you use them responsibly :)


the macro GET_INSTANCE(clsName) actually is shorthand for writing Singleton<clsName>::getInstance()
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
Post Reply