Skip to main content
GameDev.net gamedev.net
🔒 Locked

singleton template

Started by AbsolutDew May 28, 2003 at 11:04 PM 9 replies 1.1k views
Original Post
AbsolutDew
AbsolutDew
is there a proper/standard way to create a templated singleton class? I can't work out how to derive from it while keeeping the constructors private. [edited by - AbsolutDew on May 28, 2003 12:05:09 AM]
daerid
daerid
I wouldn''t keep the constructors private. Use run-time assertions to enforce that users only instantiate a single object.
daerid@gmail.com
grbrg
grbrg
The constructor is private or protected...?

------------------------------
There are only 10 kinds of people: those that understand binary and those that don''t.
------------------------------ There are only 10 kinds of people: those that understand binary and those that don't.
antareus
antareus
Use a templated base class:

class ConcreteSingleton : public Singleton

You need the template to instantiate the derived class in the instance() method

Constructors/destructors should be protected in both classes.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Cedric
Cedric
Do you want there to be only one instantiation of your templated class, or one instantiation per type given to the template?

To achieve the first, I think that you will have to inherit from a class that will contain the static stuff. Otherwise, there is one set of static values for every different type given to the template.

Cédric
Qw3r7yU10p!
Qw3r7yU10p!
Use a protected constructor.

So, you want to create a hierarchy of classes derived from some base and only allow one of each? That sounds very... (fill in your own adjectival here)
Mulligan
Mulligan
I use something like this:

class ConcreteSingleton : public Singleton < ConcreteSingleton >
{

};

But i've forgotten whats public and private.

[edited by - Mulligan on May 29, 2003 1:37:16 PM]
Seriema
Seriema
Hi! I''ve written two singleton classes you can derive from. They''re templated so when you inherit you must say
class MyClass : public Singleton
{...};

Here they are!

Singleton1 has the problem of memory leakage if you don''t call DestroySingleton()

  
#ifndef SINGLETON_H
#define SINGLETON_H

#include <vector>
#include <algorithm>


//-- DECLARATION --------------------------------------------------------------


template <class T>
class cSingleton
{
public:
cSingleton() { }
virtual ~cSingleton() { }

static T* GetSingleton();
static void DestroySingleton();

protected:
static T* m_pSingleton;
};




//-- IMPLEMENTATION -----------------------------------------------------------


template <class T>
T* cSingleton<T>::m_pSingleton = 0;


template <class T>
T* cSingleton<T>::GetSingleton()
{
if( m_pSingleton == 0 )
{
m_pSingleton = new T;
}

return m_pSingleton;
}


template <class T>
void cSingleton<T>::DestroySingleton()
{
delete m_pSingleton;
m_pSingleton = 0;
}



#endif // SINGLETON_H



Singleton2 doesn''t have memory leakage problems

  
#ifndef SINGLETON2_H
#define SINGLETON2_H



//-- DECLARATION --------------------------------------------------------------


template <class T>
class cSingleton2
{
public:
cSingleton2();
virtual ~cSingleton2();

static T& GetSingleton();

protected:
static T m_pSingleton;
};


//-- IMPLEMENTATION -----------------------------------------------------------


template <class T>
T* cSingleton2<T>::m_pSingleton;


template <class T>
cSingleton2<T>::cSingleton2()
{

}


template <class T>
cSingleton2<T>::~cSingleton2()
{

}


template <class T>
T& cSingleton2<T>::GetSingleton()
{
return m_pSingleton;
}


#endif // SINGLETON2_H



"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
AbsolutDew
AbsolutDew
I want one instance allowed for each type derived from the base class, Seriema and IFooBar both gave me what I need, thank you.

IFooBar, I stumbled across something that looks very much like what was done in that implementation in the OGRE source (exactly, in fact), but I had no idea what was going on. I appreciate the link.
antareus
antareus
Problem is on some singleton implementations you run into problems if the singletons interact because static initialization order isn''t always defined.

I use a Meyers singleton:

T&
Singleton::Instance()
{
static T singleton;
return singleton;
}

--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.