Re: [Dev-C++] Re: Design decision in C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Doug M. <dou...@al...> - 2004-03-12 05:24:52
|
Per,
I understand now. You all have been talking about taking a regular
class and adding code to it to make it a singleton. My expressed
ambivalence is toward something a bit different - it's a Singleton
utility, so to speak. Below is what I am referring to. The idea is
that you implement the Singleton behavior once and for all, as a
template, then any class can become a singleton by inheriting from it as
class Klass : public Singleton<Klass> {
...
}
It's in making it work in general, through inheritance, that the
ugliness I spoke of is introduced.
--Doug
#ifndef __SINGLETON_H__INCLUDED__
#define __SINGLETON_H__INCLUDED__
#include <assert.h>
template <typename Type>
class Singleton
{
protected:
static Type * ms_pInstance;
public:
// Constructor
// The Singleton construct takes care of assinging a
// value to the
// instance pointer. Since this class' child may have
// more than one
// parent, we have to do some pointer math to figure out
// the correct
// address for the child class
Singleton ()
{
assert (!ms_pInstance);
int iOffset = (int)(Type *)1 -
(int)(Singleton<Type> *)(Type *)1;
ms_pInstance = (Type *)((int)this + iOffset);
}
// Destructor
virtual ~Singleton ()
{
assert (ms_pInstance);
ms_pInstance = 0;
}
// Instance
// Returns a reference to the singleton
static Type & Instance ()
{
assert (ms_pInstance);
return (*ms_pInstance);
}
// InstancePtr
// Returns a pointer to the singleton
static Type * InstancePtr ()
{
assert (ms_pInstance);
return ms_pInstance;
}
// HasBeenInstantiated
// Returns true if the class has been instantiated
static bool HasBeenInstantiated ()
{
return (NULL != ms_pInstance);
}
};
template <typename Type> Type * Singleton<Type>::ms_pInstance = 0;
#endif // __SINGLETON_H__INCLUDED__
> I haven't looked at any Singleton<DataType> class doing any instance
> pointer offset hack.
>
> The sample code I sent for a singleton object was just a very fiew
> lines of manually added code to the class in question. 100% valid C++
> without any typecasts.
>
> /Per W
>
> On Thu, 11 Mar 2004, Doug MacDonald wrote:
>
>
>>> Per and Kevin,
>>>
>>> Thanks for your advice.
>>>
>>> My ambivalence wrt the Singleton<DataType> class is in part due
>>> to the instance pointer offset hack, which seems to introduce a
>>> risk of implementation dependency: are base and derived classes
>>> always included in a standard order with offsets that never vary
>>> from compiler to compiler?. Further, the instance pointer
>>> adjustment involves pointer <-> int casts that are a bit ugly,
>>> despite the elegance of Singleton as seen from the outside.
>>>
>>> But I take your points as to the limited desirability of fully
>>> static classes.
>>>
>>> (I guess declaring a top-level static _instance_ of a class is a
>>> different matter, and is useful, e.g., when there is no C++ main
>>> routine, as was the case in a former project of mine at my last
>>> employer.)
>>>
>>> --Doug
|