|
0 Q" Y6 r3 p1 R0 m1 |) `+ f: h* I偶在一个老外的文章的基础上写的,应该很有用的说, , P" l5 a2 G1 M4 U$ h
以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 : F0 b# \* t( c; Z8 @2 p
& b5 ^0 p/ F. K# o* f" x9 {# k( F//------------------------------------------------------------------------- + t* m: x" C6 F, i; N' C: H0 x" U
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
4 I( a" g8 D" C#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 n, D e P$ q8 n! `& b
//-------------------------------------------------------------------------
! v$ p- t5 H0 R$ L; X#include <classes.hpp> $ s' e3 {2 \( f3 }0 S- k0 w% {& w
//------------------------------------------------------------------------- # H8 ~# `/ u, ^. l
template <class T> & V, d$ H, Y0 { ~' J
class TTypedList : public TList * F8 k' p' T( U
{
$ t+ ]& w$ l$ z, l( b3 C$ _ typedef TList inherited; & T- W: o k0 C7 F o
private: 6 k0 p; ]0 S8 L/ d1 ~8 w
bool FAutoClear;
1 I/ J, G& _% |- I TClass FItemClassType; " b- F8 F0 c/ K: A K6 X
protected:
$ }6 {3 n- f, r! ?1 ?4 Y T* __fastcall Get(int Index) const ) P% ~: n& F/ W3 T
{ , b$ b, ^( F6 x8 g
return (T*)inherited::Get(Index); & q$ u1 ~. y$ w1 y! d2 I
} $ N, X( j( B8 ~* i
void __fastcall Put(int Index, T* Item) 3 } {& M5 r" @
{
& _7 A' P& x2 A inherited: ut(Index, Item);
% }6 J0 ^& _6 l } % h2 r. h6 u4 Y- f, R2 C) O
public:
) V. e5 {# }- }" T __property T* Items[int Index] = {read=Get, write=Put};
9 Q7 S4 C) L2 p8 V; H* V; tpublic:
" o; R- c$ D9 N m __fastcall TTypedList(bool bAutoClear = true)
7 }" E: l2 d5 ?$ E Y, B. ^" l : TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T)) " J: Z7 K, t$ D8 A8 ^+ g, p: S
{ }
6 z4 y4 u" W- b) r- i1 h // no destructor needed ; V# b* O' ?. S; n7 c
int __fastcall Add(T* Item) ! H; z# I' q" V% H
{ p& L0 S; y) X7 r/ E1 r. X( h
return inherited::Add(Item); + K H! u$ M l/ |
}
. }5 R/ r6 ^$ S. V int __fastcall AddList(const TTypedList& List) G' ^5 S, ] b' w ~
{ & T! W) k) C) E9 m
if(this == &List) : A& {6 h& K$ b) @- c
{ & \' B2 h8 [$ H6 h4 T
throw EInvalidPointer("Can't add a TTypedList to himself!"); 5 J( i8 Z+ U$ U- U- D
} $ E& j# G! c0 ?* F& _4 M& f+ n
if(FItemClassType != List.FItemClassType)
: Q. u/ C! j m* M {
5 ?4 `; V! Z" w% S; [. C* ?% v throw EConvertError("Can't add a TTypedList with a different ItemClass 8 |0 U3 x% Y! P& R' X
Type!");
]+ ~" U- ?! }! K1 Y6 } } & k; ]( i5 _9 _3 M
for(int i=0; i<List.Count; i++) $ a1 ]5 H% l8 \
{
Y) N7 L, W6 e Add(List.Get(i));
6 d2 i0 X$ u& R* [/ ]+ h }
7 K$ i V; C# ]6 u2 i return List.Count; 1 Y: V# r1 H$ o/ K. E# W2 }$ E
}
8 [$ Q! X5 {; `& n/ z$ g virtual void __fastcall Clear(void)
. n+ b+ X" O% ?' u- o. } H7 T { 8 p" o4 f- Q- J
if(FAutoClear)
' i8 a7 \ S- s# l9 j } { " [, m% ]4 s V( h* Q
for(int i=0; i<Count; i++)
U+ }( ^( E0 O { # Y9 r4 o( B" t0 O& }4 r! `5 b
delete Get(i);
* b2 |% ~: W# p1 N* X% v }
% T c0 W( c! r9 ~ } % g/ f1 T+ n9 ?7 t, n. Y6 ?
inherited::Clear(); 5 P7 v- v/ m# B0 ^& d. h
}
Q8 {& L$ T8 r2 R/ c void __fastcall Delete(int Index, bool bAutoDelete = true)
: ^5 `2 N- @; ?3 W8 m+ K { 3 F& p3 a5 Z9 M$ n( i: n0 Z
if(bAutoDelete) 5 L+ W: ?% l' I$ t5 S
{ 8 V2 }$ R+ V. u( j9 N6 @$ j/ f2 y
delete Get(Index);
8 s! z$ @$ I( ~ } 2 J2 S- f7 r3 e7 w q+ k
inherited: elete(Index); , n% r# @* e1 r1 k
}
. z0 u5 j( h# C6 b. x1 z5 m T* __fastcall First(void) const 6 g3 m3 B& ]: P1 W6 y2 o8 N
{ ^, j! i. |( e
return (T*)inherited::First(); 6 f- V3 J2 p: s4 i- X3 i8 U0 o
}
$ U5 g3 F! }* k) ] int __fastcall IndexOf(T* Item) const 7 u5 [5 h" b8 _ D
{
- m8 a- J$ w$ f( D return inherited::IndexOf(Item); * [1 ]$ r3 ^/ |* N' ^
} / F5 r; |' C6 z. o8 T
void __fastcall Insert(int Index, T* Item) # ^2 {: r$ t2 S6 T
{ 1 T: i9 ^0 N0 A
inherited::Insert(Index, Item);
& V5 d' f% \" J" l- W } ( @' h2 d! U3 {1 {
bool __fastcall IsEmpty(void) const
+ X+ E4 X3 s, R/ _* a) v1 l {
2 i5 E& B" F# W: z9 C7 h K return Count == 0;
: C O5 H3 {2 ~+ Q8 S } 9 I' J! c3 R, F2 k
T* __fastcall Last(void) const
% m7 ~' q3 l* `) } {
) A/ n v/ V* r( P2 ?6 u N1 y return (T*)inherited: ast();
) l; Q+ I1 Z D% R* A! ] } 6 a/ y. ?1 y9 F# [, i
int __fastcall Remove(T* Item, bool bAutoDelete = true) 4 s0 @9 M6 C, ]! |& F
{ ( a2 e- {% \: v- L
int nIndex = inherited::Remove(Item); : \ d+ l( U7 e% y+ S9 p9 x6 D
if(bAutoDelete && (nIndex != -1)) ( ]/ B- l* s/ a# p1 _7 Z/ b
{
' f! Z ?, h, U5 c v delete Item; , d. w H# n% n( m" P9 i/ U
}
* P# g. D h4 N5 H- A% O( E0 f return nIndex;
; d, x p+ T9 Y; P } / _8 W5 y# V$ }6 T1 h8 I
void operator<<(T* Item) ' b0 D7 ^% p7 X2 K/ l
{ ( G8 |6 S% c2 Z: X5 k) B3 {0 W
Add((T *)Item);
3 S, N5 S0 A# |; J }
$ k- \# S3 }9 B, i5 Y" R void operator<<(const TTypedList& List)
C( f. O( W" i+ L- j
5 o) T; t- ? N5 ~# d {
; T" [' n4 d/ e; a AddList(List); 6 r- A0 r/ r1 ~! i
}
: }) D3 U; o" H& h; R7 n: w& t}; // end of TTypedList ( G$ D# K1 _( p; ? P: M. g: m
//------------------------------------------------------------------------- ) Y9 K" N9 t2 L( D
#endif |