数学建模社区-数学中国

标题: TTypedList一个类型安全的TList模板类 [打印本页]

作者: 韩冰    时间: 2005-1-26 01:11
标题: TTypedList一个类型安全的TList模板类

X% ?/ w6 P1 K/ O! B2 F

偶在一个老外的文章的基础上写的,应该很有用的说,

/ G! v4 w, p2 @

以前总是用STL的vector做替代,但是STL的用法实在不是很习惯

* `# m6 t* f$ K$ U) U }

- v. G, I9 y2 t6 I& T! \& C

//-------------------------------------------------------------------------

/ c: z f, o% ^8 u/ T* G4 a& e/ d

#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4

0 ~8 h9 ?6 W+ c

#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4

' q; G6 L" F3 Z/ _) E4 m6 m: d

//-------------------------------------------------------------------------

! f- G6 b' F5 |5 |

#include <classes.hpp>

- I/ Z7 n( `9 r8 p+ |

//-------------------------------------------------------------------------

8 h7 U( v( g9 O; _4 W7 ]

template <class T>

/ Y# j( o7 q$ r

class TTypedList : public TList

. l+ l# \# y d- _1 p' D& p2 i

{

$ q. N5 s5 I9 [0 a$ O3 v

typedef TList inherited;

0 I# i6 w6 a% o3 |2 R

private:

8 d5 g" Q3 [" l. E/ B

bool FAutoClear;

3 c7 M0 ~6 [# h$ f& Y

TClass FItemClassType;

" b1 L$ z+ s' L

protected:

; P+ r/ `# A& [, A1 e: h+ v

T* __fastcall Get(int Index) const

# o5 Z, \) E1 O$ Z0 d* X

{

; r/ Q- l% r* w+ T1 I

return (T*)inherited::Get(Index);

) m* Z4 V a- I% t0 r

}

: z q0 b/ t% o, k; u

void __fastcall Put(int Index, T* Item)

+ M. S" C2 q; ]4 x- Q w6 g/ ]; ]; |

{

1 }! X6 t7 [+ r$ K3 {

inherited:ut(Index, Item);

# i$ S6 I! ]% u6 r4 x2 V

}

3 D* o1 P# X& _# }# I- ]

public:

( b9 [2 P; O0 [4 a/ Y2 Q

__property T* Items[int Index] = {read=Get, write=Put};

# |3 \, @( e( d/ N7 {: {9 N* [

public:

! [9 r9 o! R2 ]

__fastcall TTypedList(bool bAutoClear = true)

2 b8 \5 x6 Q% s0 ~5 N1 l- _

: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))

B2 `1 ~- z3 m( q7 h

{ }

C; e! k! s0 z$ J7 n

// no destructor needed

. R4 _3 H% F* ? r: o. r

int __fastcall Add(T* Item)

- I$ I$ O+ E2 N+ V- z6 t3 Z

{

g5 V/ C5 Z+ N" g- Q6 L4 V+ q. \

return inherited::Add(Item);

$ { y1 l0 N7 b$ x& x

}

, q+ Y+ Y/ Z4 B+ K; M

int __fastcall AddList(const TTypedList& List)

6 B1 }- o( V8 g* h& [

{

. j$ ^. Z# @4 @4 |2 L5 J* y

if(this == &List)

4 f' d+ Q: A( V) i- T

{

" V. p0 j$ |/ w6 d. }" U

throw EInvalidPointer("Can't add a TTypedList to himself!");

/ G! U7 v' D. w6 ]" ^5 t2 ~1 Q7 R/ ]

}

4 D' u0 D" j/ |: Y( W0 e9 T4 z

if(FItemClassType != List.FItemClassType)

+ J. A" }3 i- I3 [

{

: ^& [4 X( ~6 r, x: ^% p' N

throw EConvertError("Can't add a TTypedList with a different ItemClass

3 @% }& u" S% i1 H

Type!");

7 W0 u4 E- M$ `/ X* M7 l

}

B/ U$ t1 |) X* c7 X

for(int i=0; i<List.Count; i++)

& b( I$ u/ K1 C' S6 Z4 V

{

$ Z+ h! \6 k; N" x) t# U4 |

Add(List.Get(i));

$ K: g6 x) H7 {% u/ G& H/ q

}

6 \+ u& i5 I. D: L. h+ D/ @& C

return List.Count;

' L0 P, ~# q v9 ]- W

}

" g' d" q6 J7 e: N# l$ U& `. V

virtual void __fastcall Clear(void)

, F3 b8 K* K q) @1 z3 m6 F0 {

{

1 Q h& \0 y& i! B3 c3 t, D

if(FAutoClear)

( n; T4 Q. Y7 J2 g2 S7 U

{

' m9 R; L8 x3 B- Z8 v: E, [

for(int i=0; i<Count; i++)

1 [# t( \# j" ]3 ~

{

! i0 ]/ j6 C/ L/ F

delete Get(i);

. a) x' J- j, t: l

}

, I1 C) y1 X* I" ^% L8 n

}

3 |# r( M$ P" z

inherited::Clear();

7 t" X2 I' _/ y6 O

}

1 e# i$ C+ t2 X, ^+ P. u& U0 t

void __fastcall Delete(int Index, bool bAutoDelete = true)

1 X# H. K4 G& U* G9 R

{

* m" V2 g, |) [) X- {

if(bAutoDelete)

$ ]% S7 c; T6 j

{

. p5 K/ }& \& S1 i1 W

delete Get(Index);

+ g: X; ]: r6 e8 A# C# f

}

# l* } d9 y' _3 m6 I( N

inherited:elete(Index);

/ D1 K& d7 {1 n. a% N' G

}

K# b4 y0 _' R6 W2 W+ p% _

T* __fastcall First(void) const

0 U9 b9 B/ c% t- B7 t+ z0 |* W& \% h

{

) ?) K" K3 q2 I" [, S2 O5 N: j

return (T*)inherited::First();

0 y6 w2 Q" v$ W i! P, D

}

! K. b6 @1 Q) |5 H

int __fastcall IndexOf(T* Item) const

- ?& Q k" r9 p* a) q1 \8 i" b

{

7 ]* K, y) _2 {9 A7 F! \

return inherited::IndexOf(Item);

, A; o+ K2 ~( ?

}

& p W2 r% f; _( T; z; t; r7 V, F

void __fastcall Insert(int Index, T* Item)

, n# z3 P0 ^# y/ I; p+ h+ z+ V

{

7 v; q! G: L2 k* w& D5 I

inherited::Insert(Index, Item);

4 _& r& I) K0 W0 ^& D

}

8 L1 l5 w! Z' g; I; u- P

bool __fastcall IsEmpty(void) const

2 f9 i' \ k0 C! f: G8 q

{

/ y5 J- ^2 f# o

return Count == 0;

# m, t4 T T" N$ k4 p5 g

}

8 }6 X" r! s) P$ q

T* __fastcall Last(void) const

, E7 ^2 ]) d' |) r

{

, X/ w7 u- \( f L) \

return (T*)inherited:ast();

* z% u8 l4 f" H; }

}

0 j5 s L+ I a; Y* ]8 I

int __fastcall Remove(T* Item, bool bAutoDelete = true)

4 p, [# }) e) N' e. q! [+ o

{

: F+ v' f2 m' h2 n$ ~

int nIndex = inherited::Remove(Item);

: z; }. i& g) F' e5 x! i; j

if(bAutoDelete && (nIndex != -1))

' M$ V, B5 j- i! e( \! S6 x" P

{

, t+ P$ u3 T* P0 C: ~

delete Item;

1 v" L$ {7 t$ F" K- ?/ @4 E

}

- `8 ]) N+ v* ]9 C/ {

return nIndex;

' |; Z* A/ f- J

}

$ R+ z$ F) Z) M8 i% x

void operator<<(T* Item)

, g5 s8 d+ q. k$ U' y8 R

{

% Y7 ]- ?4 B: {3 u

Add((T *)Item);

# H2 r! f' }2 a* m: s# v$ c

}

8 d" k f( B9 K5 m; A

void operator<<(const TTypedList& List)

' M* B# A) L2 j! Z; ~" b& ^/ C$ i9 t+ Q- t+ s9 g$ s

{

- b# z2 g8 v0 r# q3 q1 ?( O

AddList(List);

6 f, U, e0 t, ~6 h, V

}

: W& y$ R! a. q0 }% x

}; // end of TTypedList

) B/ U! X4 n. L, X! M t5 U

//-------------------------------------------------------------------------

8 b) W7 S- T5 _) j& @ q

#endif






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5