* `: {5 X7 R3 j' f0 Q; O
偶在一个老外的文章的基础上写的,应该很有用的说,
以前总是用STL的vector做替代,但是STL的用法实在不是很习惯
4 x6 u, I. f5 K9 u! x( u
//-------------------------------------------------------------------------
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
* V V& Q# D, C7 C I D8 t+ t( S#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
//-------------------------------------------------------------------------
6 s( t, I# V( Q4 |% X0 |+ ~4 U#include <classes.hpp>
//-------------------------------------------------------------------------
template <class T>
9 O2 K, q% T( V; @1 u/ s: ]class TTypedList : public TList
1 Z/ ]7 Y( Z9 I% q2 A0 h{
+ T: G" `& d6 Itypedef TList inherited;
private:
1 }1 E; A. E! d; n" ]5 k5 T o+ fbool FAutoClear;
TClass FItemClassType;
protected:
& k/ @9 B9 V' ?, ET* __fastcall Get(int Index) const
{
return (T*)inherited::Get(Index);
}
t2 I( V' @6 r: n: [void __fastcall Put(int Index, T* Item)
9 m! c$ _' B/ L# Q6 e) _5 Q" U{
( |6 i& [0 o, o: O% y inherited:
ut(Index, Item);
}
public:
__property T* Items[int Index] = {read=Get, write=Put};
" Z3 Z1 [/ K Opublic:
! D( r% u* [: M# G! @" V$ M__fastcall TTypedList(bool bAutoClear = true)
1 E) `- T6 }& e- w+ G5 n: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
{ }
: N- U) Y! U! R) K+ Z// no destructor needed
: s# t; n& V1 j- eint __fastcall Add(T* Item)
; f9 n. u/ G0 ^; \4 q{
return inherited::Add(Item);
% y* N' n2 {. J}
int __fastcall AddList(const TTypedList& List)
b- @/ N# A. V. \ ~1 n{
! V+ ?2 Y6 N% U8 i* t' aif(this == &List)
6 B l7 E" W" J0 ~: e1 n; L0 z+ I C{
throw EInvalidPointer("Can't add a TTypedList to himself!");
}
, g5 \* n, H$ I; [! Nif(FItemClassType != List.FItemClassType)
{
throw EConvertError("Can't add a TTypedList with a different ItemClass
Type!");
- q! o7 Y8 q- ~$ x}
for(int i=0; i<List.Count; i++)
{
Add(List.Get(i));
}
7 g4 a/ E' s3 k- R! I' _' Dreturn List.Count;
# ]* a9 P) u, ]}
B" n* l; e+ v9 @# p5 ^. \1 }virtual void __fastcall Clear(void)
{
7 B: ]* P' Q/ s+ u; dif(FAutoClear)
4 R, l/ T; l$ A1 B5 F{
for(int i=0; i<Count; i++)
{
delete Get(i);
1 B' k' T6 q" d+ S; K1 b}
$ m# P6 s" A0 x3 [7 f# t- u V}
4 J0 s6 }! k* cinherited::Clear();
* d4 b+ u5 H( j" f! E. H4 p}
void __fastcall Delete(int Index, bool bAutoDelete = true)
{
if(bAutoDelete)
{
2 `3 e- J' B, k7 i* D( ]7 ldelete Get(Index);
4 W0 d9 Q( i9 M6 K}
$ ^5 J3 |4 C7 `1 ` inherited:
elete(Index);
}
T* __fastcall First(void) const
# k- r) H8 P$ t; K{
return (T*)inherited::First();
}
int __fastcall IndexOf(T* Item) const
9 o' c4 X7 \/ P6 y; Y0 B{
return inherited::IndexOf(Item);
}
9 P3 B+ U- V' ~# }& \7 q( Mvoid __fastcall Insert(int Index, T* Item)
- X4 U& _& v" |3 G8 H1 }' s! L3 _{
inherited::Insert(Index, Item);
. e( x6 a% V' D* L' o}
) q" R1 `) A! V) ?; V" u5 \bool __fastcall IsEmpty(void) const
{
return Count == 0;
$ B+ T4 h8 n+ Q; }8 n& u5 J! G}
Z" \; u- V5 R& ?T* __fastcall Last(void) const
{
% d7 O2 F8 F8 P3 ~ return (T*)inherited:
ast();
}
int __fastcall Remove(T* Item, bool bAutoDelete = true)
, X) M& l" Q# H) J{
/ g3 L3 V: f0 u$ b, d$ p5 ?; hint nIndex = inherited::Remove(Item);
, Y0 R" r" D9 v& _) B' D( x4 K. oif(bAutoDelete && (nIndex != -1))
5 \- z: v& F2 ^1 D& z{
delete Item;
, c- e# r, m4 C- Z/ ~}
( K: e) p* v, R* L" [return nIndex;
}
void operator<<(T* Item)
3 x) h8 l o( a4 B1 l+ U{
Add((T *)Item);
4 a/ Z& U: b5 y: U& p}
void operator<<(const TTypedList& List)
) _& e6 g7 k6 ^2 |; c; f! S% @. o0 d{
AddList(List);
* q4 x- ?0 a0 L ?% _* i}
5 c1 |. j w9 `. Z3 H}; // end of TTypedList
//-------------------------------------------------------------------------
#endif
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |