数学建模社区-数学中国

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

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

* `: {5 X7 R3 j' f0 Q; O

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

6 K6 ?( _ A* s# p3 S

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

; U) K7 w+ f3 ~5 M0 q6 c

4 x6 u, I. f5 K9 u! x( u

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

' d5 @" t, ` p( m3 g. y: b

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

* V V& Q# D, C7 C I D8 t+ t( S

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

: U+ ~1 v* Z/ v

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

6 s( t, I# V( Q4 |% X0 |+ ~4 U

#include <classes.hpp>

1 b6 r% g' H7 ]; d2 g

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

6 a" f. N# ?. D+ Z4 S" x, M

template <class T>

9 O2 K, q% T( V; @1 u/ s: ]

class TTypedList : public TList

1 Z/ ]7 Y( Z9 I% q2 A0 h

{

+ T: G" `& d6 I

typedef TList inherited;

( Y' W1 l3 U2 @ n3 k' a

private:

1 }1 E; A. E! d; n" ]5 k5 T o+ f

bool FAutoClear;

1 u, l8 Q6 b2 t

TClass FItemClassType;

2 B/ H& S' w5 f3 L: c* g: m: V

protected:

& k/ @9 B9 V' ?, E

T* __fastcall Get(int Index) const

H5 a+ N5 ]- w4 S1 ~2 `

{

' `) {5 D8 f( R* E

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

- n& Q5 j" A2 Z: ~$ i3 Z

}

t2 I( V' @6 r: n: [

void __fastcall Put(int Index, T* Item)

9 m! c$ _' B/ L# Q6 e) _5 Q" U

{

( |6 i& [0 o, o: O% y

inherited:ut(Index, Item);

% Q+ R% ?1 H: r: @' C% t

}

8 E7 J+ z9 o' D* A% B& O& K

public:

; \7 ^) X- T9 f- m& w

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

" Z3 Z1 [/ K O

public:

! D( r% u* [: M# G! @" V$ M

__fastcall TTypedList(bool bAutoClear = true)

1 E) `- T6 }& e- w+ G5 n

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

, W+ |6 I$ l3 X( X% [- l

{ }

: N- U) Y! U! R) K+ Z

// no destructor needed

: s# t; n& V1 j- e

int __fastcall Add(T* Item)

; f9 n. u/ G0 ^; \4 q

{

u& J) [, L* b# c3 D) M

return inherited::Add(Item);

% y* N' n2 {. J

}

1 I& P/ ?/ ^1 x

int __fastcall AddList(const TTypedList& List)

b- @/ N# A. V. \ ~1 n

{

! V+ ?2 Y6 N% U8 i* t' a

if(this == &List)

6 B l7 E" W" J0 ~: e1 n; L0 z+ I C

{

8 M; A/ m7 k0 \ h' _

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

) K* p! h: l* m9 n

}

, g5 \* n, H$ I; [! N

if(FItemClassType != List.FItemClassType)

5 D0 R6 ], c( I F; g

{

8 d8 [. U0 B+ i" x/ p+ T/ |

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

, ~! O% ~: y; f

Type!");

- q! o7 Y8 q- ~$ x

}

* S7 `7 f( Y* ^* ~2 G

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

/ j$ @( ]3 @5 z+ |: g

{

$ C- v7 q9 w9 p! L c# X5 k

Add(List.Get(i));

! Y* [4 |: ?: {7 q) {

}

7 g4 a/ E' s3 k- R! I' _' D

return List.Count;

# ]* a9 P) u, ]

}

B" n* l; e+ v9 @# p5 ^. \1 }

virtual void __fastcall Clear(void)

" K: D; y, m! c" ]

{

7 B: ]* P' Q/ s+ u; d

if(FAutoClear)

4 R, l/ T; l$ A1 B5 F

{

" Z7 D3 e1 d+ r- A

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

/ m! @6 c9 v7 y6 G# W: W1 c

{

' |3 s+ x- _: d4 S8 c

delete Get(i);

1 B' k' T6 q" d+ S; K1 b

}

$ m# P6 s" A0 x3 [7 f# t- u V

}

4 J0 s6 }! k* c

inherited::Clear();

* d4 b+ u5 H( j" f! E. H4 p

}

, I6 }" X2 {1 u. R( \6 W, A* @: M; G! y

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

, X6 a3 k) s4 h

{

5 M9 q8 G0 g+ K: Q

if(bAutoDelete)

8 E3 s5 N5 v0 d" q6 f: \1 O/ l

{

2 `3 e- J' B, k7 i* D( ]7 l

delete Get(Index);

4 W0 d9 Q( i9 M6 K

}

$ ^5 J3 |4 C7 `1 `

inherited:elete(Index);

3 M# U3 _. H5 r4 p- J

}

$ F: _! a# D. }. }. w, n2 O

T* __fastcall First(void) const

# k- r) H8 P$ t; K

{

4 R/ y9 Z( F9 L2 t* O

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

/ z+ O3 G, g: j4 f+ }' E

}

/ |/ R9 C+ J, S& \, H# @& w

int __fastcall IndexOf(T* Item) const

9 o' c4 X7 \/ P6 y; Y0 B

{

7 R! D) N" I+ \, {7 k/ ?" B

return inherited::IndexOf(Item);

J8 j6 m/ X# r$ w2 [. R

}

9 P3 B+ U- V' ~# }& \7 q( M

void __fastcall Insert(int Index, T* Item)

- X4 U& _& v" |3 G8 H1 }' s! L3 _

{

6 ^- w2 d# \4 x6 t' o# U4 U

inherited::Insert(Index, Item);

. e( x6 a% V' D* L' o

}

) q" R1 `) A! V) ?; V" u5 \

bool __fastcall IsEmpty(void) const

: N: H$ w/ W* ^4 ~1 j6 T0 z

{

( J4 l9 a4 t" j# z

return Count == 0;

$ B+ T4 h8 n+ Q; }8 n& u5 J! G

}

Z" \; u- V5 R& ?

T* __fastcall Last(void) const

4 v3 X7 S- z, L# \ _/ O8 c0 f7 @

{

% d7 O2 F8 F8 P3 ~

return (T*)inherited:ast();

9 e1 @ I. p! a1 @$ M

}

. @. ~; N2 I4 c$ V. Z& x

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

, X) M& l" Q# H) J

{

/ g3 L3 V: f0 u$ b, d$ p5 ?; h

int nIndex = inherited::Remove(Item);

, Y0 R" r" D9 v& _) B' D( x4 K. o

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

5 \- z: v& F2 ^1 D& z

{

& N& V& A. W; e. [

delete Item;

, c- e# r, m4 C- Z/ ~

}

( K: e) p* v, R* L" [

return nIndex;

! C0 z2 x9 [3 F) ]/ e. z, L

}

' v# e7 j6 \( L" x# p$ \

void operator<<(T* Item)

3 x) h8 l o( a4 B1 l+ U

{

, v4 G: D; S' m% @& H" b

Add((T *)Item);

4 a/ Z& U: b5 y: U& p

}

7 N8 b/ F& _: [! p

void operator<<(const TTypedList& List)

4 ]: a9 p/ a4 X) A, i) _& e6 g7 k6 ^2 |; c; f! S% @. o0 d

{

% j( f' C; m2 c; t7 M! a: |- G4 N

AddList(List);

* q4 x- ?0 a0 L ?% _* i

}

5 c1 |. j w9 `. Z3 H

}; // end of TTypedList

% \- [: b# J# Q9 C+ u

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

1 V) w1 F7 J! [0 z) b& \/ l

#endif






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