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

/home/pauldt/projects/IDEA/IDEA-ALL/IDEA-DEV/Core/Utilities/Includes/Singleton.hh

Go to the documentation of this file.
00001 // -*- Mode: c++ -*-
00002 //      CVS: $Id: Singleton.hh,v 1.4 2006/01/06 01:18:42 chucko Exp $
00003 //      Tag: $name$
00004 //     Info: $CVSROOT/IDEA-DEV/COPYRIGHT
00005 
00006 #ifndef SINGLETON_HEADER_FILE_
00007 #define SINGLETON_HEADER_FILE_
00008 
00009 #include <cstdlib> // for std::atexit()
00010 
00011 namespace IDEA
00012 {
00032   template < typename T >
00033   class Singleton 
00034   {
00035   public:
00036     inline static T* instance() 
00037     {
00038 
00039       if (getInstance() == 0) 
00040         {
00041           getInstance() = new T;
00042 
00043           std::atexit(Singleton::release);
00044         }
00045 
00046       return getInstance();
00047     }
00048   protected:
00052     Singleton() {}
00056     Singleton( T* pInstance ) { getInstance() = pInstance; }
00060     Singleton(const Singleton &);
00064     Singleton& operator=(const Singleton&);
00068     ~Singleton() { getInstance() = 0; /* do not delete */ } 
00075     static T*& getInstance()
00076     {
00077       static T* s_Instance = 0;
00078 
00079       return s_Instance;
00080     }
00084     static void release() 
00085       {
00086         if ( getInstance() != 0 ) 
00087           {
00088             delete(getInstance());
00089             getInstance() = 0;
00090           }
00091       }
00092     private:
00093     static T* s_Instance;
00094     };
00095   }
00096 
00097 #endif //SINGLETON_HEADER_FILE_

Contact information
© IDEA
Generated on Fri Feb 3 17:09:41 2006 for IDEA.