Tim said:
Is there such a thing as a Singleton template that actually saves
programming effort?
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.
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?
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