CrossFramework Library
|
00001 00006 #if defined(XBASE_INCLUDED_RUNTIMEAUTOARRAY_HPP) 00007 #else 00008 #define XBASE_INCLUDED_RUNTIMEAUTOARRAY_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 RuntimeAutoArray : public ::XBase::NonCopyable 00022 { 00023 public: 00025 00026 typedef T ValueType; 00028 00030 00031 00039 RuntimeAutoArray( 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 ~RuntimeAutoArray() 00053 { 00054 // 全てをデストラクト 00055 clear(); 00056 00057 if ( mPtr != 0 ) 00058 { 00059 ValueType* ptr = mPtr; 00060 mPtr = 0; 00061 mAllocator.free( reinterpret_cast< ptr_t >( ptr ) ); 00062 } 00063 } 00064 00066 00068 00069 00070 bool isEmpty()const 00071 { 00072 return mCount == 0; 00073 } 00074 00076 bool isFull()const 00077 { 00078 return mCount == mCountMax; 00079 } 00080 00082 uint count()const 00083 { 00084 return mCount; 00085 } 00086 00088 uint countMax()const 00089 { 00090 return mCountMax; 00091 } 00092 00094 ValueType& at( const uint aIndex ) 00095 { 00096 if ( mCount <= aIndex ) 00097 { 00098 XBASE_RANGE_ASSERT_MAX( aIndex , mCount ); 00099 return mPtr[ 0 ]; // fail safe code 00100 } 00101 return mPtr[ aIndex ]; 00102 } 00103 00105 const ValueType& at( const uint aIndex )const 00106 { 00107 if ( mCount <= aIndex ) 00108 { 00109 XBASE_RANGE_ASSERT_MAX( aIndex , mCount ); 00110 return mPtr[ 0 ]; // fail safe code 00111 } 00112 return mPtr[ aIndex ]; 00113 } 00114 00116 ValueType& first() { return at( 0 ); } 00118 const ValueType& first()const { return at( 0 ); } 00119 00121 ValueType& last() { return at( mCount - 1 ); } 00123 const ValueType& last()const { return at( mCount - 1 ); } 00124 00126 00128 00129 00131 void clear() 00132 { 00133 if ( mPtr == 0 ) 00134 { 00135 return; 00136 } 00137 00138 // 逆順でデストラクタを呼び出す 00139 for ( uint i = mCount; 0 < i; --i ) 00140 { 00141 const uint idx = i - 1; 00142 at( idx ).~ValueType(); 00143 } 00144 mCount = 0; 00145 } 00146 00148 00150 00151 00152 void add() 00153 { 00154 if ( isFull() ) 00155 { 00156 XBASE_NOT_REACH_ASSERT(); 00157 return; 00158 } 00159 new ( &mPtr[ mCount ] ) ValueType(); 00160 ++mCount; 00161 } 00162 template< typename A0 > 00163 void add( A0 a0 ) 00164 { 00165 if ( isFull() ) 00166 { 00167 XBASE_NOT_REACH_ASSERT(); 00168 return; 00169 } 00170 new ( &mPtr[ mCount ] ) ValueType( a0 ); 00171 ++mCount; 00172 } 00173 template< typename A0 , typename A1 > 00174 void add( A0 a0 , A1 a1 ) 00175 { 00176 if ( isFull() ) 00177 { 00178 XBASE_NOT_REACH_ASSERT(); 00179 return; 00180 } 00181 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 ); 00182 ++mCount; 00183 } 00184 template< typename A0 , typename A1 , typename A2 > 00185 void add( A0 a0 , A1 a1 , A2 a2 ) 00186 { 00187 if ( isFull() ) 00188 { 00189 XBASE_NOT_REACH_ASSERT(); 00190 return; 00191 } 00192 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 ); 00193 ++mCount; 00194 } 00195 template< typename A0 , typename A1 , typename A2 , typename A3 > 00196 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 ) 00197 { 00198 if ( isFull() ) 00199 { 00200 XBASE_NOT_REACH_ASSERT(); 00201 return; 00202 } 00203 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 ); 00204 ++mCount; 00205 } 00206 template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 > 00207 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 ) 00208 { 00209 if ( isFull() ) 00210 { 00211 XBASE_NOT_REACH_ASSERT(); 00212 return; 00213 } 00214 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 , a4 ); 00215 ++mCount; 00216 } 00217 template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 > 00218 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 ) 00219 { 00220 if ( isFull() ) 00221 { 00222 XBASE_NOT_REACH_ASSERT(); 00223 return; 00224 } 00225 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 , a4 , a5 ); 00226 ++mCount; 00227 } 00228 template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 > 00229 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 ) 00230 { 00231 if ( isFull() ) 00232 { 00233 XBASE_NOT_REACH_ASSERT(); 00234 return; 00235 } 00236 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 , a4 , a5 , a6 ); 00237 ++mCount; 00238 } 00239 template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 > 00240 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 ) 00241 { 00242 if ( isFull() ) 00243 { 00244 XBASE_NOT_REACH_ASSERT(); 00245 return; 00246 } 00247 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 ); 00248 ++mCount; 00249 } 00250 template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 , typename A8 > 00251 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 , A8 a8 ) 00252 { 00253 if ( isFull() ) 00254 { 00255 XBASE_NOT_REACH_ASSERT(); 00256 return; 00257 } 00258 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 ); 00259 ++mCount; 00260 } 00261 template< typename A0 , typename A1 , typename A2 , typename A3 , typename A4 , typename A5 , typename A6 , typename A7 , typename A8 , typename A9 > 00262 void add( A0 a0 , A1 a1 , A2 a2 , A3 a3 , A4 a4 , A5 a5 , A6 a6 , A7 a7 , A8 a8 , A9 a9 ) 00263 { 00264 if ( isFull() ) 00265 { 00266 XBASE_NOT_REACH_ASSERT(); 00267 return; 00268 } 00269 new ( &mPtr[ mCount ] ) ValueType( a0 , a1 , a2 , a3 , a4 , a5 , a6 . a7 , a8 , a9 ); 00270 ++mCount; 00271 } 00272 00274 00276 00277 ValueType& operator[]( const uint aIndex ) { return at( aIndex ); } 00278 const ValueType& operator[]( const uint aIndex )const { return at( aIndex ); } 00279 00280 00281 private: 00282 IAllocator& mAllocator; 00283 const uint mCountMax; 00284 uint mCount; 00285 ValueType* mPtr; 00286 }; 00288 } 00289 //------------------------------------------------------------ 00290 #endif 00291 // EOF