Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Examples  

Singleton.hpp

00001 #if !defined(__SINGLETON_HPP)
00002 #define  __SINGLETON_HPP
00003 
00004 /*
00005    CoreLinux++ 
00006    Copyright (C) 1999 CoreLinux Consortium
00007   
00008    The CoreLinux++ Library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public License as
00010    published by the Free Software Foundation; either version 2 of the
00011    License, or (at your option) any later version.
00012 
00013    The CoreLinux++ Library Library is distributed in the hope that it will 
00014    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public
00019    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00020    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021    Boston, MA 02111-1307, USA.  
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          // Constructors and destructor
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                                  ;  // do nothing
00093                               }
00094                            }
00095                            else
00096                            {
00097                               ;  // do nothing
00098                            }
00099                         }
00100    
00101          //
00102          // Operator overload
00103          //
00104 
00112                   bool  operator==( const Singleton & aSingleton ) const
00113                   {
00114                      return ( &aSingleton == theSingleton );
00115                   }
00116 
00117          //
00118          // Accessor
00119          //
00120 
00126          static   TypeImpl * instance( void )
00127                   {
00128                      return theType;
00129                   }
00130 
00131       private:
00132 
00133             //
00134             // Constructor
00135             //
00141                            Singleton( const Singleton & ) throw( Assertion )
00142                               :
00143                               CoreLinuxObject()
00144                            {
00145                               NEVER_GET_HERE;
00146                            }
00147 
00148             //
00149             // Operator overload
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    Common rcs information do not modify
00190    $Author: prudhomm $
00191    $Revision: 1.1 $
00192    $Date: 2000/04/23 20:43:13 $
00193    $Locker:  $
00194 */
00195 

This is the CoreLinux++ reference manual
Provided by The CoreLinux Consortium