|
4 P2 r% T' j+ ^4 z& r6 o2 k偶在一个老外的文章的基础上写的,应该很有用的说,
4 P% I0 h* K- N以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 : R( {# M4 E/ L1 E
- `, V9 l, m: f+ }3 L
//------------------------------------------------------------------------- ! S' W& R: c G' x; M4 [
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 & Y6 Y% ?3 V, ^5 e6 \1 u
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
% k6 b5 E. M* }% S8 P0 X) {//-------------------------------------------------------------------------
1 o/ g# n0 w+ k- O4 c* s) c#include <classes.hpp> ) u1 F$ W. i. Q6 P: ~
//-------------------------------------------------------------------------
; c* n$ `. z" u0 Ttemplate <class T>
9 ^/ m, W* c4 u f% E. _" bclass TTypedList : public TList 2 h6 W/ ?7 ?0 W ]8 J0 h
{ 4 h5 U0 O/ b# Y# g" t
typedef TList inherited; / O! ]- p0 V9 g- ^/ l4 ^; Q" m: i
private: 7 y. C8 U* Y- }+ F% L# {
bool FAutoClear;
* U' j/ x q- C) I TClass FItemClassType;
( p7 j" A3 r9 s5 n8 ]6 ~- Iprotected: 7 z( i+ h) l9 o- T1 T3 x: n
T* __fastcall Get(int Index) const 4 A4 R4 c- P. Y4 N
{ * X0 H. p* |) | d. `2 k, K
return (T*)inherited::Get(Index);
. O6 Q8 w5 ~9 ~9 |# ? } - D, C& s" S! m. a. P
void __fastcall Put(int Index, T* Item) & P) {$ ?, r1 t; f/ { Y
{
# ~% A2 I# C* X$ v$ C! h inherited: ut(Index, Item); 8 v/ E( _8 n) |! ~7 L3 v
} ( E% D, l# F, e. t2 \" j( _
public:
" y9 a: b9 D/ {% D% K# A y __property T* Items[int Index] = {read=Get, write=Put};
9 K" w/ O# [* G l% @public:
( J2 I& g% j- j' F$ Z) Y __fastcall TTypedList(bool bAutoClear = true)
- v! q# \' D2 k : TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
3 J0 C2 M1 ?; C: U `5 ^8 k { } ; o. M ]& H" w9 n+ t
// no destructor needed ) G; u, {& |5 [$ T
int __fastcall Add(T* Item)
! d' ]* Z- P! E {
- R6 ^8 ^; K& _ return inherited::Add(Item); % C# t' W- ?; e8 Q8 d/ T
} ' ^: S+ A- D+ l C1 e I' j! }
int __fastcall AddList(const TTypedList& List) 5 m( i8 _8 t" ?! x
{
3 a( ^+ n8 c8 Y/ F1 o9 H% q if(this == &List)
' W7 j; o. _8 [0 C, g5 [, n. { {
; w, N. d9 x! {5 `" { throw EInvalidPointer("Can't add a TTypedList to himself!");
% E% }5 @ \0 B0 d1 b! P, m! v } 4 N- e7 J# A9 R
if(FItemClassType != List.FItemClassType)
" F, c1 D# t: ^3 a; A. v {
4 j }# W1 w- O; ?7 r throw EConvertError("Can't add a TTypedList with a different ItemClass
$ Y% }4 | O {+ L. Y Z* p! I2 }- EType!"); ) J2 O" ?8 j' w& r
}
7 Q5 D ?# w3 X/ R for(int i=0; i<List.Count; i++) ' ?% R, N/ n( M" v) E
{
- r+ ~6 K' I4 L! _' |8 N' w Add(List.Get(i)); - h, Z& q1 J* a- t- z
}
% `) Q* w1 G: q4 z' W return List.Count; , }# D: L" _# W- U5 W/ M, Q. f) @- Y
} L) k8 ~; f$ n4 F2 C9 R( C
virtual void __fastcall Clear(void) U3 w3 U; ]8 f+ z# b6 r
{ ! Y: r \( f# ^: l# n# ]
if(FAutoClear) 2 I) r: z' W5 p2 B4 @+ o; \
{ 5 B2 k E' J7 _5 ]/ B" W4 @" }* t& }" `
for(int i=0; i<Count; i++)
$ F( u# F. Z h1 `6 P! e- `2 [ {
f- h4 k( F' _$ ~$ \+ A delete Get(i); 0 H2 n- [9 s8 C& |( a
} 6 a5 ]) x( ?5 b/ m- F% @, Y& P
} + L. s9 f7 B! K" B; |
inherited::Clear(); # z% F# ^5 l5 X& `) M& Y" G
}
5 T3 z; ~$ p' A ?' d void __fastcall Delete(int Index, bool bAutoDelete = true) $ K( n1 J/ [5 N7 ^8 O! j' ]7 H/ p
{
$ ]5 S# E% ?2 \9 S if(bAutoDelete) 0 I' g" T5 ]: Y1 e# F- Q7 c6 o
{ / O7 ~( B+ q; W2 P2 ?( O' }
delete Get(Index); 1 R" H7 h6 o) S$ z
}
' k4 b1 t" J/ j! M5 l inherited: elete(Index); : k( P( Q/ q0 W3 A- m/ G
} & a) L' C9 W; Z5 c8 W2 e, `! [2 N: x( E
T* __fastcall First(void) const
1 H% E: ^- M& Y. O! k& f { ( @# ^, O6 R& _& l1 @ ?2 _/ J- ]/ X9 [
return (T*)inherited::First();
& `$ B( d1 N Z' ~) t }
* g# H% M R) W+ O4 P int __fastcall IndexOf(T* Item) const + @! j, f- g5 L% I' z K
{
2 d* [( T* K; g) `& X2 R$ R. Y: _: c7 i" o return inherited::IndexOf(Item);
z7 o% s8 r4 n }
z6 Z n/ t9 ~7 o9 x$ C& W, J void __fastcall Insert(int Index, T* Item)
: g8 X3 ?( P7 ]8 c% b. \3 ] { 0 X F5 b) x+ m6 b
inherited::Insert(Index, Item);
8 V' L: y; E0 o# i& _5 M } + ^5 t+ p! F2 B% T8 j, O
bool __fastcall IsEmpty(void) const
5 R: K9 n; v+ J' \: R) C { ) {; ]! Z5 D" R2 j& _
return Count == 0;
9 A h1 f9 m }7 ?8 B8 ^6 `; {3 B } " I. y' S+ o. J4 ~- Z) i
T* __fastcall Last(void) const 3 [ \' M8 T; G% C5 X# j0 w, D
{ . q( p. U I# O
return (T*)inherited: ast();
! u8 q+ a1 C; s! _& K+ {, P } / \0 H& u, Q% S& R# H
int __fastcall Remove(T* Item, bool bAutoDelete = true)
s5 C: X( N4 m( k { ' ^, E0 |" T- l% W2 g
int nIndex = inherited::Remove(Item); * \& |* A# {, ^9 i0 {2 q# Y
if(bAutoDelete && (nIndex != -1))
9 u2 N" x2 V. N' Z# ~/ } { / r3 |) V, g+ @+ u2 c8 u
delete Item;
: Y9 V+ i( S3 ~ }
# }# C1 f F5 Y& f) L$ d return nIndex;
Y+ y/ A. S( Z4 z4 X } & H" A; O/ c+ j6 |' d1 I
void operator<<(T* Item) $ w4 Y3 Q% F3 n: f; g
{
: I- ^/ D1 m3 o( ^ Add((T *)Item); & H2 h5 N9 p5 |2 e. w+ |" p
}
; x S5 y% S, I( k7 w" N! g* z void operator<<(const TTypedList& List) 6 b/ C, G% R; x- Q$ n3 M
' i9 q9 S; y4 O/ d; @% s {
( o$ I1 s1 z7 {5 ]% B0 T' p1 I AddList(List);
, \4 U$ w( v. B6 k }
/ K9 N# j5 e6 q q* [* N}; // end of TTypedList
3 d0 z8 H% H5 r2 A& O: e//------------------------------------------------------------------------- 6 n. k3 d$ d+ Q0 \
#endif |