|
8 b- T( k# ]" |" j
偶在一个老外的文章的基础上写的,应该很有用的说, ' u& {! x" }3 o6 \7 W* j
以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 ; w, }8 ?% ] E0 F' z
3 q" m& K4 d4 Y5 [3 h! @: K# m//------------------------------------------------------------------------- 0 l/ t, d/ j6 t [+ _; d/ R/ K
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 8 m# s# u! q3 X4 w+ \' w
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
! i+ H0 L/ M4 s! m* k5 I# ?. M; D//-------------------------------------------------------------------------
3 } D1 G4 n% M+ A6 F" g/ o#include <classes.hpp>
0 g$ W7 b7 L; p9 u( w//------------------------------------------------------------------------- ! i% G' w" h/ T5 R( |. c4 @
template <class T>
: Z2 s) U5 j4 i2 b* y, k$ Cclass TTypedList : public TList
5 P* o+ i% p! {7 D: S2 a{
( B C6 ?* ?/ ] \$ A( K" R typedef TList inherited;
5 }( ^- P) c5 Z" Fprivate: ! p( i! k# e" F7 Q! Y5 r
bool FAutoClear; 6 \* _1 b* O1 J* \: V: n
TClass FItemClassType;
4 a- |. `2 O8 t: Gprotected:
) T% x1 h8 m- _( O2 O T* __fastcall Get(int Index) const
. I. u- U+ R3 ?: m {
; I) k1 S7 l* M$ }/ F. }! ^ return (T*)inherited::Get(Index);
9 T3 |5 @9 i- O, ~ }
: ?& B0 g# h$ a8 K7 T' t void __fastcall Put(int Index, T* Item) 1 d2 n4 k+ E3 N- G6 f3 t3 p
{
, s1 d, G& E% E inherited: ut(Index, Item); / m' F! N, B g; y0 u) Q! y
}
: a' Q [+ ^+ i0 j. q; Lpublic: 4 G1 r) P+ ]0 ~& d; u( q( M
__property T* Items[int Index] = {read=Get, write=Put}; * a: P" t! N1 A4 J. D
public:
! p; e# v; M. S( g n T/ ~3 } __fastcall TTypedList(bool bAutoClear = true)
2 s8 \: H8 u* H* ], i4 j" ~1 n. | : TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T)) 4 r3 u- r0 n2 O% D) a
{ } % k$ E9 l( \; F% A; _
// no destructor needed
2 x# y7 e$ S5 s" t3 D) H int __fastcall Add(T* Item)
+ |+ G( d8 ~# c9 g- q7 o {
) p( L; n8 f. J9 i7 W, ` return inherited::Add(Item);
8 `4 Y4 I* b! J' ^- C% Y, j" L }
- C, x! {, q. h% Z int __fastcall AddList(const TTypedList& List)
# m% c# i: s( ?- N1 V {
6 v) D# `% n7 `5 u R1 J if(this == &List)
' k5 W+ `8 q ^( I { ; L5 E& i, C) r, {: |9 R: ~$ x
throw EInvalidPointer("Can't add a TTypedList to himself!"); & [9 e5 h, d. p" k9 X9 t
}
2 r8 K z* G, `! e8 B' b if(FItemClassType != List.FItemClassType) . a* e: H/ c1 X! O" q8 p2 X! D
{
2 A/ y2 K) I" t: ]# E throw EConvertError("Can't add a TTypedList with a different ItemClass ) O4 b+ U; i* T% Z6 \' I J
Type!");
& v6 E( n8 Y- T7 p" ~. j }
T9 P! m: R) i1 a for(int i=0; i<List.Count; i++) w- T% N4 @& ?4 i4 I' O$ y- G7 m
{
8 K9 d" G* D/ E Add(List.Get(i)); . Y4 R$ v! p3 r- L+ Q9 J
}
3 g1 W, q$ y4 o; _1 Z' R return List.Count;
6 }& {* P0 u/ w% Q }
& e* j/ {/ B. o/ U) m virtual void __fastcall Clear(void) % X4 m9 l' W# t8 v7 `2 G" _
{
; X2 g# m- A+ _4 L% P! l) Q4 S if(FAutoClear) ' I0 I" P9 s1 T- _/ v
{ ! r0 D4 V) P1 e* ^
for(int i=0; i<Count; i++) ' |+ F0 [6 @/ l2 f
{ 0 X# o4 R+ P" W4 Z9 x5 v
delete Get(i);
3 A" Z- n1 K5 T5 ? S }
6 ^ u4 _3 ^ N }
! o# x, R3 G, I inherited::Clear(); 6 h; G0 v! B6 v# z: E
} : N$ E1 t) S$ @0 |9 p% r& m
void __fastcall Delete(int Index, bool bAutoDelete = true)
+ m y6 }# ~- i/ v, ~7 L {
6 A% p2 H( P0 ]4 J# g& g if(bAutoDelete) 8 g, L* u, N1 w5 r4 N+ F
{
7 I6 s8 d/ U; j3 f delete Get(Index); ! p& w( N' ^6 y' d
}
$ K5 A, k0 t k6 o8 u* A: f inherited: elete(Index);
' [5 S' P) E) A. L6 [ } 0 s, S+ e. _/ D
T* __fastcall First(void) const
3 X* v* X5 |1 {' ^" |5 k {
7 V5 [8 k' R+ u( V% D return (T*)inherited::First(); ' D+ Q& R, H o4 l
} 8 }) K& @( c9 L" J( W
int __fastcall IndexOf(T* Item) const
! X* l- W+ C3 |) k* r) g$ g5 [ {
* j; M0 l1 h4 d) b7 `% x return inherited::IndexOf(Item);
: `. U. D& E) [: W }
; K) r- D, h) p( o, t3 L9 s! r void __fastcall Insert(int Index, T* Item)
# n" W& q" k' O' | { : \/ E" O- l* V% B% H6 L7 H
inherited::Insert(Index, Item); - P- @6 X" C6 j5 P6 D7 W9 b% i5 L
} ; _7 F& W2 v- f6 S( Y
bool __fastcall IsEmpty(void) const
N! O# H R- l6 S- L3 F { " o5 f0 m/ I: Z1 S3 T+ D1 D
return Count == 0; : `0 W/ R& L r, d5 M* |
} 6 s6 M8 `$ u. C( f
T* __fastcall Last(void) const ) Y4 M# `2 R2 D$ }3 }: y
{
) t& D+ I/ h+ U4 x return (T*)inherited: ast();
$ W6 b( L- D. M. S5 y% L6 G$ s/ h+ C }
9 g6 M" U8 j) L# n) L/ Q4 u int __fastcall Remove(T* Item, bool bAutoDelete = true)
' Z2 g+ I6 {' i; c k+ \7 b {
% h6 E2 ~! M' v, |& l int nIndex = inherited::Remove(Item); 7 @' V- p- L h& y4 J6 l
if(bAutoDelete && (nIndex != -1))
0 E4 W! j7 q- e9 l+ [( L+ ~ {
6 \3 a6 S$ J9 G! T delete Item;
3 Z/ Q# V% r' o. U1 X }
' v1 H( y% W2 K/ R+ ? return nIndex; _! A% k( {* h- Q* j0 j! K
}
7 x# A7 O5 u4 Y' T5 K void operator<<(T* Item) & {, L, L2 k; a/ D5 f$ y. L
{
- U2 o( q9 J* y3 G) F1 Y- S2 H k Add((T *)Item);
& `$ H* v& U: S& a" Q }
1 X" E1 s" K8 i void operator<<(const TTypedList& List) & S1 B1 g& J2 }! m( @ U
- m7 q$ w+ r; M( j! v
{
5 @) v4 r3 }: D( c AddList(List); 5 m" K) p: S7 I7 F
} 8 p$ v/ R& O! |3 f4 p. A4 I
}; // end of TTypedList
9 f/ K* ^! N6 ^$ w3 X$ p//-------------------------------------------------------------------------
5 q. S5 ?% G5 D/ }# ^2 T; A) k o+ F#endif |