CrossFramework Library
|
00001 00006 #if defined(XBASE_INCLUDED_RUNTIMEARRAY_HPP) 00007 #else 00008 #define XBASE_INCLUDED_RUNTIMEARRAY_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 RuntimeArray : public ::XBase::NonCopyable 00022 { 00023 public: 00025 00026 typedef T ValueType; 00028 00030 00031 00039 RuntimeArray( uint aCount , IAllocator& aAllocator = IAllocator::Default() ) 00040 : mAllocator( aAllocator ) 00041 , mCount( aCount ) 00042 , mPtr( 0 ) 00043 { 00044 if ( 0 < mCount ) 00045 { 00046 mPtr = reinterpret_cast< ValueType* >( mAllocator.alloc( sizeof( ValueType ) * mCount ) ); 00047 } 00048 } 00049 00051 ~RuntimeArray() 00052 { 00053 if ( mPtr != 0 ) 00054 { 00055 ValueType* ptr = mPtr; 00056 mPtr = 0; 00057 mAllocator.free( reinterpret_cast< ptr_t >( ptr ) ); 00058 } 00059 } 00060 00062 00064 00065 00066 uint count()const 00067 { 00068 return mCount; 00069 } 00070 00072 ValueType& at( const uint aIndex ) 00073 { 00074 if ( mCount <= aIndex ) 00075 { 00076 XBASE_RANGE_ASSERT_MAX( aIndex , mCount ); 00077 return mPtr[ 0 ]; // fail safe code 00078 } 00079 return mPtr[ aIndex ]; 00080 } 00081 00083 const ValueType& at( const uint aIndex )const 00084 { 00085 if ( mCount <= aIndex ) 00086 { 00087 XBASE_RANGE_ASSERT_MAX( aIndex , mCount ); 00088 return mPtr[ 0 ]; // fail safe code 00089 } 00090 return mPtr[ aIndex ]; 00091 } 00092 00094 00096 00097 ValueType& operator[]( const uint aIndex ) { return at( aIndex ); } 00098 const ValueType& operator[]( const uint aIndex )const { return at( aIndex ); } 00099 00100 00101 private: 00102 IAllocator& mAllocator; 00103 const uint mCount; 00104 ValueType* mPtr; 00105 }; 00107 } 00108 //------------------------------------------------------------ 00109 #endif 00110 // EOF