共享一个Singleton template的实现:
Singleton.hpp如下
************************************************************************ * Template of Singleton, threads safe. * by xiuleili 2013-7-5 ************************************************************************/ #pragma once #include#ifdef WIN32 #include "ResGuard.hpp" #else #include "ResGuard_linux.hpp" #endif template class Singleton { public: // return instance of the singleton static inline T* instance(){ if( 0 == _instance.get() ) { ResGuard::Lock gd(_rs); if( 0== _instance.get()) { _instance.reset ( new T); } } return _instance.get(); } private: Singleton(void){} ~Singleton(void){} Singleton(const Singleton&){} Singleton & operator= (const Singleton &){} static std::auto_ptr _instance; // auto_ptr used for release the resource. static ResGuard _rs; // }; template std::auto_ptr Singleton ::_instance; template ResGuard Singleton ::_rs; // use this macro to declare a singleton #define DECLARE_SINGLETON_CLASS( type ) \ friend class std::auto_ptr< type >;\ friend class Singleton< type >; // How to use: // class A{ // private: // A(){} // A(const A &){} // ~A(){} // A & operator=(const A &){} // DECLARE_SINGLETON_CLASS(A); // }; // // declare the singleton: // typedef Singleton theA; // // call it somewhere like this: // theA::instance()->foo(); /************************************************************************ * Template of Singleton, threads safe. * by xiuleili 2013-7-5 ************************************************************************/ #pragma once #include #ifdef WIN32 #include "ResGuard.hpp" #else #include "ResGuard_linux.hpp" #endif template class Singleton { public: // return instance of the singleton static inline T* instance(){ if( 0 == _instance.get() ) { ResGuard::Lock gd(_rs); if( 0== _instance.get()) { _instance.reset ( new T); } } return _instance.get(); } private: Singleton(void){} ~Singleton(void){} Singleton(const Singleton&){} Singleton & operator= (const Singleton &){} static std::auto_ptr _instance; // auto_ptr used for release the resource. static ResGuard _rs; // }; template std::auto_ptr Singleton ::_instance; template ResGuard Singleton ::_rs; // use this macro to declare a singleton #define DECLARE_SINGLETON_CLASS( type ) \ friend class std::auto_ptr< type >;\ friend class Singleton< type >; // How to use: // class A{ // private: // A(){} // A(const A &){} // ~A(){} // A & operator=(const A &){} // DECLARE_SINGLETON_CLASS(A); // }; // // declare the singleton: // typedef Singleton theA; // // call it somewhere like this: // theA::instance()->foo(); Resguard.hpp
* Implementation of ResGuard on Windows. * see <<windows kernal var cpp>> * by xiuleili 2013-7-5 */ #pragma once #ifndef _UNIX #include/////////////////////////////////////////////////////////////////////////////// // Instances of this class will be accessed by multiple threads. So, // all members of this class (except the constructor and destructor) // must be thread-safe. class ResGuard { public: ResGuard() { m_guardcount = 0; InitializeCriticalSection(&m_cs); } ~ResGuard() { DeleteCriticalSection(&m_