|
: r9 |- @" k% d" A/ P
偶在一个老外的文章的基础上写的,应该很有用的说, 4 u) }# ]" C, g% E$ r1 B$ q
以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 L9 T4 W+ [! ?* t9 r4 ?! G
! `( G. d, L7 z' M6 C
//-------------------------------------------------------------------------
0 _+ W! ~8 Y4 [2 L# ~#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 4 m, X1 J. a) J+ }$ Q- y1 W
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 ' w4 C- r1 D* h
//------------------------------------------------------------------------- ! z7 y4 w6 F# t: O }
#include <classes.hpp>
0 _( c: x& f. B0 I//-------------------------------------------------------------------------
, |8 x1 g6 V) @: n0 a3 ktemplate <class T>
. U/ X a+ H! u7 u; i( P/ V1 Wclass TTypedList : public TList
! l; F( c! K2 H{ : b7 s1 _4 L7 C% d* Q2 N2 J
typedef TList inherited;
% K: H; o# ^5 k Q# ]+ pprivate:
# w4 _% W2 r! J) w6 K( Q/ u bool FAutoClear; 1 i5 D& r6 [/ I0 O/ J- F
TClass FItemClassType; + m$ \0 g- Z+ o! b0 R* t) i
protected: * u1 c/ t0 a$ S3 I* p& Q d
T* __fastcall Get(int Index) const
- g) ?; j7 X* l% e0 G5 ^- l! }" Z { % ]0 D; Z2 a4 ~7 p/ m" d
return (T*)inherited::Get(Index); ! k% h8 _) t6 [& K- Z
} . d$ J+ U. B n4 S4 A
void __fastcall Put(int Index, T* Item)
6 p6 N0 a; F5 I' S' L1 V$ x3 H+ J {
) O% j4 E, ~/ h, t N+ C5 B inherited: ut(Index, Item);
7 x B, U7 ?. @, S0 J } $ X7 P7 x2 i( c$ x' [/ ~
public: # }4 X" t3 L! t+ J
__property T* Items[int Index] = {read=Get, write=Put};
. @2 w! f% o5 O2 `public: - H& T5 o2 e* H* G3 a+ M
__fastcall TTypedList(bool bAutoClear = true) ) e- r3 q0 t9 K# T
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T)) % ^/ @, V) h# J+ }3 I4 R+ g0 m5 W
{ }
{1 l- M8 J" v+ I5 x9 ~3 D9 w // no destructor needed " b0 x6 J! {# a, U
int __fastcall Add(T* Item)
2 P- l: d6 ]3 b# ?0 q* ^3 g+ }( i {
z+ e; p- d& N- }* T" e" Q" Y return inherited::Add(Item);
% M" x9 O i4 \( e% r }
# P B4 v4 E7 h int __fastcall AddList(const TTypedList& List)
8 t) @% Q+ i1 Z% p5 r/ \4 P { 1 W& Z/ ~6 G* c* a% J! t
if(this == &List) 6 K+ m3 i% L9 @5 t% p w
{ ( k. P5 V# e) _9 C; x7 R- }
throw EInvalidPointer("Can't add a TTypedList to himself!");
+ W8 i, x# F+ [( z! h( f0 m } , H. R3 @3 k- o( T* {0 G3 S/ ?
if(FItemClassType != List.FItemClassType) 4 _) Q4 R4 ^* |: k
{
$ G6 T+ m, { |$ n& F8 o$ t throw EConvertError("Can't add a TTypedList with a different ItemClass ' V$ X5 g) e' x- e \* ]
Type!");
/ ]- F; O2 N }1 q }
8 ?, x0 _) ^& a5 M for(int i=0; i<List.Count; i++)
2 ~1 @! t2 g, _5 G( Y/ E {
: {+ w( G/ S+ m- z8 L Add(List.Get(i)); 3 {3 ~. O* G- R `
}
/ R8 _) K2 }% x `0 U return List.Count; 5 q. n& |# B1 \0 o& a4 V
}
. M3 u" C! w& A% K O virtual void __fastcall Clear(void)
: R6 [+ v: _' \* Y {
7 i0 g, Q% b& z! y: ?2 T: j if(FAutoClear) ; d2 @1 e4 p- q: M1 P% u8 B$ m7 u
{
, A, p, e4 [' @. F% ` for(int i=0; i<Count; i++) # D8 t' T9 I# Q( I/ l
{
' W' ? d1 K6 @6 o" E+ l; ?. m delete Get(i);
7 X9 O& q" l# h5 q }
) r6 |5 x# j" u0 ` } 6 t- g9 C! r1 P1 l" O
inherited::Clear(); 0 } l$ u7 t$ n0 V6 m" R' c
}
# ]0 f9 k* o+ k1 Z( N) t void __fastcall Delete(int Index, bool bAutoDelete = true) 2 q% `1 u1 m( ^" u) h7 L
{
7 P; ?- n4 K; k* F if(bAutoDelete) 5 K) s1 W3 |9 I0 i8 t; P# w- X
{
- o' v+ V% i3 i+ T" Z2 M/ u delete Get(Index); : H$ e0 V* h$ w% u W* F! K; d: c
} 3 q* g# u3 {, s* |+ b, m
inherited: elete(Index); + X7 E6 ^: @* [% f& x' X8 h
} + X7 ]3 M- y7 o6 s" o3 W2 a
T* __fastcall First(void) const
& y4 p/ A1 w4 P2 N% g$ V. U { $ H" U( S/ h4 o" K4 Q
return (T*)inherited::First();
5 a O/ b+ k9 d" X# m3 Y& ? } $ i2 v- \' M/ Z. v( C' m$ `4 p% I
int __fastcall IndexOf(T* Item) const
* O# j# Y6 F& a$ J/ ]) Y$ c& O8 s/ D { % r4 s0 i. D7 V/ W y
return inherited::IndexOf(Item); & f) w3 M6 h0 x8 Q5 Q) T/ `
}
- ]& @2 K4 { A- c, c void __fastcall Insert(int Index, T* Item)
/ w O) e5 W) `& g6 g! n {
( }% ?% L& a! S$ B2 _ inherited::Insert(Index, Item); % w8 c) e' a* q7 G; @) j; U# K
}
% A- ?( k, w8 i& ` bool __fastcall IsEmpty(void) const 2 c* b. M% I; ~1 S- I
{
2 q f: x! ?3 m* }1 S8 ^ return Count == 0;
4 L" b, j% O7 z, i) K } ! ]# f8 p* s5 S5 i4 t
T* __fastcall Last(void) const 0 o; _4 y3 i0 V* Z# c
{ / V6 |0 X/ S7 e, _( Z! ~2 k' ~) A8 a7 ?
return (T*)inherited: ast();
- y" J& Q3 U9 d+ i- A K } . `7 X# C5 x3 d8 x. V$ {
int __fastcall Remove(T* Item, bool bAutoDelete = true) 7 d; v- U( {( s9 G
{ . }9 T2 r2 a5 a" T+ ^. [
int nIndex = inherited::Remove(Item); . a0 f+ | q5 p
if(bAutoDelete && (nIndex != -1))
" _" [8 Z* s3 F$ L, j* {$ Y4 H {
! A, Z1 j& u; N: ]% O9 {6 N0 \ delete Item;
; i+ r( B0 V8 \$ s* N }
- y3 ^0 h" `- a2 s) G8 E1 S) M5 O return nIndex;
6 q: l# B8 W% z$ p, t1 l }
2 X4 [- j6 V# N2 J7 n. k. z. } void operator<<(T* Item) % d3 v% X# b; q7 F& u
{ $ n8 e$ w7 U7 Q7 u; e2 z/ }
Add((T *)Item);
R# Y/ d$ o. s& o# G5 l. ^ }
n: H2 \3 B6 |1 X void operator<<(const TTypedList& List)
$ [7 t7 }! b! Y/ @, A. A6 C! n& T9 R8 _! i5 R0 U9 p$ d6 r* s
{ 6 c/ v2 G1 I$ D% q% b& h# ~
AddList(List); : p0 \2 n k: `3 i
}
2 ]& N" Z5 R- D6 n}; // end of TTypedList
, \, O! B8 v7 D' E+ i5 A# U. o//------------------------------------------------------------------------- / |/ U# v. U |0 {% K8 ~: M0 W
#endif |