1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
PowerDNS Versatile Database Driven Nameserver
Copyright (C) 2002 PowerDNS.COM BV
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SINGLETON_HH
#define SINGLETON_HH
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
//! A common singleton template class.
template <class _Ty>
class Singleton
{
private:
static long m_refCount; //! Reference counter.
static _Ty *m_pInstance; //! Pointer to the actual instance.
//! No assign operator allowed.
const _Ty & operator=( const _Ty & rhv )
{
}
//! No copy constructor.
Singleton( const _Ty & copy )
{
}
protected:
public:
//! Default constructor.
Singleton( void )
{
if ( m_refCount == 0 )
{
m_pInstance = reinterpret_cast< _Ty * >( this );
}
// Increase refcount.
InterlockedIncrement( &m_refCount );
}
//! Destructor.
virtual ~Singleton( void )
{
if ( m_refCount == 0 )
return;
if ( InterlockedDecrement( &m_refCount ) == 0 )
{
m_pInstance = NULL;
}
}
//! Creates the singleton.
static _Ty *create( void )
{
if ( m_refCount == 0 )
{
m_pInstance = new _Ty;
}
// Increase refcount.
InterlockedIncrement( &m_refCount );
return m_pInstance;
}
//! Releases the singleton.
static void release( void )
{
if ( m_refCount == 0 )
return;
if ( InterlockedDecrement( &m_refCount ) == 0 )
{
// No more instances needed, delete object.
delete m_pInstance;
m_pInstance = NULL;
}
}
//! Returns a pointer to the singleton.
static _Ty *instance( void )
{
return m_pInstance;
}
//! Initializes the singleton.
virtual bool init( void )
{
return true;
}
//! Shuts the singleton down.
virtual bool shutdown( void )
{
return true;
}
};
template <class _Ty>
_Ty *Singleton< _Ty >::m_pInstance = NULL;
template <class _Ty>
long Singleton< _Ty >::m_refCount = 0;
#endif // SINGLETON_HH
|