QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2616|回复: 0
打印 上一主题 下一主题

TTypedList一个类型安全的TList模板类

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:11 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

2 x" q; o. }+ v8 m6 d, H. v

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

7 e5 O0 u/ ^9 L* x5 R, v/ V

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

7 w+ y! n! m+ f0 B

4 g: j1 h8 U0 X8 @( d( A

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

- v* n: A( C& B2 m* C1 k$ f

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

; \+ r: \ h! X2 {9 C% p$ w

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

5 H, y% ^- R' U8 K

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

5 T+ q6 ?& ?# A8 `+ f% _

#include <classes.hpp>

. Z8 [/ a; a \9 M' E) o; a) L

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

( v; B" B% x' ^# M2 j

template <class T>

8 P5 L. V' |) h3 S" s5 a+ }

class TTypedList : public TList

) P; n7 Y* J% K. X3 z& }

{

+ m; v7 C! h! T( D' g2 {5 E% E

typedef TList inherited;

( z0 S N6 t3 e |+ Z$ G8 q) W& R0 P

private:

& K z) S5 K: D. e

bool FAutoClear;

( [. O( W) x) n7 Q) Y3 r" g

TClass FItemClassType;

I1 m, L- o- @

protected:

) h0 G2 U2 M2 b

T* __fastcall Get(int Index) const

! M9 M- y. y: y; k2 }

{

2 ~- t6 M0 k. r6 ?7 @. R

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

8 k0 V" [9 [2 Q( R

}

x( X* [+ F7 l5 m, ~4 M! l; K. t

void __fastcall Put(int Index, T* Item)

6 x8 ~6 M$ T) j' B; \2 y

{

$ C2 g4 `: A/ N" J$ e3 {$ V0 L9 }! N: ?

inherited:ut(Index, Item);

5 M& ~$ e1 h* s; j F

}

* t/ R; I. m6 a

public:

( D7 z( _. O( r& h/ S

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

3 \& z- Y1 I$ Z9 w+ ]

public:

, H9 q* D6 Y) q/ G* ]! B. _

__fastcall TTypedList(bool bAutoClear = true)

$ y, G1 c" c* d2 l4 w

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

2 C( f) O' b, K+ x, A

{ }

5 w+ G6 g p+ M1 J: R i

// no destructor needed

4 {5 w# `/ }# a) {& c

int __fastcall Add(T* Item)

( g# J) E& z# X3 x! z: @4 g4 T$ q

{

7 V+ n# r5 k6 l0 E' Q5 w- e+ L

return inherited::Add(Item);

+ I. d$ n' {) ]( l4 h4 N

}

. `- G1 g0 H: m4 [7 |; [

int __fastcall AddList(const TTypedList& List)

3 b+ k; {+ N$ Y5 F( j

{

% F: |4 ]8 h: R) F( G3 Z) A& `

if(this == &List)

]; Z4 f! x/ ~$ i3 O

{

- `3 ^1 l1 [( |

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

. o5 e! ^5 P1 \ Q' y5 c4 j

}

% ^$ F( |8 R7 ~, Z

if(FItemClassType != List.FItemClassType)

1 ~9 k; i5 n4 N/ [) _: O- M

{

$ p9 O2 M$ e2 h0 L, v2 f) M

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

2 p. R$ J; P5 k4 N1 _9 O

Type!");

4 g0 ~4 ^- q2 x' Q1 [8 E0 q

}

! r' Z, y O; j: A

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

; \+ |# }3 Y* p) c, u9 H

{

+ S) Q4 V8 o7 y5 X' b

Add(List.Get(i));

2 f4 E d* E' n9 r

}

$ O* s1 k7 d# u7 W

return List.Count;

; i& e9 g& i9 `, g

}

- ]: H# s" K7 m/ n. J6 A

virtual void __fastcall Clear(void)

3 w9 W: H- e6 X" o

{

$ J3 l3 `: C5 a4 o( H

if(FAutoClear)

% C0 F) |- X5 R3 n, F

{

. C5 I( B8 G% P1 h

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

) w& u* x& I: `# R1 Q

{

7 H5 y6 r3 c0 R1 Q; o0 [4 P

delete Get(i);

; |2 U' d! M% }$ Z J* P: m: @6 U& W* Z/ ~

}

8 i P( G' |9 V

}

0 q& b2 R9 z1 O6 Y

inherited::Clear();

9 {- b9 [ z5 k: i$ c$ W: A

}

! L7 y$ X) ?+ E) e- f2 p; J

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

. j* u: x. Q% ^( W, x

{

! V7 v( e. G( _* r2 P8 x( ` W% {

if(bAutoDelete)

4 V$ |. Q2 K7 y+ l8 ~, c8 l

{

( ^9 X. v$ A- |

delete Get(Index);

2 V, E6 d6 b$ b l I. R; V) H7 I# n

}

, J* b6 f8 J: u

inherited:elete(Index);

9 f& D0 n+ _7 Q" m7 G5 z

}

1 S( o1 G7 Q! @/ H% k% p# F5 K

T* __fastcall First(void) const

( ~5 Y) Q0 l, ^8 ~* [ V1 t( @

{

* `7 W* E0 f, |5 U( }( Z/ X

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

1 D/ }3 h0 q" x4 \& o# ?

}

, `% q9 Z# N* s+ J

int __fastcall IndexOf(T* Item) const

+ R/ @: M9 K. ]( U9 b

{

) S; e8 ?* c! y" d: E

return inherited::IndexOf(Item);

4 a9 g) @, Z* T. n, P# p

}

/ x- ~" j* a/ k$ {3 z

void __fastcall Insert(int Index, T* Item)

. Z1 c- Y8 z5 c

{

+ K% ]+ d$ \) t$ P* O# V7 f

inherited::Insert(Index, Item);

) N5 r" g2 @% v) \/ i* L

}

6 {6 z$ R9 w$ T& V5 M G

bool __fastcall IsEmpty(void) const

! U, Q! q. Q9 i. h

{

* u! _" q- }# {. H) C; O7 q

return Count == 0;

0 B# g4 |$ f9 `8 t, ?4 v" s

}

, C; n1 N% w( \( c- G1 G

T* __fastcall Last(void) const

5 n+ z% d1 i% x, K/ I6 v- o) u

{

3 ?. h" e1 Z/ A+ ~5 Q( `7 t3 J

return (T*)inherited:ast();

4 b- \) i: y- ~( O7 b) u* y

}

: M) c' I/ S4 h8 K e, ?$ o

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

?( d8 p2 o+ ]9 R& ]7 G

{

4 p/ L" `' T; F: [( q0 ^

int nIndex = inherited::Remove(Item);

" w! j3 Z9 A0 g5 j* `9 S# x, T: S

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

! \/ p: x5 n, \' Z# `

{

6 A# y$ P6 d+ K$ V6 a7 f

delete Item;

2 Q& }2 g! B; O

}

; p8 t9 U& G5 `4 t. |

return nIndex;

: j4 ~5 |, e; t0 d

}

b2 t6 ?2 e0 E8 I' c: w

void operator<<(T* Item)

2 b& a) y2 A' Z B; y. c$ r3 E N

{

- O1 t! `7 j9 Q$ M' ~# t

Add((T *)Item);

" U( v4 ?; l; ?7 Y( |6 l; i

}

5 b% {9 D6 |+ g7 I; v( n

void operator<<(const TTypedList& List)

, ^# \6 z' l- W( @* Q6 ~ ( `7 o( m6 F$ ^( E

{

' {' j9 z, M; ?) h2 w

AddList(List);

( k0 z. A& C9 l5 U

}

' c; i$ f( P+ R( ]: W' |( P$ T

}; // end of TTypedList

& `8 j0 o% m$ n# o) s" U8 W4 n

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

% ?8 k6 o8 J7 d4 a" b! U% ~

#endif

zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-8-12 01:11 , Processed in 0.460438 second(s), 52 queries .

回顶部