00001
#if !defined(__SINGLETON_HPP)
00002
#define __SINGLETON_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#if !defined(__COMMON_HPP)
00025
#include <Common.hpp>
00026
#endif
00027
00028
namespace corelinux
00029 {
00037
template<
class TypeImpl >
00038 class Singleton :
public CoreLinuxObject
00039 {
00040
00041
public:
00042
00043
00044
00045
00046
00054 Singleton(
void ) throw(
Assertion )
00055 :
00056
CoreLinuxObject()
00057 {
00058 REQUIRE( theSingleton == NULLPTR );
00059 REQUIRE( theType == NULLPTR );
00060 theSingleton =
this;
00061 theType =
new TypeImpl;
00062 }
00063
00067 Singleton( TypeImpl *aTypePtr )
throw(
Assertion )
00068 :
00069
CoreLinuxObject()
00070 {
00071 ENSURE( aTypePtr != NULLPTR );
00072 REQUIRE( theSingleton == NULLPTR );
00073 REQUIRE( theType == NULLPTR );
00074 theSingleton =
this;
00075 theType = aTypePtr;
00076 }
00077
00079
00080 virtual ~Singleton(
void )
00081 {
00082
if( theSingleton ==
this )
00083 {
00084 theSingleton = NULLPTR;
00085
if( theType != NULLPTR )
00086 {
00087
delete theType;
00088 theType = NULLPTR;
00089 }
00090
else
00091 {
00092 ;
00093 }
00094 }
00095
else
00096 {
00097 ;
00098 }
00099 }
00100
00101
00102
00103
00104
00112 bool operator==(
const Singleton & aSingleton )
const
00113
{
00114
return ( &aSingleton == theSingleton );
00115 }
00116
00117
00118
00119
00120
00126 static TypeImpl *
instance(
void )
00127 {
00128
return theType;
00129 }
00130
00131
private:
00132
00133
00134
00135
00141
Singleton(
const Singleton & ) throw(
Assertion )
00142 :
00143
CoreLinuxObject()
00144 {
00145 NEVER_GET_HERE;
00146 }
00147
00148
00149
00150
00151
00157 Singleton & operator=(
const Singleton & )
00158 throw( Assertion )
00159 {
00160 NEVER_GET_HERE;
00161
return (*this);
00162 }
00163
00164
private:
00165
00167
00168
static Singleton<TypeImpl> *theSingleton;
00169
00171
00172
static TypeImpl *theType;
00173 };
00174
00176
00177
template<
class TypeImpl >
00178 Singleton<TypeImpl> *Singleton<TypeImpl>::theSingleton( NULLPTR );
00179
00181
00182
template<
class TypeImpl >
00183 TypeImpl *Singleton<TypeImpl>::theType( NULLPTR );
00184 }
00185
00186
#endif // if !defined(__SINGLETON_HPP)
00187
00188
00189
00190
00191
00192
00193
00194
00195