/* -*- Base: 10 ; Mode: C++ -*- */ /*------------------------------------------------------------------------ ** FOST project ** -------------------------------------------------------------------------- -------------------------------------------------------------------------- FILE LOG $Revision: 1.2 $ $Date: 2011-06-23 15:46:09 $ -------------------------------------------------------------------------- CREATION F.CASIMIR SUMMARY DESCRIPTION The main function performs the following actions : ------------------------------------------------------------------------*/ //============================================================================= // // History of code // // creation // // modification //============================================================================= #ifndef _PATTERN_SINGLETON_HH_ #define _PATTERN_SINGLETON_HH_ 1 #include #include /** */ //============================================================================= // Class Singleton //============================================================================= /*------------------------------------------------------------------------ CLASS KEYWORDS DESCRIPTION ------------------------------------------------------------------------*/ template< class T > class Singleton { public: /** @return the only instance of C object */ static T* getInstance(void); /** */ static void releaseInstance(); protected: /** Constructor (default one) */ Singleton() {}; /** Copy Constructor (normally should not be used) */ Singleton(const Singleton& copied) {}; /** Destructor */ virtual ~Singleton() {}; // Get Methods // Set Methods // Other Methods //FIELDS private: // Static current instance static T* _singleton; static boost::mutex _pointerAccessMutex; }; //============================================================================= // Inline Methods for Class Singleton //============================================================================= // Initialisation template T* Singleton::_singleton = 0; template boost::mutex Singleton::_pointerAccessMutex; //----------------------------------------------------------------------------- // Get Methods //----------------------------------------------------------------------------- // Set Methods //----------------------------------------------------------------------------- // Other Methods template< class T > T* Singleton::getInstance(void) { if( _singleton==0 ) { boost::lock_guard lock(_pointerAccessMutex); if( _singleton==0 ) { _singleton = new T; } } return _singleton; } template< class T > void Singleton::releaseInstance() { if ( _singleton!=0 ) { boost::lock_guard lock(_pointerAccessMutex); if ( _singleton!=0 ) { delete _singleton ; _singleton=0; } } } //============================================================================= // End of Class Singleton //============================================================================= #endif // _PATTERN_SINGLETON_HH_