CrossFramework Library

XBase/RuntimeMArray.hpp

説明を見る。
00001 
00006 #if defined(XBASE_INCLUDED_RUNTIMEMARRAY_HPP)
00007 #else
00008 #define XBASE_INCLUDED_RUNTIMEMARRAY_HPP
00009 
00010 //------------------------------------------------------------
00011 #include <XBase/IAllocator.hpp>
00012 #include <XBase/NonCopyable.hpp>
00013 #include <XBase/RuntimeAssert.hpp>
00014 
00015 //------------------------------------------------------------
00016 namespace XBase {
00018 
00019 
00020     template< typename T >
00021     class RuntimeMArray : public ::XBase::NonCopyable
00022     {
00023     public:
00025 
00026         typedef T ValueType;
00028 
00030 
00031 
00039         RuntimeMArray( uint aCountMax , IAllocator& aAllocator = IAllocator::Default() )
00040             : mAllocator( aAllocator )
00041             , mCountMax( aCountMax )
00042             , mCount( 0 )
00043             , mPtr( 0 )
00044         {
00045             if ( 0 < aCountMax )
00046             {
00047                 mPtr = reinterpret_cast< ValueType* >( mAllocator.alloc( sizeof( ValueType ) * aCountMax ) );
00048             }
00049         }
00050 
00052         ~RuntimeMArray()
00053         {
00054             if ( mPtr != 0 )
00055             {
00056                 // 逆順でデストラクタを呼び出す
00057                 for ( uint i = mCount; 0 < i; --i )
00058                 {
00059                     const uint idx = i - 1;
00060                     at( idx ).~ValueType();
00061                 }
00062 
00063                 ValueType* ptr = mPtr;
00064                 mPtr = 0;
00065                 mAllocator.free( reinterpret_cast< ptr_t >( ptr ) );
00066             }
00067         }
00068 
00070 
00072 
00073 
00074         bool isEmpty()const
00075         {
00076             return mCount == 0;
00077         }
00078 
00080         bool isFull()const
00081         {
00082             return mCount == mCountMax;
00083         }
00084 
00086         uint count()const
00087         {
00088             return mCount;
00089         }
00090 
00092         uint countMax()const
00093         {
00094             return mCountMax;
00095         }
00096 
00098         ValueType& at( const uint aIndex )
00099         {
00100             if ( mCount <= aIndex )
00101             {
00102                 XBASE_RANGE_ASSERT_MAX( aIndex , mCount );
00103                 return mPtr[ 0 ]; // fail safe code
00104             }
00105             return mPtr[ aIndex ];
00106         }
00107         
00109         const ValueType& at( const uint aIndex )const
00110         {
00111             if ( mCount <= aIndex )
00112             {
00113                 XBASE_RANGE_ASSERT_MAX( aIndex , mCount );
00114                 return mPtr[ 0 ]; // fail safe code
00115             }
00116             return mPtr[ aIndex ];
00117         }
00118 
00120         ValueType& first() { return at( 0 ); }
00122         const ValueType& first()const { return at( 0 ); }
00123         
00125         ValueType& last() { return at( mCount - 1 ); }
00127         const ValueType& last()const { return at( mCount - 1 ); }
00128 
00130 
00132 
00133 
00135         void clear()
00136         {
00137             mCount = 0;
00138         }
00139 
00141         void add( const ValueType& aVal )
00142         {
00143             if ( isFull() )
00144             {
00145                 XBASE_NOT_REACH_ASSERT();
00146                 return;
00147             }
00148             mPtr[ mCount ] = aVal;
00149             ++mCount;
00150         }
00151 
00153 
00155 
00156         ValueType& operator[]( const uint aIndex ) { return at( aIndex ); } 
00157         const ValueType& operator[]( const uint aIndex )const { return at( aIndex ); } 
00158 
00159 
00160     private:
00161         IAllocator& mAllocator;
00162         const uint  mCountMax;
00163         uint        mCount;
00164         ValueType*  mPtr;
00165     };
00167 }
00168 //------------------------------------------------------------
00169 #endif
00170 // EOF
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義