Connecting Tech Pros Worldwide Forums | Help | Site Map

Singleton template

Tim Clacy
Guest
 
Posts: n/a
#1: Jul 19 '05
Is there such a thing as a Singleton template that actually saves
programming effort?


Is it possible to actually use a template to make an arbitrary class a
singleton without having to:

a) explicitly make the arbitrary class's constructor and destructor private
b) declare the Singleton a friend of the arbitrary class

If not, then a singleton template is no better than cutting and pasting a
one-liner like this:

C
{
C& Instance() { static C instance; return instance; }
:
};

Any thoughts from template gurus?


Tim Clacy
An instance of the Simpleton pattern?



Thomas Matthews
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Singleton template


Tim Clacy wrote:[color=blue]
> Is there such a thing as a Singleton template that actually saves
> programming effort?[/color]

I have one, so I don't have to recode all the time.
When most everything is the same but the types differ, that
is when a template is useful.


[color=blue]
> Is it possible to actually use a template to make an arbitrary class a
> singleton without having to:
>
> a) explicitly make the arbitrary class's constructor and destructor private
> b) declare the Singleton a friend of the arbitrary class
>
> If not, then a singleton template is no better than cutting and pasting a
> one-liner like this:
>
> C
> {
> C& Instance() { static C instance; return instance; }
> :
> };
>
> Any thoughts from template gurus?
>
>
> Tim Clacy
> An instance of the Simpleton pattern?[/color]

How about this:
#ifndef SINGLETON_HPP
#define SINGLETON_HPP

//-------------------------------------------------------------------------
// File: singleton.hpp
//
// Class: Singleton
//
// Copyright (C) 2001 - 2003, Thomas Matthews
//
// PROPRIETARY NOTICE
//
// This document is the property of Thomas Matthews, with the
// information herein reserved as proprietary to Thomas Matthews,
// and is not to be published, reproduced, copied, disclosed,
// or used without the express written consent of a duly
// authorized representative of Thomas Matthews
//
// Description:
// This class represents the Singleton Design Pattern.
//
// Notes:
// 1. A singleton allows only one instantiation of a class.
//
// Usage:
// #include "singleton.hpp"
// using namespace Common;
// class My_Class
// : public Singleton<My_Class>
// {
// friend class Singleton<My_Class>;
// protected:
// My_Class();
// };
//
// Recent History {most recent first}:
// 05 Nov 2001 TOM Correction: Changed variable in ref() to static.
//
// Extended history at end of file.
//-------------------------------------------------------------------------

namespace Common
{

template <class Target>
class Singleton
{
//---------------------------------------------------------------------
// Public types
//---------------------------------------------------------------------
public:

//---------------------------------------------------------------------
// Public Constructors & Destructors
//---------------------------------------------------------------------
public:
virtual ~Singleton(); // destructor.

//---------------------------------------------------------------------
// Public Overloaded Operators
//---------------------------------------------------------------------
public:

//---------------------------------------------------------------------
// Public methods
//---------------------------------------------------------------------
public:
static Target * ptr(void);
static Target & ref(void);

//---------------------------------------------------------------------
// Protected methods
//---------------------------------------------------------------------
protected:
Singleton(); // Default constructor

//---------------------------------------------------------------------
// Protected members
//---------------------------------------------------------------------
protected:

//---------------------------------------------------------------------
// Private methods
//---------------------------------------------------------------------
private:

//---------------------------------------------------------------------
// Private members
//---------------------------------------------------------------------
private:

};


//-------------------------------------------------------------------------
// Singleton Exceptions
//-------------------------------------------------------------------------
class Singleton_Exceptions
{
public:
Singleton_Exceptions()
{ ; };
};


//-------------------------------------------------------------------------
// Singleton Constructors & Destructors
//-------------------------------------------------------------------------
template <class Target>
inline
Singleton<Target> ::
Singleton()
{
}


template <class Target>
inline
Singleton<Target> ::
~Singleton()
{
}


//-------------------------------------------------------------------------
// Singleton Overloaded Operators
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
// Singleton methods in alphabetical order
//-------------------------------------------------------------------------
template <class Target>
Target *
Singleton<Target> ::
ptr(void)
{
return &(ref());
}


template <class Target>
Target &
Singleton<Target> ::
ref(void)
{
static Target the_instance;
return the_instance;
}


} // End namespace: Common


//-------------------------------------------------------------------------
// Extended history, most recent entry first.
//
// 30 Oct 2001 TOM Created.
//-------------------------------------------------------------------------
#endif // SINGLETON_HPP


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

David Rubin
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Singleton template


Thomas Matthews wrote:
[color=blue][color=green]
>> Is it possible to actually use a template to make an arbitrary class a
>> singleton without having to:
>>
>> a) explicitly make the arbitrary class's constructor and destructor
>> private[/color][/color]

No, AFAICT. See below.
[color=blue][color=green]
>> b) declare the Singleton a friend of the arbitrary class[/color][/color]

[snip][color=blue]
> // Usage:
> // #include "singleton.hpp"
> // using namespace Common;
> // class My_Class
> // : public Singleton<My_Class>
> // {
> // friend class Singleton<My_Class>;
> // protected:
> // My_Class();
> // };[/color]

This implementation does not meet requirement (b).

As for requirement (a)...
[color=blue]
> template <class Target>
> Target &
> Singleton<Target> ::
> ref(void)
> {
> static Target the_instance;
> return the_instance;
> }[/color]

The static Target declaration demands that Target have a public constructor.

I distilled Thomas Matthews' code to the following:

template <typename T>
class Singleton {
protected:
Singleton() {}

public:
static T* getptr() { return &(getref()); }
static T& getref();

virtual ~Singleton() {}
};

