|
: g2 H- I) r# x+ m3 x" j L- e7 D, x# E偶在一个老外的文章的基础上写的,应该很有用的说,
8 C% _" ` j4 n" H- |* |( [/ K- Y以前总是用STL的vector做替代,但是STL的用法实在不是很习惯 : Z- [, K8 h# L- d7 K9 n
# D- Y: ^2 Q' d' f//------------------------------------------------------------------------- , V* ?7 H2 a3 @) V) G
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4 2 ]4 J: V2 A3 z8 k8 T+ h4 o
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
: ?0 {' r* u- J- M: {0 N//-------------------------------------------------------------------------
2 h, B+ W9 ^- e1 {5 ]; |#include <classes.hpp>
$ s2 v' ~& \& [; `0 I4 d//-------------------------------------------------------------------------
+ _9 p$ z: c/ A; H4 F+ B% t1 ntemplate <class T> 0 h4 u- x- M h# W, z; p+ z
class TTypedList : public TList 0 a0 t2 k b4 w, ]2 T D
{ 3 h4 p( Q9 I5 y c
typedef TList inherited;
. T' i; h6 V+ m3 j5 Uprivate:
- _9 k+ h2 V- z0 K4 W9 g bool FAutoClear; ( \; @( R9 r1 {# b! \* e- G
TClass FItemClassType;
# p6 J* B0 a0 C) a6 Q0 e- i) ~7 \9 aprotected: e$ k m$ `0 M
T* __fastcall Get(int Index) const
3 Y5 C1 [, d8 g* P! n4 a, R9 Z/ ^ {
}$ h _. g5 u) t7 e9 {( x return (T*)inherited::Get(Index); - N0 f7 \# L5 x0 n; i% J
}
% o# `- |& }( \7 h void __fastcall Put(int Index, T* Item)
: g) ~, [/ i, G" l: q. I( O {
8 R9 C$ s1 f- U inherited: ut(Index, Item);
# i- g8 Y3 h% H }
`# d2 r5 ?, mpublic: / Q; z( a2 F3 z6 e+ S* n
__property T* Items[int Index] = {read=Get, write=Put};
0 U7 a8 o% K: f3 h) A6 i% K0 Cpublic:
0 Q/ }" M( C: G' T7 o: ~ __fastcall TTypedList(bool bAutoClear = true) 9 t* U7 z# y* z/ i: R
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T)) + p+ @7 c* i) v, b$ W
{ }
: n% Z4 W5 S: J' f // no destructor needed + r8 G; u. G: h, B2 z) P
int __fastcall Add(T* Item) 9 D; F3 ~( ^6 \4 c! l) I
{ . B- Z6 a/ p$ Z: ~
return inherited::Add(Item); 0 x' U. w' T0 c6 q) T5 q: ~
} $ x" _ ]7 r$ k# Y7 u S) q1 @
int __fastcall AddList(const TTypedList& List) $ a7 F/ j6 ?7 W
{
' D9 k* t4 w9 ?; u* ~ if(this == &List)
( H$ T+ b5 n1 U% t; @ I { - H/ I/ J" ]. V: g! |5 ?
throw EInvalidPointer("Can't add a TTypedList to himself!"); 8 x2 l5 r8 Z7 h2 {0 P
} ; h& O3 t9 [- H3 @5 c! n
if(FItemClassType != List.FItemClassType)
9 P# _* x- D. t. L! c7 |% j { / l, h8 r* W" b
throw EConvertError("Can't add a TTypedList with a different ItemClass - ^- p# F' x/ H# t1 b+ P: \
Type!"); ! t s6 M" V8 u! j
}
8 v* D4 g4 m, \( \ for(int i=0; i<List.Count; i++) 6 n, l1 f! Q, q$ y; y
{
1 @& S( e' n$ v. G Add(List.Get(i));
. W1 [2 S( G% c1 H6 b0 s6 W1 Y9 e }
8 a* k; R6 }% D! n& ~ return List.Count; 4 U7 l2 y& d8 z( E# I
} * H1 k1 w( m: A+ V" R1 d- N" F
virtual void __fastcall Clear(void) 7 l. C2 L/ ?+ S' }& c( b! {+ W
{
9 g9 @2 C4 N* z9 ?1 y if(FAutoClear) - k" z, q. O% W9 H
{
# Q7 j& _# \+ G for(int i=0; i<Count; i++) ) C9 {0 Y# t$ u4 S( U w3 Q
{ 7 P, g( M0 a/ r5 [2 }
delete Get(i); 9 C) i$ c- p5 w$ T ]
}
* c0 @0 l7 m( Y S& K }
' c" \. j* |9 O% c; ^4 X! c* g inherited::Clear();
! K) n/ K. r( y" I& ] }
: s7 u- F5 A0 e8 t- X9 |; t void __fastcall Delete(int Index, bool bAutoDelete = true) 7 u! E" b6 ]! {9 R0 A
{
9 [; c$ }( I% `, L2 [! X1 ?% Y0 l if(bAutoDelete)
$ a; o; y" t, }( N" D2 ^4 d { 7 _5 V; z8 U1 i, F. ]( F5 B# Y7 k
delete Get(Index);
, C) K3 \" p* ]4 j }
) _9 |! o* G8 n# G inherited: elete(Index);
8 _9 i& x* N& {! { } / O- p) k5 o& J H; G3 K1 O
T* __fastcall First(void) const
. J+ S3 H0 R. h% N$ n7 k { " g {0 V1 l& }3 u
return (T*)inherited::First(); : ~+ M6 n; b$ W' ~
}
# L$ c9 p3 r; D. T$ X5 Z int __fastcall IndexOf(T* Item) const
: q4 h3 u8 o1 w* A {
0 O% q9 A4 I' a9 v: q return inherited::IndexOf(Item); 9 U6 g" d Q {/ U$ L6 } d
}
* a* }/ Q8 s- K( Q void __fastcall Insert(int Index, T* Item)
/ v4 H ]" u4 w6 t { 9 x( E) x! j8 k* h) X9 i
inherited::Insert(Index, Item); ; f$ b2 F' D' }# x7 S
} 5 o' n# u* T: \4 u' N) g. d, U) U
bool __fastcall IsEmpty(void) const ( a" ~: O+ w, E
{
4 i( s/ y h- r( L& f return Count == 0; ! v$ m2 f) C0 C; _& [4 b
}
& J3 o1 y) T. W; i T, k. V T* __fastcall Last(void) const
7 T- c V+ Y- @- Z0 Z, q4 p {
! H, R: ?, D l' X return (T*)inherited: ast(); p5 t: b1 `" o* M1 h$ k+ U
}
; C& b; z0 ~! F- Q int __fastcall Remove(T* Item, bool bAutoDelete = true)
+ i0 B/ Z4 ` W8 L0 J1 N2 J0 c6 t {
3 \, `5 t! {8 c8 I8 j' f# R7 G int nIndex = inherited::Remove(Item); & J: q# t3 s: X7 |
if(bAutoDelete && (nIndex != -1))
3 K$ w9 z5 V" }3 o2 x {
. w5 b- |+ f( D0 H6 p2 [ delete Item; & L% I/ w) C: Q) m
} 9 q; i' X* i I7 x
return nIndex;
9 _+ i: Q m6 q' W1 ?# f } 7 _& W3 A+ F0 u+ ?9 z
void operator<<(T* Item)
8 D7 Y8 `5 d4 ^2 A {
, s0 P4 h/ z+ s6 a7 _ Add((T *)Item);
3 r" b1 m/ ?$ |# e6 d) k) ? } " A% |4 Y1 h$ p$ ]* Z% @" M
void operator<<(const TTypedList& List)
/ ]+ s8 `+ G: Y+ [6 Z7 A: A; p# n. k$ X: k( ?" D: H& @' M. m* k
{ - Y8 u G% A9 C j4 @
AddList(List); $ U/ F0 r3 S# s \% B0 }) {' s
} ; X, |, d! m1 |6 z$ F3 o) x9 {4 O
}; // end of TTypedList
! ]2 ~$ s/ b2 @//-------------------------------------------------------------------------
% s3 l, K5 q9 ~* X, @( v6 i3 Y. ~#endif |