"L. Spiro Engine"
|
00001 00018 #ifndef __LSTL_VECTORPOD_H__ 00019 #define __LSTL_VECTORPOD_H__ 00020 00021 #include "../LSTLib.h" 00022 #include "LSTLSVectorCrtp.h" 00023 00024 namespace lstl { 00025 00034 template <typename _tType, typename _tDataType = LSUINT16, unsigned _uAllocSize = 512UL> 00035 class CVectorPoD : public CSVectorCrtp<CVectorPoD<_tType, _tDataType, _uAllocSize>, _tType, _tDataType, _uAllocSize> { 00036 public : 00037 // == Various constructors. 00038 LSE_CALL CVectorPoD() : 00039 m_paOurAllocator( Parent::m_paDefaultAllocator ) { 00040 } 00041 explicit LSE_CALL CVectorPoD( LSUINT32 _tTotal ) : 00042 m_paOurAllocator( Parent::m_paDefaultAllocator ) { 00043 Allocate( _tTotal ); 00044 } 00045 explicit LSE_CALL CVectorPoD( CAllocator * _paAllocator ) : 00046 m_paOurAllocator( _paAllocator ) { 00047 if ( !m_paOurAllocator ) { 00048 m_paOurAllocator = Parent::m_paDefaultAllocator; 00049 } 00050 } 00051 LSE_CALL CVectorPoD( LSUINT32 _tTotal, CAllocator * _paAllocator ) : 00052 m_paOurAllocator( _paAllocator ) { 00053 if ( !m_paOurAllocator ) { 00054 m_paOurAllocator = Parent::m_paDefaultAllocator; 00055 } 00056 Allocate( _tTotal ); 00057 } 00058 LSE_CALL CVectorPoD( const CVectorPoD<_tType, _tDataType, _uAllocSize> &_vOther, CAllocator * _paAllocator = NULL ) : 00059 m_paOurAllocator( _paAllocator ) { 00060 if ( !m_paOurAllocator ) { 00061 m_paOurAllocator = Parent::m_paDefaultAllocator; 00062 } 00063 (*this) = _vOther; 00064 } 00065 LSE_CALL ~CVectorPoD() { 00066 Reset(); 00067 } 00068 00069 00077 LSBOOL LSE_CALL Allocate( LSUINT32 _ui32Total ) { 00078 // If allocating 0 bytes, just reset the list. 00079 if ( !_ui32Total ) { 00080 Reset(); 00081 return true; 00082 } 00083 00084 // Destroy items that are going to be removed. 00085 if ( Parent::m_tLen ) { 00086 for ( LSUINT32 I = Parent::m_tLen; --I >= _ui32Total; ) { 00087 Parent::Destroy( static_cast<_tDataType>(I) ); 00088 } 00089 } 00090 // Adjust the length. 00091 if ( Parent::m_tLen > static_cast<_tDataType>(_ui32Total) ) { Parent::m_tLen = static_cast<_tDataType>(_ui32Total); } 00092 00093 // Attempt to allocate. 00094 _tType * ptNew = reinterpret_cast<_tType *>(m_paOurAllocator->ReAlloc( Parent::m_ptData, _ui32Total * sizeof( _tType ) )); 00095 if ( !ptNew ) { return false; } 00096 00097 // Success. 00098 Parent::m_ptData = ptNew; 00099 Parent::m_tAllocated = static_cast<_tDataType>(_ui32Total); 00100 return true; 00101 } 00102 00106 LSVOID LSE_CALL Reset() { 00107 for ( LSUINT32 I = Parent::m_tLen; I--; ) { 00108 Parent::Destroy( static_cast<_tDataType>(I) ); 00109 } 00110 if ( Parent::m_ptData ) { 00111 m_paOurAllocator->Free( Parent::m_ptData ); 00112 Parent::m_ptData = NULL; 00113 } 00114 Parent::m_tLen = Parent::m_tAllocated = 0; 00115 } 00116 00124 LSVOID LSE_CALL SetAllocator( CAllocator * _paAllocator ) { 00125 Reset(); 00126 m_paOurAllocator = _paAllocator; 00127 if ( !m_paOurAllocator ) { 00128 m_paOurAllocator = Parent::m_paDefaultAllocator; 00129 } 00130 } 00131 00137 CAllocator * LSE_CALL GetAllocator() { 00138 return m_paOurAllocator; 00139 } 00140 00141 protected : 00142 // == Members. 00146 CAllocator * m_paOurAllocator; 00147 00148 private : 00149 typedef CSVectorCrtp<CVectorPoD<_tType, _tDataType, _uAllocSize>, _tType, _tDataType, _uAllocSize> 00150 Parent; 00151 }; 00152 00153 00154 } // namespace lstl 00155 00156 #endif // __LSTL_VECTORPOD_H__