QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

; T: @: W! @1 N4 v' `/ H

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

2 I1 j! e3 H0 V

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

6 N+ L% r" }( \) `9 p( }" y' }' i7 P

+ Y3 Y+ W1 B0 N& ~* w) a' ^

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

" ?# ?- O: {6 }! T

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

: d9 y9 p& y) f# S% ]; V" i9 y- b

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

9 Z6 N5 l. f1 O, H- d2 a

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

* @$ z% X* d+ F; h Z% u% e

#include <classes.hpp>

% ^, Z4 V2 A: @9 W( }+ x

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

; D0 n/ u N( h: _2 y+ M& U

template <class T>

8 l; l: R7 n* r7 A; l" T

class TTypedList : public TList

! F5 t% `; x V- \& {$ k9 F7 s

{

; P4 F0 {+ a" u# k7 ^

typedef TList inherited;

) h) k5 _7 c& \& U& t% X

private:

! _* N# l1 d" I0 Q! s

bool FAutoClear;

! v: \. `5 g& R/ K& M5 A1 Q) V

TClass FItemClassType;

8 k( C" p. H' F7 I) J7 y

protected:

- [" [5 Y J4 ^: N

T* __fastcall Get(int Index) const

2 ]9 u9 k8 i: X3 }+ E

{

0 t. W$ V" j. ~4 D; }

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

+ ]6 u) y# Y0 S% x6 F9 F

}

' v& T) ~( |/ G# \

void __fastcall Put(int Index, T* Item)

, ?- D( h1 a& s3 Y1 l* I

{

) ]9 L& u5 m6 q! H

inherited:ut(Index, Item);

& H- H" y8 F% c2 K0 v

}

& G& t# o6 m6 Y8 _- Z

public:

1 ^: N8 z! f9 s+ ?

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

, m- s4 U5 C& P% p* g

public:

0 ~" c; y1 d2 X& J8 [5 s# e7 a

__fastcall TTypedList(bool bAutoClear = true)

. v" D+ R' D/ _& x* q6 N% ^. y

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

. H; @8 I) N; M/ W# g3 H

{ }

) \- i( }8 g. o: ?( v

// no destructor needed

6 t6 T' \$ b- C7 C D+ F

int __fastcall Add(T* Item)

- b# Q/ N8 d% ?3 J6 I

{

$ K7 e" y$ S$ K6 e; `0 z

return inherited::Add(Item);

, W9 H7 Y- a+ E3 z2 C$ ?

}

5 E! P! h2 G8 }% Q6 l

int __fastcall AddList(const TTypedList& List)

# I& l8 `/ h" K/ }2 C1 C

{

. _4 S& n& K" o$ t1 Y) {4 L. }9 N

if(this == &List)

B5 n0 A) o: s

{

7 z2 [# T( k* d/ n) E# i" A4 E

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

) \8 I- W ]. b! f; k! n

}

7 K/ O$ W7 T/ T$ Y, A: ?; C

if(FItemClassType != List.FItemClassType)

1 c0 U% r% e7 I$ u! [, p* D5 a) D: d

{

P0 r- p! @' M/ O: h: T0 j: f

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

" [, n! d7 Z5 A0 Z

Type!");

9 ^3 J$ _ A" h3 O. V" j5 \

}

) M- _5 w+ |! H

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

' q9 H* s4 [# g- p

{

+ ]! g) I% I# k& S" S' F9 r

Add(List.Get(i));

+ ^& F. ~1 x: i6 ?. \7 v

}

% ?$ R3 Q2 l: R: W

return List.Count;

+ I) n- m% z0 D" x2 G* W

}

- T1 T- f! ?/ e' ~; k- I+ O

virtual void __fastcall Clear(void)

( @2 @# g& H0 U$ C# j" k: E

{

; m5 `( ^4 h" c Y

if(FAutoClear)

7 O/ o, ]9 ~" e! g# o) I2 f: G

{

, y! x- {. }) A) s* ~

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

2 {# H' s/ Y1 m3 u

{

$ K7 w0 z- E" a U B* y1 y

delete Get(i);

8 A3 x Y* b7 D# I. j

}

% W4 `& o; E, e" d

}

9 D2 I5 _" g# i; a# M

inherited::Clear();

& A {! U) q6 m

}

# i i( F2 m( V ?1 f+ W$ A

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

4 U" M* n% a* C4 C+ K/ _& A

{

T5 l% T1 o+ y

if(bAutoDelete)

+ F) f6 }# @. B/ B8 E* D7 |

{

# x( a; w% B- R& d% f- f+ D

delete Get(Index);

' C9 b- C* W* l1 E

}

( _5 [! x" h) U1 f

inherited:elete(Index);

/ j/ y, S1 ]- y5 @

}

7 Q }7 w& ~+ t* {: ?$ x V! p

T* __fastcall First(void) const

8 \9 ^% R, Q9 Y5 H) U) Q* Z

{

0 o' i, R# ]5 a8 D

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

& O) d( H0 D0 _2 t7 }: |

}

9 h0 b: S4 l+ ~( P2 s

int __fastcall IndexOf(T* Item) const

1 |, m" l& j: K& [4 O+ N

{

% g1 `& `5 T5 G x: y

return inherited::IndexOf(Item);

% z4 s2 t1 v' v6 e

}

$ U) `/ N! c3 K( i

void __fastcall Insert(int Index, T* Item)

, M8 m' D" q. _( X3 `

{

, _* j4 b/ A9 M* q/ N/ Z: q

inherited::Insert(Index, Item);

5 l5 c2 o' c W* W$ V

}

' p- s$ o: @) X2 u/ i, q

bool __fastcall IsEmpty(void) const

* b& U8 r3 X" L4 {) a0 U5 T

{

- S5 b6 _+ r B0 O* v

return Count == 0;

: U3 Q" I! m7 G! f9 J9 h+ `

}

$ _) b. d) N6 c/ e6 F

T* __fastcall Last(void) const

+ n K5 a' ~0 K5 C+ P

{

6 A6 w" o, H& A1 \! A1 h

return (T*)inherited:ast();

! S) _2 |; y" G/ X: _; c; t

}

- M! d. f8 u6 B5 V

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

& G$ x$ J- |; Z' I& R

{

, X* R1 L" N& _- s5 C. a

int nIndex = inherited::Remove(Item);

. o& j6 o0 x; L: I3 T

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

( J$ d6 }1 }" r- E2 K

{

" O8 ]; m* l3 D Z+ Z# o; ?% m5 e

delete Item;

0 b1 g# @5 w" l3 @

}

* N" Q; M% c- M5 i& R' R5 B% J

return nIndex;

3 l5 D) }2 c4 @! x

}

1 J! A4 Y# v* S. H( Q/ p- d

void operator<<(T* Item)

' D# Q+ [( q7 d. }6 Q: X6 m

{

* y+ n2 n$ n% m& ~- m7 T

Add((T *)Item);

, W0 s; w! ?6 b3 O: X* K3 X- H

}

! N7 M+ o1 N6 E8 S; |5 z3 S

void operator<<(const TTypedList& List)

7 l$ G! {. F0 e+ X! z3 B! _5 _8 X4 h ( `" |# ?- d$ O* N

{

2 s# x( N0 f: Z( u2 d9 c

AddList(List);

* J& o7 s" I! I( r

}

8 Q+ f6 h' z# ^, f2 A7 ]2 V

}; // end of TTypedList

* i) U& c, l' q. H, L: a

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

. q+ \3 G" ^1 M$ j

#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-4-18 21:41 , Processed in 0.431297 second(s), 52 queries .

回顶部