数学建模社区-数学中国

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

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

& @2 N+ i6 ^4 r2 ^0 Y

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

3 Z4 l" ]; ]$ J" z, o* g! p/ f

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

, r! z' ~ O1 ?9 M

, y! x$ q+ l: o) c8 y

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

- ^0 N, Z4 V7 u7 O

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

: c* K. J/ C, [* Q/ S+ g

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

4 K7 a/ H( e' o" a% j

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

! ^# ^6 S0 H; Q6 Q `2 J, }

#include <classes.hpp>

( i! U/ u1 n4 r+ |

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

9 P* F3 s0 o9 @1 N4 e* L

template <class T>

/ [5 @. ]3 @& r

class TTypedList : public TList

: {# h8 a2 o3 q

{

1 U0 ^5 ^7 b) Y% L

typedef TList inherited;

6 \' @" ?0 y& R, c- y4 {) o

private:

& X& v: f- V4 \" ]6 r* ]( {( `

bool FAutoClear;

( X5 O4 u$ k* K [

TClass FItemClassType;

5 r3 Z% y0 ^0 m% |# V* D. g8 t

protected:

! D, |& S3 V& I6 F

T* __fastcall Get(int Index) const

, d3 }( w: ^4 n* N0 `" E

{

X# x' w# e" n) k7 J

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

V% ?- Q$ I0 R! w: z

}

U( `* N) y( c3 z3 u

void __fastcall Put(int Index, T* Item)

6 U9 I8 Y: s9 `4 W# D1 I

{

4 ?, ?( m5 v2 C7 a

inherited:ut(Index, Item);

+ T- h+ \) z1 Z5 x' y

}

9 l* Y- E2 U, l8 z

public:

& w) V* N# W7 W) J) \; n

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

0 J% J# l9 Z& \) S) `

public:

. ]8 S. F u3 {6 {$ z2 f9 r' x* Z

__fastcall TTypedList(bool bAutoClear = true)

6 H. M3 v1 c8 X3 L& i

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

$ `/ a7 }) V. ^+ m: ]+ _

{ }

. M0 ]3 y7 \6 p8 |* f

// no destructor needed

: a0 h- _8 z4 X9 T

int __fastcall Add(T* Item)

" W+ q; K. P- t+ H6 ]% R

{

3 n; u9 i" p$ ]; K7 e) `

return inherited::Add(Item);

( [; h- X C: ]! `

}

6 B9 o: F) O8 ~) y- z2 Y; \6 b+ D

int __fastcall AddList(const TTypedList& List)

6 U: q! _- ?/ G4 t3 t& C3 f2 Y& H

{

& b; H: a# X1 L' Z2 W

if(this == &List)

; J% D6 T. d: v, W+ H* @5 L

{

6 o; z" N* x* P" P

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

/ A" U( J ^2 a, t$ B( ~2 I

}

8 `2 T0 ^: p* S2 l1 b

if(FItemClassType != List.FItemClassType)

( j* Q$ g/ p% `/ L0 b, I4 P# E5 W; V

{

2 @3 U0 _. Q Q. W4 `9 L6 q# [

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

2 b8 \9 T9 V7 N4 ^

Type!");

2 V0 s! p+ l, D( j% F

}

/ O% M p5 ^8 L

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

- M# K5 i9 x" D% q6 P

{

0 A C: F7 k3 ~6 L, x

Add(List.Get(i));

6 x! q! Y S0 G# R- D: {' T

}

# Y2 |) ^( Y$ p- X' h+ ^9 ~: v' a

return List.Count;

, n: M5 p1 B, y

}

j, q- @. p! }6 i

virtual void __fastcall Clear(void)

) n Q' l( O& u9 ~

{

1 E- B# f. f7 T4 |7 H( E7 k

if(FAutoClear)

" _$ H' x2 s" n9 r Q- C" V

{

! `/ j5 j. b* M% H8 l! I- r, c

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

# h+ ^6 e& S" ]( V' B

{

9 I1 b+ c0 x) B; }! \1 w, Y4 `

delete Get(i);

' A! `2 J: y7 y' L' r6 {

}

# o" N1 g, v- k

}

. j/ I; V' I- Q! A* O6 @

inherited::Clear();

" a0 [7 N L+ t3 K/ q: T) N5 H

}

- l8 c2 ]7 L/ i* o; l3 e& b: b; a

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

7 e# [8 e8 P8 O

{

* z2 d: H# h/ ]/ v5 o

if(bAutoDelete)

3 W0 y( Z$ g1 c# [

{

9 S9 V5 R9 b; k8 b

delete Get(Index);

" H: y' W8 D& ^: T

}

3 F z. ~; |, w& d

inherited:elete(Index);

( }! S, {$ [% a

}

2 q p# y0 s. M5 a. B5 S% a9 d

T* __fastcall First(void) const

" C5 u8 y$ ?% \; e

{

1 }$ Y% O( P: ]! b) `" [

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

% I) |8 \1 y# v C* M

}

) X0 J2 g9 h# L- J2 ]* f! s

int __fastcall IndexOf(T* Item) const

( S, e& E8 |: z- v

{

* F# Q4 D! I: V/ H4 Y& N5 F* S

return inherited::IndexOf(Item);

; s( H1 K/ @2 r) I

}

2 A# A/ L- w8 p( e; X

void __fastcall Insert(int Index, T* Item)

, l" s: l1 R# O2 Z# l3 [

{

4 o3 J3 R5 m# r q$ m

inherited::Insert(Index, Item);

) ?6 t3 Y; w7 [8 Q

}

]4 }4 z" V' p1 X' M

bool __fastcall IsEmpty(void) const

: C9 q& J* ?6 A

{

7 D: Q7 K ]9 o$ e u1 w

return Count == 0;

9 E0 k& _2 o; K4 N

}

' L5 E1 M5 h. S' W9 B% u# M

T* __fastcall Last(void) const

9 l$ G$ E) [5 h

{

; j4 S. \8 n }* e3 e

return (T*)inherited:ast();

. `% I+ y) ~# t* a$ M) H

}

1 Z3 o7 ]" Z+ t

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

2 G; `: Q; v+ [4 ^( v, |

{

% L8 C p. N) ~# @- l, D

int nIndex = inherited::Remove(Item);

. B. w5 ^! v1 U6 [( H0 O

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

7 I% | `; j+ X6 m9 ]3 P

{

5 x9 k0 F0 u# ]

delete Item;

! ~' z/ O$ t- t% Y) u9 }

}

# F7 l) ?; x* ?$ Q3 Q3 O, z

return nIndex;

( L$ Z3 T: y2 ~: A; Q2 K

}

4 @( H) G ^8 ?3 Y' |

void operator<<(T* Item)

+ a( j/ [' e, _) ?

{

' Q; I) y4 Z) M$ e1 Q

Add((T *)Item);

$ g' g1 P" A' H2 r3 Z- P

}

4 @" i, d2 X, |+ v4 J

void operator<<(const TTypedList& List)

& K$ Z9 t% ?, D) _/ R' l u% F+ y 5 F4 Q- q2 V" C5 N# m1 s

{

" \" V: C" o6 H6 I. Q

AddList(List);

' e5 B$ i! ]2 U7 W& {

}

! d% p+ H3 Q0 M* t

}; // end of TTypedList

# c6 P3 t7 _1 r

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

: m5 q1 G' Q& h" O3 {) g% l) M

#endif






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