TTypedList一个类型安全的TList模板类
<P> </P><P>偶在一个老外的文章的基础上写的,应该很有用的说, </P>
<P>以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 :) </P>
<P> </P>
<P>//------------------------------------------------------------------------- </P>
<P>#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 </P>
<P>#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 </P>
<P>//------------------------------------------------------------------------- </P>
<P>#include <classes.hpp> </P>
<P>//------------------------------------------------------------------------- </P>
<P>template <class T> </P>
<P>class TTypedList : public TList </P>
<P>{ </P>
<P> typedef TList inherited; </P>
<P>private: </P>
<P> bool FAutoClear; </P>
<P> TClass FItemClassType; </P>
<P>protected: </P>
<P> T* __fastcall Get(int Index) const </P>
<P> { </P>
<P> return (T*)inherited::Get(Index); </P>
<P> } </P>
<P> void __fastcall Put(int Index, T* Item) </P>
<P> { </P>
<P> inherited::Put(Index, Item); </P>
<P> } </P>
<P>public: </P>
<P> __property T* Items = {read=Get, write=Put}; </P>
<P>public: </P>
<P> __fastcall TTypedList(bool bAutoClear = true) </P>
<P> : TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T)) </P>
<P> { } </P>
<P> // no destructor needed </P>
<P> int __fastcall Add(T* Item) </P>
<P> { </P>
<P> return inherited::Add(Item); </P>
<P> } </P>
<P> int __fastcall AddList(const TTypedList& List) </P>
<P> { </P>
<P> if(this == &List) </P>
<P> { </P>
<P> throw EInvalidPointer("Can't add a TTypedList to himself!"); </P>
<P> } </P>
<P> if(FItemClassType != List.FItemClassType) </P>
<P> { </P>
<P> throw EConvertError("Can't add a TTypedList with a different ItemClass </P>
<P>Type!"); </P>
<P> } </P>
<P> for(int i=0; i<List.Count; i++) </P>
<P> { </P>
<P> Add(List.Get(i)); </P>
<P> } </P>
<P> return List.Count; </P>
<P> } </P>
<P> virtual void __fastcall Clear(void) </P>
<P> { </P>
<P> if(FAutoClear) </P>
<P> { </P>
<P> for(int i=0; i<Count; i++) </P>
<P> { </P>
<P> delete Get(i); </P>
<P> } </P>
<P> } </P>
<P> inherited::Clear(); </P>
<P> } </P>
<P> void __fastcall Delete(int Index, bool bAutoDelete = true) </P>
<P> { </P>
<P> if(bAutoDelete) </P>
<P> { </P>
<P> delete Get(Index); </P>
<P> } </P>
<P> inherited::Delete(Index); </P>
<P> } </P>
<P> T* __fastcall First(void) const </P>
<P> { </P>
<P> return (T*)inherited::First(); </P>
<P> } </P>
<P> int __fastcall IndexOf(T* Item) const </P>
<P> { </P>
<P> return inherited::IndexOf(Item); </P>
<P> } </P>
<P> void __fastcall Insert(int Index, T* Item) </P>
<P> { </P>
<P> inherited::Insert(Index, Item); </P>
<P> } </P>
<P> bool __fastcall IsEmpty(void) const </P>
<P> { </P>
<P> return Count == 0; </P>
<P> } </P>
<P> T* __fastcall Last(void) const </P>
<P> { </P>
<P> return (T*)inherited::Last(); </P>
<P> } </P>
<P> int __fastcall Remove(T* Item, bool bAutoDelete = true) </P>
<P> { </P>
<P> int nIndex = inherited::Remove(Item); </P>
<P> if(bAutoDelete && (nIndex != -1)) </P>
<P> { </P>
<P> delete Item; </P>
<P> } </P>
<P> return nIndex; </P>
<P> } </P>
<P> void operator<<(T* Item) </P>
<P> { </P>
<P> Add((T *)Item); </P>
<P> } </P>
<P> void operator<<(const TTypedList& List) </P>
<P> { </P>
<P> AddList(List); </P>
<P> } </P>
<P>}; // end of TTypedList </P>
<P>//------------------------------------------------------------------------- </P>
<P>#endif </P>
页:
[1]