CrossFramework Library

XBase/ExplicitSingleton.hpp

説明を見る。
00001 
00006 #if defined(XBASE_INCLUDED_EXPLICITSINGLETON_HPP)
00007 #else
00008 #define XBASE_INCLUDED_EXPLICITSINGLETON_HPP
00009 
00010 //------------------------------------------------------------
00011 #include <XBase/NonCopyable.hpp>
00012 #include <XBase/RuntimeAssert.hpp>
00013 
00014 //------------------------------------------------------------
00015 namespace XBase {
00017 
00018 
00047     template< typename T >
00048     class ExplicitSingleton : public NonCopyable
00049     {
00050     public:
00052 
00053         ExplicitSingleton()
00054         {
00055             XBASE_ASSERT( !IsCreated() );
00056         }
00057         ~ExplicitSingleton()
00058         {
00059             XBASE_ASSERT( !IsCreated() );
00060         }
00062 
00064 
00065         static inline T& Instance(); 
00066         static inline bool IsCreated(); 
00067 
00068 
00069     protected:
00071         inline void SetInstance( T& );
00072 
00074         inline void UnsetInstance();
00075 
00076     private:
00077         static T* sPtr;
00078     };
00080     
00081     template< typename T >
00082     T& ExplicitSingleton<T>::Instance()
00083     {
00084         XBASE_ASSERT( IsCreated() );
00085         return *sPtr;
00086     }
00087 
00088     template< typename T >
00089     bool ExplicitSingleton<T>::IsCreated()
00090     {
00091         return sPtr != 0;
00092     }
00093 
00094     template< typename T >
00095     void ExplicitSingleton<T>::SetInstance( T& aRef )
00096     {
00097         XBASE_ASSERT( !IsCreated() );
00098         sPtr = &aRef;
00099     }
00100     
00101     template< typename T >
00102     void ExplicitSingleton<T>::UnsetInstance()
00103     {
00104         XBASE_ASSERT( IsCreated() );
00105         sPtr = 0;
00106     }
00107 
00108     template< typename T >
00109     T* ExplicitSingleton<T>::sPtr = 0;
00110 }
00111 //------------------------------------------------------------
00112 #endif
00113 // EOF
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義