template <typename T>
inline T& Singleton<T>::getref()
{
static T ref;
return ref;
}

This allows

class B : public Singleton<B> {}

which lets you use B::getptr and B::getref to refer to a single instance
of B, but does not prevent instantiations of other B objects. Adding

friend class Singleton<B>;

to the declaration of B allows you to make B's default constructor
private, but violates both of OP's requirements. Anyhow, if B requires
some kind of member initialization, you will need to decalre a default
constructor, which either violates requirement (a) or the Singleton pattern.

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Chris Theis
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Singleton template



"Tim Clacy" <nospamtcl@nospamphaseone.nospamdk> wrote in message
news:3f9d3f84$0$265$4d4eb98e@news.dk.uu.net...[color=blue]
> Is there such a thing as a Singleton template that actually saves
> programming effort?
>
>
> Is it possible to actually use a template to make an arbitrary class a
> singleton without having to:
>
> a) explicitly make the arbitrary class's constructor and destructor[/color]
private[color=blue]
> b) declare the Singleton a friend of the arbitrary class
>
> If not, then a singleton template is no better than cutting and pasting a
> one-liner like this:[/color]

[SNIP]

This statement is not generally true. If you consider more difficult and
sophisticated singelton variations (Phoenix Singleton and so on) you'll see
that templates come in very handy especially if you use policies to create a
fully user configurable singleton pattern. I'd recommend to check out a good
pattern design book (Gang of Four) and Modern Design C++.

HTH
Chris


Chris Theis
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Singleton template



"Thomas Matthews" <Thomas_MatthewsHatesSpam@sbcglobal.net> wrote in message
news:Otdnb.16108$IK4.5176@newssvr32.news.prodigy.c om...
[SNIP]

There is one problem with your singleton approach because it's not failsave
against compile synthesized copy ctors. For example:

My_Class& ObjRef = My_Class::ref();
ObjRef.m_X = 5;
cout << ObjRef.m_X << endl;
My_Class& ObjRef2 = My_Class::ref();
ObjRef2.m_X = 7;
cout << ObjRef.m_X << endl;

My_Class Obj3( My_Class::ref() ); // sneaky copy is possible
Obj3.m_X = 99;
cout << Obj3.m_X << endl;
cout << ObjRef.m_X << endl;

One possible solution would be to implement the singleton as a wrapper and
enforce the check that the wrapped object is for example derived from a
class CNonCopyable.

class CNonCopyable {
protected:
CNonCopyable() {};
virtual ~CNonCopyable() {};
private:
CNonCopyable( const CNonCopyable& rhs );
CNonCopyable& operator=(const CNonCopyable& rhs );
};

Regards
Chris


Tim
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Singleton template


"Thomas Matthews" <Thomas_MatthewsHatesSpam@sbcglobal.net> wrote in message
news:Otdnb.16108$IK4.5176@newssvr32.news.prodigy.c om...[color=blue]
> Tim Clacy wrote:[color=green]
> > Is there such a thing as a Singleton template that actually saves
> > programming effort?[/color][/color]

Thomas, your template is pretty much like my best effort; note that it's
more work to make an arbitary class a singleton by using a template than it
is to cut and paste a one-liner... which I find troubling, not because I'm,
lazy but because it suggests that there's something wrong with the language.


Tim


Tim
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Singleton template



"Chris Theis" <Christian.Theis@nospam.cern.ch> wrote in message
news:bnll16$gfs$1@sunnews.cern.ch...[color=blue]
>
> "Tim Clacy" <nospamtcl@nospamphaseone.nospamdk> wrote in message
> news:3f9d3f84$0$265$4d4eb98e@news.dk.uu.net...[color=green]
> > Is there such a thing as a Singleton template that actually saves
> > programming effort?
> >
> >
> > Is it possible to actually use a template to make an arbitrary class a
> > singleton without having to:
> >
> > a) explicitly make the arbitrary class's constructor and destructor[/color]
> private[color=green]
> > b) declare the Singleton a friend of the arbitrary class
> >
> > If not, then a singleton template is no better than cutting and pasting[/color][/color]
a[color=blue][color=green]
> > one-liner like this:[/color]
>
> [SNIP]
>
> This statement is not generally true. If you consider more difficult and
> sophisticated singelton variations (Phoenix Singleton and so on) you'll[/color]
see[color=blue]
> that templates come in very handy especially if you use policies to create[/color]
a[color=blue]
> fully user configurable singleton pattern. I'd recommend to check out a[/color]
good[color=blue]
> pattern design book (Gang of Four) and Modern Design C++.
>
> HTH
> Chris[/color]

I think I might have come across the complex Singleton example you refer to;
is it the one where you can control how the object is allocated (e.g. using
new or some other means) and how it's life-time is managed?

I'm reasonably familiar with the original GoF Design Patterns and loosely
familiar with a few dozen other trendy patterns; in fact, it seems every one
and his dog has a pattern now.


Chris Theis
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Singleton template



"Tim" <timclacy@hotmail.com> wrote in message
news:bnlnu0$ej7$1@news.cybercity.dk...[color=blue]
>
>
> I think I might have come across the complex Singleton example you refer[/color]
to;[color=blue]
> is it the one where you can control how the object is allocated (e.g.[/color]
using[color=blue]
> new or some other means) and how it's life-time is managed?[/color]

Yes exactly.
[color=blue]
>
> I'm reasonably familiar with the original GoF Design Patterns and loosely
> familiar with a few dozen other trendy patterns; in fact, it seems every[/color]
one[color=blue]
> and his dog has a pattern now.
>[/color]

You're probably right, but they are sometimes quite useful.

Chris


Closed Thread