[Ipstream-devel] IPSocket/lib/helpers/Singleton Singleton.h, 1.1.1.1, 1.2
Status: Beta
Brought to you by:
kontramot
|
From: Sergey V. B. <kon...@us...> - 2010-03-19 05:41:43
|
Update of /cvsroot/ipstream/IPSocket/lib/helpers/Singleton In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv24410/lib/helpers/Singleton Modified Files: Singleton.h Log Message: minor reformating Index: Singleton.h =================================================================== RCS file: /cvsroot/ipstream/IPSocket/lib/helpers/Singleton/Singleton.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** Singleton.h 22 Jul 2004 10:25:03 -0000 1.1.1.1 --- Singleton.h 19 Mar 2010 05:41:34 -0000 1.2 *************** *** 1,5 **** ///////////////////////////////////////////////////////////////////////////// ! // ! // Created by: Sergey Belovolov // Created on: 21.06.04 // --- 1,8 ---- + ///////////////////////////////////////////////////////////////////////////// + // @file: Singleton.h ///////////////////////////////////////////////////////////////////////////// ! // ! // @version 1.0 ! // @author Sergey V. Belovolov // Created on: 21.06.04 // *************** *** 14,86 **** #include "boost/thread/mutex.hpp" ! // Multi-threaded singleton class template ! // Create new instance only with the help of CreateInstance() ! // Newer use default constructor!!! You were warned! template < typename SingletonedClass > class TCSingleton : private SingletonedClass { ! private: ! // Flag which indicates if m_pInstance is dead ! volatile static bool m_bIsDead; ! // That member makes TCSingleton class singleton ! // volatile is for avoiding troubles with SMP environment ! volatile static SingletonedClass* m_pInstance; ! // Mutex for synchronization access to m_pInstance ! static boost::mutex m_STMutex; ! // Section for safety (non-copyable class) ! TCSingleton(); ! virtual ~TCSingleton(); ! TCSingleton( const TCSingleton& ); ! TCSingleton& operator=( const TCSingleton& ); ! // Create instance of our singleton ! static void Create() ! { ! static SingletonedClass theInstance; ! m_pInstance = &theInstance; ! } ! // Error handling ! static void OnDeadReference() ! { ! throw std::runtime_error( "Dead reference." ); ! } ! ! public: ! // Acquire instance of Singleton ! static SingletonedClass& CreateInstance() ! { ! // Check if we here for the first time ! if ( !m_pInstance ) ! { ! // Set lock for synchronization ! boost::mutex::scoped_lock SLock( m_STMutex ); ! // Double checking for avoiding race condition ! if ( !m_pInstance ) ! { ! if ( m_bIsDead ) ! OnDeadReference(); ! else ! Create(); ! } ! } ! return ( ( SingletonedClass& )*m_pInstance ); ! } ! }; ! // Static members initialization ! template < typename SingletonedClass > ! volatile SingletonedClass* TCSingleton< SingletonedClass >::m_pInstance = 0; template < typename SingletonedClass > ! volatile bool TCSingleton< SingletonedClass >::m_bIsDead = false; template < typename SingletonedClass > ! boost::mutex TCSingleton< SingletonedClass >::m_STMutex; #endif // _SINGLETON_H --- 17,84 ---- #include "boost/thread/mutex.hpp" ! /** ! * @section DESCRIPTION ! * Multi-threading supporting singleton class template. Guarantee creation of only one instance of ! * SingletonedClass in the system. Create new instances only with the help of CreateInstance()! ! * @param SingletonedClass must have public default constructor. ! * Based on the idea of Andrei Alexandrescu for Singleton implementation, but uses static object ! * instance - once created in a program data segment it will exist until the program ended. ! */ template < typename SingletonedClass > class TCSingleton : private SingletonedClass { ! private: ! ! // That member makes TCSingleton class singleton ! // volatile is for avoiding troubles with SMP environment ! volatile static SingletonedClass* m_pInstance; ! // Mutex for synchronization access to m_pInstance ! static boost::mutex m_STMutex; ! // Section for safety (non-copyable class) ! TCSingleton(); ! virtual ~TCSingleton(); ! TCSingleton( const TCSingleton& ); ! TCSingleton& operator=( const TCSingleton& ); ! // Create instance of our singleton ! static void Create() ! { ! // Create our instance in data segment ! static SingletonedClass theInstance; ! m_pInstance = &theInstance; ! } ! public: ! // Acquire instance of Singleton ! static SingletonedClass& Instance() ! { ! // Check if we here for the first time ! if ( !m_pInstance ) ! { ! // Set lock for synchronization ! boost::mutex::scoped_lock SLock( m_STMutex ); ! // Double checking after lock for avoiding race condition ! if ( !m_pInstance ) ! { ! Create(); ! } ! } ! return ( ( SingletonedClass& ) *m_pInstance ); ! } // static SingletonedClass& CreateInstance() + }; // class TCSingleton : private SingletonedClass + + // Static members initialization template < typename SingletonedClass > ! volatile SingletonedClass* TCSingleton < SingletonedClass >::m_pInstance = 0; template < typename SingletonedClass > ! boost::mutex TCSingleton < SingletonedClass >::m_STMutex; #endif // _SINGLETON_H |