|
% n3 i9 v6 f; }) G4 G& D# I- y偶在一个老外的文章的基础上写的,应该很有用的说,
% b) G' }6 O# I8 D+ X以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 ' I/ t, |6 R7 U {- [
3 _2 X. {1 K. ^% K. q5 L7 ^7 @//------------------------------------------------------------------------- * e: N8 f! N1 J; r
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
! L) `: b, b7 q& l- j#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 : A' p3 |( ^# k% y" w
//-------------------------------------------------------------------------
$ r! |0 k7 t, ]1 X#include <classes.hpp> 7 x' o: x1 |1 N0 d" \6 j
//------------------------------------------------------------------------- $ l) G" T5 d i& R5 }- \+ ?4 I
template <class T>
/ n1 v6 o. Z9 V; ^: M' v9 Aclass TTypedList : public TList + _! |, T* Z) C" g/ V/ k& z5 N
{
3 ~7 Q h4 M" r; V- B4 i1 D typedef TList inherited; 1 V: F/ d4 j8 u3 Q" ]0 t4 k/ p/ K6 I
private:
. A# e; A) p. J+ ~( K bool FAutoClear;
- }6 r. }, c J TClass FItemClassType; 6 \8 Z8 ~# m6 o" W+ {! n
protected:
& ]( l8 G5 \3 B# h( a6 a T* __fastcall Get(int Index) const : }* X8 ^4 M! ?) \+ D
{
0 A; H% t) j% k2 C2 L4 N" t$ O return (T*)inherited::Get(Index); ' A9 {5 V6 m# x+ d. _- R6 q$ S! J
}
* l3 b$ }) G6 e" I void __fastcall Put(int Index, T* Item) 3 d$ W. [5 \. x8 y
{
) t5 C, B1 {: M8 P# I' Z inherited: ut(Index, Item); % R. C' S A* e% u6 S1 Q) Q# F A
} ) l! C# U4 w9 A4 n7 f
public:
' Q# @! k1 Y w A) }& ^ __property T* Items[int Index] = {read=Get, write=Put};
1 A( S) D2 q& u) |public: 9 C- H! {7 F l2 M3 E
__fastcall TTypedList(bool bAutoClear = true) 8 u, Q. ~: x! X* L4 e0 j
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
- p/ c2 n+ h% H0 P, `3 c { }
( ^3 u0 _0 e2 ^0 X: f+ y // no destructor needed 9 U `6 L# L! Q; Z X
int __fastcall Add(T* Item) 3 a$ C. D/ ?, [
{ $ V! A9 t+ p3 C
return inherited::Add(Item); 5 A0 Q% v8 ]* m; {8 Y r5 W
} / A5 Y0 ]$ W* g- S: d( F
int __fastcall AddList(const TTypedList& List) : }! [6 A H2 u+ U' s+ B
{
: o5 T" x0 F$ }. h2 c if(this == &List) 9 ?8 P- P5 `" f* l4 p
{
1 R' X( X4 p1 X3 b8 x throw EInvalidPointer("Can't add a TTypedList to himself!");
2 j, ]* P' O. B4 Y }
8 J8 Z q b& x if(FItemClassType != List.FItemClassType)
4 b8 d! L% O$ U u" I$ {& a {
! W3 [' m+ a+ D* I# n" D; G throw EConvertError("Can't add a TTypedList with a different ItemClass
. m8 C# x3 }9 c ]% SType!");
9 S! _7 h; P4 U& W }
5 @ B7 D5 O* z; g( x for(int i=0; i<List.Count; i++) $ A2 Q. U& w; g
{
2 w. R' I2 T' }- s' t Add(List.Get(i)); * d4 a4 e3 k- h, ?' ~
} - }, _) G4 j0 I
return List.Count; 3 \! K7 s3 X6 `3 Z/ A& @2 L6 M
} / A, D# ` M" ^
virtual void __fastcall Clear(void) ( m9 V+ T9 ]- X7 K* y4 _
{ $ B5 h. r5 a# E7 c
if(FAutoClear) 1 l, Q! h+ A" @
{ & O2 G% ?/ i/ C& o. O( o* v0 v+ h! B
for(int i=0; i<Count; i++)
* p5 N+ w2 T0 F0 t- T5 O {
$ v$ i2 h. C8 Y& q- r+ l delete Get(i);
C! _$ p9 k2 L6 F }
6 p: W2 A6 ]" I7 \; f& ^+ S } ! K/ P) n# f7 j+ D3 Y
inherited::Clear();
! u3 v. P! e9 c& \+ o } ( m6 Z$ }/ ^. [+ k
void __fastcall Delete(int Index, bool bAutoDelete = true) 8 ]& w4 b% X& H; s2 h j! S3 `6 U4 |% X
{ ( Q9 h2 X3 y/ ?2 m
if(bAutoDelete)
# E' Q! e: @+ e( h1 T3 R& V' a! d {
9 d4 \, H9 H" F' b! b delete Get(Index); / @3 e, q y/ O& s3 ]4 X4 a- {% p8 N" f
} * w1 o5 _; k i, I z' w, F3 _
inherited: elete(Index);
/ J! T. _5 C0 j5 @0 a } 9 G: [" i( L, t, N1 k
T* __fastcall First(void) const ( ]1 K u! [% l
{
; g: ]* K! b- X2 { E return (T*)inherited::First(); ; M' l% o$ B: ^( G7 _+ U4 t9 [) f9 e
} 7 t# H* o# q6 x) B% w
int __fastcall IndexOf(T* Item) const
' h0 W4 C- Q; `! I# H% H( H {
4 n! T( c) `2 E9 B( N' m3 y return inherited::IndexOf(Item); . @$ |* W$ P5 b
}
1 M6 Q2 y: F4 \9 n W, q( W# ` void __fastcall Insert(int Index, T* Item)
( J+ g Q* x e, O) Y( o5 d* V, v { . l' `5 U( G0 J" D# d3 n5 H7 i! N
inherited::Insert(Index, Item); 2 n+ X: [9 [% ?
}
- r3 i# E, \( v# x* U6 Z bool __fastcall IsEmpty(void) const 8 w5 o. i0 r5 h0 e/ I7 G1 |' C
{
. }8 M5 C9 W+ B1 W return Count == 0; + j9 a$ P4 n. h* M4 @; ~$ g1 p
}
& e* a) [- T3 K( O' a& H T* __fastcall Last(void) const
9 x2 R" k5 Z* [5 S$ d2 i: F+ L { $ A0 c2 l+ d* B& o, |
return (T*)inherited: ast();
, Q: m: j7 q. z; S% y* E+ b, _2 K }
- v4 [: _0 ?. [& O int __fastcall Remove(T* Item, bool bAutoDelete = true) ! U! i/ K/ a3 y/ a2 X) h
{
- H: B/ R+ D+ |8 E: n. Y int nIndex = inherited::Remove(Item); ) `1 W; g1 R X/ s- _& \' P: K
if(bAutoDelete && (nIndex != -1))
5 A3 X5 f7 [+ i+ K {
6 Y& h) Q+ A8 V( g1 f" e; p$ S$ t delete Item; 9 y; @. {8 ]% T
}
% y: y( V1 V5 c' v& ]: X% |, a return nIndex; ! R) U9 L0 V" i, l
} 3 P; t; N4 T- Y4 P
void operator<<(T* Item)
, r6 Q9 {; v2 n+ F: ^" h; P; ]4 w {
3 ]& c: C- q+ a( m- v Add((T *)Item); ' Q! B- Z. C6 S6 u% g! X7 l' H3 K
}
) ?+ F* l; E* z3 W5 |* j void operator<<(const TTypedList& List) 0 H8 L/ a9 t/ W
3 L, a' ]( W+ L2 f v {
- a4 g) L( b1 V, S AddList(List);
# P+ Q1 v |- | }
: R% p0 a# x7 @) Y}; // end of TTypedList - [5 z3 y. P! d- E: E# g
//------------------------------------------------------------------------- ) d7 A* B- [1 t0 I6 r4 E
#endif |