Archived

This topic is now archived and is closed to further replies.

AbsolutDew

singleton template

Recommended Posts

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]

Share this post


Link to post
Share on other sites
I wouldn''t keep the constructors private. Use run-time assertions to enforce that users only instantiate a single object.

Share this post


Link to post
Share on other sites
The constructor is private or protected...?

------------------------------
There are only 10 kinds of people: those that understand binary and those that don''t.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites