|
& i7 v9 w0 G& |" I, I0 \* N偶在一个老外的文章的基础上写的,应该很有用的说,
6 T/ {* s( N' m4 z# B* M E8 }以前总是用STL的vector做替代,但是STL的用法实在不是很习惯
( T6 s* ^/ c8 i0 o: G
# A6 Q7 a+ f3 ~& _, a3 ?3 H6 U//------------------------------------------------------------------------- 2 a6 u( ?7 u- b& S' l7 F
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 0 y6 t: }* z1 `9 w$ I, B: e( h P
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 - i U1 d( O) u ~
//------------------------------------------------------------------------- 8 b; ?; h0 U6 v# T9 q1 n, h
#include <classes.hpp> ( J* G3 `1 b1 W- x5 l
//-------------------------------------------------------------------------
- |: L( l. A5 G1 m: c' n+ { Atemplate <class T>
; o; {1 }9 U% ~! y% P$ ~- a# Qclass TTypedList : public TList
/ k) i. k" C9 y) ^, |; n{
8 d8 \5 y5 q/ c typedef TList inherited; * c# \% Y8 F% l/ J6 X- I
private: ; c1 f: m# h. _
bool FAutoClear; 5 J# ]4 m9 V$ I
TClass FItemClassType;
) W( w. }, u2 Sprotected:
' F& z7 L# D, @! S9 P T* __fastcall Get(int Index) const ( B D6 `& A" L, p' k# x) @
{
8 i/ B3 r& {. D+ H8 Y return (T*)inherited::Get(Index); : t: ]0 W! |6 K+ |3 J' v6 E& G
} . a( r4 K: S6 ]* F$ n6 t& Z
void __fastcall Put(int Index, T* Item)
8 T4 D. E$ t1 N- ^! j { / b8 f8 p6 v3 T
inherited: ut(Index, Item);
/ T2 [% {# ?: a9 C7 N* O } 4 P) }0 S# T8 a' N" w/ e5 }
public:
$ }- l- P5 W: B2 D' n __property T* Items[int Index] = {read=Get, write=Put}; 0 P, ^+ V/ _( r6 j: z ^0 r1 u
public:
' p2 Q5 k% Z0 ^. l __fastcall TTypedList(bool bAutoClear = true) . o1 ^, P8 w9 r0 t% [
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T)) 9 B+ w! ?* y5 l0 A. a, c5 [
{ } & a4 ?4 u/ N, ?8 A0 q6 {
// no destructor needed , @+ I$ W* J4 H) r5 [
int __fastcall Add(T* Item) 4 e' ~8 q6 B2 h! s
{ , y& g9 Y2 a) g* j3 O2 R( U% I
return inherited::Add(Item); / R' |! H" _. e" `6 j
} - m4 E; [$ U7 P& |
int __fastcall AddList(const TTypedList& List)
' O8 G* a; x' ~# n- g4 k( z; R2 ^+ W {
. \. U# t/ `* R- f if(this == &List) |4 P4 ~% R2 ^
{ 1 H' t* Y5 n7 K2 A* r$ {
throw EInvalidPointer("Can't add a TTypedList to himself!");
7 R2 g/ n! k4 N1 I! l } 5 d- a; S2 w: V8 z
if(FItemClassType != List.FItemClassType)
' d2 Q3 X) K @ { 4 _9 r5 C+ W- r1 V
throw EConvertError("Can't add a TTypedList with a different ItemClass " T0 J2 _5 |$ S4 R7 w) i9 {
Type!");
' H) S6 E. A) j' w }
7 D) J; o( k6 D4 x6 P+ d9 s- G for(int i=0; i<List.Count; i++) , m" Q3 R# l' P7 G" t
{ * `* K: _4 R6 ~9 G
Add(List.Get(i));
1 h: y2 g8 u# _$ }# v5 R8 c }
; _9 Q2 k+ ]& e7 G/ q4 w$ q return List.Count;
4 r; F8 h1 g$ k- P- h } ) V/ h# \5 Z7 p' m, A6 L j
virtual void __fastcall Clear(void) # U! s; _; k7 x; q. @6 ]" e8 s
{ ( Z W, }* N$ A4 \& i* q
if(FAutoClear)
2 r$ `6 j8 |- U* [; T3 ~' E { 7 n. R* P$ o4 z% S6 u0 T# [3 A9 F
for(int i=0; i<Count; i++) ) r: }! o. u' n+ l5 S3 J
{
( M m6 X) o: y3 \- W delete Get(i);
# r3 B* Z- K2 M# U+ A+ Q }
$ v. k% \4 u' |. x% G }
1 P4 F, G0 Y2 Y4 Z \" v* u inherited::Clear(); ' z, K# k! k' r3 C2 v# l4 J; B
}
8 j H4 u/ K: m% q void __fastcall Delete(int Index, bool bAutoDelete = true)
* i) P. A# h% x& k, P { 2 b# j* p$ [9 h. a5 K% S
if(bAutoDelete)
7 ^* } ?0 S8 ?! E7 x# i {
' `. k; F3 R! v" }0 _6 M delete Get(Index); : k- f. S! y/ R+ X6 M( H$ A
} & a c: \ e8 u. G0 A* c( ?0 d& e
inherited: elete(Index);
- D W( b4 j& f( U# r# T } 8 A' R& N, Y! Y3 H$ f) O) ]% C2 m
T* __fastcall First(void) const " b& ~" e/ d- t* f$ A9 W& }
{ 3 v; m; b4 o6 h/ G8 \
return (T*)inherited::First();
- z @3 \7 F7 P0 N% [- Q% u* g }
* Q6 _) ~# q3 E& c int __fastcall IndexOf(T* Item) const ' V8 Z1 l% b6 z$ E+ n6 G6 h
{
% k; D Y' W6 ^; l9 g" S8 W return inherited::IndexOf(Item); / l( D7 r3 ~ i0 N# x g2 @. Z
}
. w( n, A( U% L5 w void __fastcall Insert(int Index, T* Item) + H. Y c7 O# i
{
5 {' E; O! W' ]% O1 Q inherited::Insert(Index, Item); ( H. l$ `' S' x8 O# m7 h) E
} 5 t3 L" }( ?8 m' V
bool __fastcall IsEmpty(void) const
9 F1 Y2 c: X1 n! q3 S. W7 ~ { 7 Y) u7 i! U9 [
return Count == 0;
2 X2 d+ @# c5 ~. |, H' u* e } # }1 I. N) R& `6 R, i
T* __fastcall Last(void) const , z, y+ X1 w! r. x8 I' y
{
1 `) F0 z% g& d v return (T*)inherited: ast(); : D I7 W5 i% X4 V
}
, L7 ]3 Z7 Z; t5 G int __fastcall Remove(T* Item, bool bAutoDelete = true)
) H4 z. o& k1 a6 M+ O { % D2 A' E9 K3 G$ J. A- ^! e
int nIndex = inherited::Remove(Item);
$ B( Z5 I! c) Y8 y% t if(bAutoDelete && (nIndex != -1))
+ q) q8 d2 t: O3 M6 M { * j* _$ f3 D1 }' S
delete Item;
0 K* B5 D+ Y; w% ~ d9 N: @ } & r8 t( z1 c7 c! ?+ z$ A6 m. e8 R+ a
return nIndex; ! U4 m% o9 J8 [% Y+ M6 h5 J
}
! p! R! L* R8 W8 A void operator<<(T* Item)
3 ?3 G: J( j2 O& I; w$ S. f- Q {
5 z4 |% j4 c! E) ]# b Add((T *)Item);
7 g. i. j, V. S }
3 R6 m k W) K+ g6 M+ s! I q+ @ void operator<<(const TTypedList& List)
* A7 S/ ]( S7 p( U9 ]( u$ W K% U* I! ^5 K' H* r8 p( `
{ ) ~+ g" c" y# M( J2 d/ n: I9 P( S
AddList(List); 5 P ]( G1 p, r9 T
} / X" o1 j* N2 }; U" L8 y
}; // end of TTypedList
# K. F5 ?# L6 f7 P+ M7 E//------------------------------------------------------------------------- - a/ [# Q' k u( P. h+ ^
#endif |