CrossFramework Library

XBase/AutoSPPtr.hpp

説明を見る。
00001 
00006 #if defined(XBASE_INCLUDED_AUTOSPPTR_HPP)
00007 #else
00008 #define XBASE_INCLUDED_AUTOSPPTR_HPP
00009 
00010 //------------------------------------------------------------
00011 #include <XBase/IAllocator.hpp>
00012 #include <XBase/Pointer.hpp>
00013 
00014 //------------------------------------------------------------
00015 namespace XBase {
00017 
00018 
00019     template< typename T >
00020     class AutoSPPtr
00021     {
00022     public:
00024 
00025 
00026         AutoSPPtr()
00027         {
00028         }
00029         
00031         AutoSPPtr( ::XBase::IAllocator& aAllocator )
00032         {
00033             init( aAllocator );
00034         }
00035 
00037         template< typename A0 >
00038         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 )
00039         {
00040             init( aAllocator , a0 );
00041         }        
00042         
00044         template< typename A0 , typename A1 >
00045         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 )
00046         {
00047             init( aAllocator , a0 , a1 );
00048         }
00049         
00051         template< typename A0 , typename A1 , typename A2 >
00052         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 )
00053         {
00054             init( aAllocator , a0 , a1 , a2 );
00055         }
00056         
00058         template< typename A0 , typename A1 , typename A2 , typename A3 >
00059         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 )
00060         {
00061             init( aAllocator , a0 , a1 , a2 , a3 );
00062         }
00063         
00065         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 >
00066         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 )
00067         {
00068             init( aAllocator , a0 , a1 , a2 , a3 , a4 );
00069         }
00070         
00072         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 >
00073         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 )
00074         {
00075             init( aAllocator , a0 , a1 , a2 , a3 , a4 , a5 );
00076         }
00077         
00079         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 >
00080         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 )
00081         {
00082             init( aAllocator , a0 , a1 , a2 , a3 , a4 , a5 , a6 );
00083         }
00084         
00086         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 >
00087         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 )
00088         {
00089             init( aAllocator , a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 );
00090         }
00091         
00093         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 , typename A8 >
00094         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 , A8 a8 )
00095         {
00096             init( aAllocator , a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 );
00097         }
00098 
00100         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 , typename A8 , typename A9 >
00101         AutoSPPtr( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 , A8 a8 , A9 a9 )
00102         {
00103             init( aAllocator , a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 , a9 );
00104         }
00105 
00107         AutoSPPtr( const AutoSPPtr<T>& aPtr )
00108         {   
00109             *this = aPtr;
00110         }
00111 
00113         template< typename OtherType >
00114         AutoSPPtr( const AutoSPPtr<OtherType>& aPtr )
00115         {   
00116             *this = aPtr;
00117         }
00118 
00120         ~AutoSPPtr()
00121         {
00122             reset();
00123         }
00124 
00126 
00128 
00129 
00130         bool isNull()const
00131         {
00132             return mPtr.isNull();
00133         }
00134 
00136         bool isValid()const
00137         {
00138             return mPtr.isValid();
00139         }
00140 
00142         T& ref()const
00143         {
00144             XBASE_ASSERT( isValid() );
00145             return *mPtr;
00146         }
00147 
00152         T* get()const
00153         {
00154             return mPtr.get();
00155         }        
00157         
00159 
00160 
00161         void reset()
00162         {
00163             if ( isNull() )
00164             {
00165                 return;
00166             }
00167             T* ptr = mPtr.get();
00168             mPtr.reset();
00169             ptr->~T();
00170             operator delete ( ptr , mAllocatorPtr.ref() );
00171             mAllocatorPtr.reset();
00172         }
00173 
00175 
00177 
00178         void init( ::XBase::IAllocator& aAllocator )
00179         {
00180             prepareCtor( aAllocator );
00181             mPtr.reset( new ( aAllocator ) T() );
00182         }
00183         template< typename A0 >
00184         void init( ::XBase::IAllocator& aAllocator , A0 a0 )
00185         {
00186             prepareCtor( aAllocator );
00187             mPtr.reset( new ( aAllocator ) T( a0 ) );
00188         }
00189         template< typename A0 , typename A1 >
00190         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 )
00191         {
00192             prepareCtor( aAllocator );
00193             mPtr.reset( new ( aAllocator ) T( a0 , a1 ) );
00194         }
00195         template< typename A0 , typename A1 , typename A2 >
00196         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 )
00197         {
00198             prepareCtor( aAllocator );
00199             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 ) );
00200         }
00201         template< typename A0 , typename A1 , typename A2 , typename A3 >
00202         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 )
00203         {
00204             prepareCtor( aAllocator );
00205             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 ) );
00206         }        
00207         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 >
00208         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 )
00209         {
00210             prepareCtor( aAllocator );
00211             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 , a4 ) );
00212         }        
00213         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 >
00214         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 )
00215         {
00216             prepareCtor( aAllocator );
00217             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 , a4 , a5 ) );
00218         }        
00219         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 >
00220         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 )
00221         {
00222             prepareCtor( aAllocator );
00223             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 , a4 , a5 , a6 ) );
00224         }        
00225         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 >
00226         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 )
00227         {
00228             prepareCtor( aAllocator );
00229             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 ) );
00230         }        
00231         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 , typename A8 >
00232         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 , A8 a8 )
00233         {
00234             prepareCtor( aAllocator );
00235             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 ) );
00236         }        
00237         template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 , typename A8 , typename A9 >
00238         void init( ::XBase::IAllocator& aAllocator , A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 , A8 a8 , A9 a9 )
00239         {
00240             prepareCtor( aAllocator );
00241             mPtr.reset( new ( aAllocator ) T( a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 , a9 ) );
00242         }
00244 
00246 
00247 
00248         AutoSPPtr< T >& operator=( const AutoSPPtr< T >& aRHS )
00249         {
00250             reset();
00251             if ( aRHS.isValid() )
00252             {
00253                 mPtr.set( *aRHS.mPtr );
00254                 mAllocatorPtr = aRHS.mAllocatorPtr;
00255                 aRHS.mPtr.reset();
00256                 aRHS.mAllocatorPtr.reset();
00257             }
00258             return *this;
00259         }
00260 
00262         template< typename OtherType >
00263         AutoSPPtr< T >& operator=( const AutoSPPtr< OtherType >& aRHS )
00264         {
00265             reset();
00266             if ( aRHS.isValid() )
00267             {
00268                 mPtr.set( *aRHS.mPtr );
00269                 mAllocatorPtr = aRHS.mAllocatorPtr;
00270                 aRHS.mPtr.reset();
00271                 aRHS.mAllocatorPtr.reset();
00272             }
00273             return *this;
00274         }
00275         
00277         T& operator*()const
00278         {
00279             return ref();
00280         }
00281 
00283         T* operator->()const
00284         {
00285             XBASE_ASSERT( isValid() );
00286             return get();
00287         }
00289 
00290     private:
00291         mutable Pointer< T >          mPtr;
00292         mutable Pointer< IAllocator > mAllocatorPtr;
00293         //------------------------------------------------------------
00294         void prepareCtor( ::XBase::IAllocator& aAllocator )
00295         {
00296             reset();
00297             mAllocatorPtr.set( aAllocator );
00298         }
00299     };
00301 }
00302 //------------------------------------------------------------
00303 #endif
00304 // EOF
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義