|
5 [3 t. j" I0 \' u* ?$ w) V. T; [偶在一个老外的文章的基础上写的,应该很有用的说,
: U6 F; Y. T' P7 n: ?4 h6 F' k以前总是用STL的vector做替代,但是STL的用法实在不是很习惯
/ N E* ?8 {- v; [
" J' Z2 S7 T1 i//------------------------------------------------------------------------- & G5 K% Y/ Z3 `$ z+ c! c' b
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 , k. n" K! a* R5 n4 g4 u# D; [( h4 t
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 0 @ J5 [( Z8 E B
//------------------------------------------------------------------------- 9 K) ]. g! S, O3 Z& | c F
#include <classes.hpp> 3 P; Y5 ?0 x+ T+ D ^5 M
//-------------------------------------------------------------------------
5 S+ ?; ~2 ]- htemplate <class T>
7 v" T8 T7 ^3 W# O! Wclass TTypedList : public TList j" G- I, Z1 N9 ]2 y
{ - f) W! d& D/ m7 B7 T0 u5 t3 ]% v
typedef TList inherited;
, ?/ f% b/ h" R- z0 |private: . {. b% z; l7 b) r! M
bool FAutoClear;
: R. B3 y: f! r2 `: I/ V% j: k TClass FItemClassType;
. ~$ v+ y3 B) p2 g1 K. t$ c+ @protected:
; q* u! ]* \9 L5 X T* __fastcall Get(int Index) const ' e+ F6 y S4 t( m3 k3 H
{
4 U5 z- R$ c- V0 Q' S0 }* C return (T*)inherited::Get(Index); % D; t C, t+ C2 _% ?* e
}
- g4 x# o7 \& f) U. b2 S% [- m void __fastcall Put(int Index, T* Item)
6 i+ f+ `) A) } {
1 c% V7 h" j" {% H x inherited: ut(Index, Item);
% ~( B0 L- _7 W# m: C% C }
) |' J' O( G6 F) S7 q9 P& }public:
8 ]- O! `* I9 g1 {; U3 i' J __property T* Items[int Index] = {read=Get, write=Put}; " j7 ^* D7 A( D2 K' G6 O
public:
! s$ X; E* ?7 U! P+ S1 |/ y __fastcall TTypedList(bool bAutoClear = true) , e9 O9 ^* t' O$ s2 _
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
* k) {6 R% O' L# D5 @5 I, K8 d { } & y! d( r& f' }' H2 U, e$ t
// no destructor needed 0 K, }4 `" Z- n4 ?" }$ h- _+ d
int __fastcall Add(T* Item) 4 r3 q, ?* U7 G& Q* \ X
{ , s" y$ ?, a3 j0 e C% Z9 [) Q* I6 o/ c
return inherited::Add(Item);
* ?9 ]. N: S9 z* @2 J! |4 A }
5 I7 E0 M9 b) y! ^ int __fastcall AddList(const TTypedList& List) 8 g* A7 s9 j6 B4 j
{
! w7 U. l6 z/ N& T( h9 ] if(this == &List) 1 v+ A. N, P1 z! H/ n
{
H6 |: i4 v: |0 q& \0 U" ? throw EInvalidPointer("Can't add a TTypedList to himself!");
+ Q' u/ C5 X9 ?* ~( X9 q }
" w p* \7 d b if(FItemClassType != List.FItemClassType)
7 {9 Z5 M4 o1 H* A1 _/ q {
. q( U/ G. F3 ]2 x3 y j/ E) { throw EConvertError("Can't add a TTypedList with a different ItemClass ! v+ s5 F( c- J3 ~9 _0 s
Type!"); " Q) m9 N. k8 X( i! }( b9 F9 t9 r% N
}
5 x" h1 l: R8 T& v; m1 H for(int i=0; i<List.Count; i++)
: e! U x3 v3 w, X {
8 b8 l J/ |, f. `8 v7 w/ E Add(List.Get(i)); 2 b! ?: E1 m# |. S6 Z
} 2 S9 s9 a3 D0 y' E
return List.Count; 5 K+ J4 i! x& q6 u% L8 p" _
}
& Q9 Q/ {5 @0 q# B virtual void __fastcall Clear(void)
- P( M5 _6 C. l( Q) y {
' R9 v9 t9 C( ?; N% { if(FAutoClear)
) T z* \3 @8 W; L. M% ^ { " t" ?( n9 Q; T
for(int i=0; i<Count; i++)
7 b" C. {! x1 Z+ C1 j {
& x8 v% ?0 a5 ~) w! X" p delete Get(i);
' W( k) U& o% J- B! ^ }
! _. c* D5 o: u; G3 A! E }
- n1 ?/ n6 H3 l+ }+ Y) l- g inherited::Clear(); 8 f6 m% Y* L O _5 B
}
* A1 d% f- ]: X% a; `- w) T void __fastcall Delete(int Index, bool bAutoDelete = true)
% O N8 h( F& I( J { ) u9 m3 C0 I+ \( w. K
if(bAutoDelete)
5 t. {' h5 G) l/ D6 r {
( _5 v3 G$ d4 y4 q4 `* H, o! E delete Get(Index);
: {6 X, @8 y; h/ @6 v0 ` } % `1 X9 c+ s4 _8 s" E# h8 `
inherited: elete(Index);
6 b I- b5 j/ l/ O }
: Q1 X; I. S+ S" G# X7 I T* __fastcall First(void) const
+ W, L- P0 R' f) t { " L3 N6 L8 d( K" @7 b! N
return (T*)inherited::First(); " ~$ e/ ~; \7 V
} + Y( h0 R' O+ Y) ~( ]3 I2 s6 [
int __fastcall IndexOf(T* Item) const 4 H& r2 ]1 q5 ^' d' i& J
{ 4 ?! _% R( E) K- N, R* g# n
return inherited::IndexOf(Item); 4 `, f0 c; }% q# ~
} # p3 F; e4 N/ V' p4 _0 ^" L- ?$ Q
void __fastcall Insert(int Index, T* Item)
6 J! r: E# t( x) x l/ J4 ~ { ! ^: {9 I+ X; T- P% L
inherited::Insert(Index, Item);
( |% I% |+ c5 P5 i% G, w } 1 `1 o7 z. m, b) M7 B/ j
bool __fastcall IsEmpty(void) const , b* C3 @/ \( D4 [0 I' x( F; o5 ~; M
{
8 R# r, P/ k. G) P* Q, q1 W return Count == 0; / m' R( w/ A3 L) M3 x1 e
} N: U3 ^, O& y: d+ c
T* __fastcall Last(void) const ! W7 o, N8 _! e+ ~, M
{
- \% w% J6 S7 U& y return (T*)inherited: ast();
, j0 G. z; e+ E9 G: g' x3 t } 2 [. C/ N& i9 e( J
int __fastcall Remove(T* Item, bool bAutoDelete = true) - E9 \$ Z+ u6 _) z2 b* }
{
6 e' ^4 ]1 p( } B3 e& L int nIndex = inherited::Remove(Item); / x" i7 p. j" U" F3 v" h: i2 _
if(bAutoDelete && (nIndex != -1)) , c- Y( L- v$ s* ?4 O
{ " R0 `1 N5 P; s P4 f" q2 o
delete Item; # T: h" O- Z L
}
0 I) m- E m% o2 b) Y' b8 T return nIndex; ' b/ o, q' Y; U
}
8 W) i' R$ s1 P/ c# x- N D void operator<<(T* Item)
/ a, b% z. c# |2 M8 O { + j% l/ S: K, r, g0 C; B4 Y8 F
Add((T *)Item);
. _7 X0 [( j, x F$ T7 ^ }
# ~6 ~9 |& x9 v' j/ ^! ]+ R- {0 s- J void operator<<(const TTypedList& List) * e: b/ e( e( H d! h* V
) F5 l" J5 i) H! l! Y) k! R4 f { ; c( [- t" c" H9 J6 _$ u5 |
AddList(List);
, A) m+ _/ f2 j+ D# L/ h, K. K# S } $ z+ y/ c# y. W% N, H$ k2 m' d9 x
}; // end of TTypedList + z+ ?" s0 {) q% ^, c0 K
//-------------------------------------------------------------------------
- P$ Q4 r4 m3 M7 ]' T" Z6 C#endif |