QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

* F |4 I& G6 n

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

0 m/ L; u5 v2 j! q0 L8 f

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

: Z* `5 _0 `% T' g& C. {3 g5 Z7 K

4 o2 |9 |- E" X) j5 |

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

2 T1 V1 L! X3 \9 @( z- B4 L' |

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

7 A8 r' e% T7 r0 W0 p' I7 a

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

2 \' J: o, U' x* D u+ j# C

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

" u1 I! U& u2 H) X7 w0 [

#include <classes.hpp>

7 Y1 ~+ I- V- `3 i) L! y

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

2 q, G' ?3 p; Q6 t4 @0 D2 a' U

template <class T>

- \7 _ m4 f) z" b" }

class TTypedList : public TList

) [0 ]: d# h+ p) K/ H; }4 X* |

{

" r, D& K9 @5 K5 U8 q- b8 O. p

typedef TList inherited;

+ k$ a, @" A* f* l( X% o

private:

* b* b! r3 B/ l& {7 o+ C

bool FAutoClear;

9 g, G4 m7 B8 B" A

TClass FItemClassType;

& [$ p$ F% R" W

protected:

, f# G! c- r8 J+ J2 i7 @

T* __fastcall Get(int Index) const

% u' j1 |& g4 W( L; f

{

; c8 i4 [( h; |( l7 c: a+ G+ B

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

[8 Q$ n7 w4 X6 m& w# {

}

/ z$ s9 v8 D* c$ H ^% T: s$ ]

void __fastcall Put(int Index, T* Item)

b: _! A& N* D! \

{

r( P7 x" E2 C# n7 h

inherited:ut(Index, Item);

7 }: Q. ^, @7 S4 h

}

" n9 v' E- Y5 w) e

public:

, X6 o- [# q- {0 Y

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

- N/ e* k; k3 T9 O) a1 ~

public:

$ T2 y4 m5 M( K3 M# s* F+ c

__fastcall TTypedList(bool bAutoClear = true)

" a z6 l3 d0 W8 E; w

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

1 e0 b& _& a2 |$ v: O& a

{ }

% ?7 `7 b: d# l" [$ t& A' e2 f

// no destructor needed

S7 V5 Z/ Q; b5 Y) d, B8 y

int __fastcall Add(T* Item)

' [1 o1 N z- _- J6 I; q! j' o- I; W

{

; s" P1 O! Q& Q1 S

return inherited::Add(Item);

: H2 J2 ^8 |- U- Q) s

}

. x+ ]+ z; I# Z; D

int __fastcall AddList(const TTypedList& List)

* @- \9 I, ~3 `

{

. p6 a& ^" Y+ F a$ Z Y

if(this == &List)

+ K( g2 [9 m9 S s; r) z

{

0 i" k# R- P4 u

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

1 L* f' ~: V3 X

}

& _( l* y) M) j2 _) l

if(FItemClassType != List.FItemClassType)

7 M: L& }! P9 n9 [9 H$ d

{

$ a1 {9 A7 ^8 i% f, U

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

; q: \" l" ^" `: z* B! `- O

Type!");

/ o+ p8 \% K' T/ O( x! F

}

: O* i; D8 [& w$ W7 L: E% Y' B

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

0 I3 W9 L2 ^2 h" R5 W

{

( C& _: o. T! z6 Q& Q8 m, ?$ @/ V: d

Add(List.Get(i));

9 V! b- Y B4 Z* f# Z$ u

}

/ n' U: |" d3 `" u

return List.Count;

9 [$ p# u1 C) `, q$ Y9 e& k7 W

}

3 y) b; ]& |! F7 N5 h2 K7 s

virtual void __fastcall Clear(void)

0 a% J k& ^/ q! W0 \7 Q$ X7 }" R ]

{

3 p6 z7 c& A) Y! Q; O5 c3 ?, l8 F

if(FAutoClear)

7 c, r7 O! @% I0 a2 }1 k9 A( a$ ^

{

2 }3 s" H# O5 Y9 I

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

) \# d' t6 n. `$ f

{

- s2 z6 `. s+ b$ t

delete Get(i);

3 D6 n& `! P; f' `

}

! q5 P* V; w+ D, S% C

}

4 h9 R& c. N8 p# a0 m( J

inherited::Clear();

% L& u# U' l8 {0 V; j7 M, ^

}

' w9 \: F6 `; ?6 {* D+ L

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

9 S$ i2 J4 G" O: S/ l

{

7 B2 U# E C+ F4 Q+ l

if(bAutoDelete)

: m8 k* R$ u5 p' n x

{

9 O, v( D7 _. ^! d- U* F! A0 g

delete Get(Index);

# o! w2 Q- O9 H C2 U

}

9 u, [$ K% W3 E9 [# K5 m' A4 X

inherited:elete(Index);

3 ~' C1 ^) x$ \4 h

}

# g* y7 |1 N* x* E

T* __fastcall First(void) const

, k! A) u. B/ _8 T% b1 l( s

{

; |0 V: O+ H- d$ u4 n0 U

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

f" o; O5 |4 M9 a6 I4 y* P

}

: `" F1 o" b1 @% g

int __fastcall IndexOf(T* Item) const

; N4 G8 v) V6 r6 Y

{

: @3 O$ x; V6 t& O. ^

return inherited::IndexOf(Item);

9 S1 P7 n8 R5 y8 c, [1 u" U

}

5 d+ ~0 y% H. k# A/ l8 Y

void __fastcall Insert(int Index, T* Item)

4 f+ ~/ {- Y# B" R: g1 F- d

{

+ e: A$ [+ V+ j# \: c& J* p

inherited::Insert(Index, Item);

+ T' O3 h; K( m q/ @

}

! x4 o' q& r( Z7 q% C0 r# G, \

bool __fastcall IsEmpty(void) const

! b) t! Z! O: T( u! v" e% `

{

8 o: {5 }% `5 U

return Count == 0;

6 ]5 s; I: u- x& z

}

# `9 w8 G& z3 j0 C. r! [

T* __fastcall Last(void) const

1 S# [9 U0 f/ i3 P4 M* h7 _

{

/ ^$ x. u: O$ p: {9 y& e0 x

return (T*)inherited:ast();

6 `2 g! L: T( W; V

}

% G% {/ ~- M1 y L0 f" F

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

) Y/ Z) f! }" W$ ^/ b( R2 i3 l

{

3 w: n' J: `9 r, v. i9 @9 H

int nIndex = inherited::Remove(Item);

' h7 c6 w2 ^. w: ?1 l/ d+ o8 L( m

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

3 b6 Q7 z5 g0 S' M1 L& }

{

+ K; H* c6 e4 [2 W+ L2 G

delete Item;

" U# Q" v) U& n' ^$ w8 ]$ N

}

; I( O- T: R( x( j

return nIndex;

8 [. i# f6 D) j1 z( b! u# }

}

: e8 ]/ B; ]7 R1 n2 d5 Y9 R

void operator<<(T* Item)

+ Y/ p5 y9 q- G) H

{

5 y I: O: V* {) t

Add((T *)Item);

+ r" D* j1 o0 Y+ W5 ?

}

4 o- U% |% r; K8 A7 A% R# t7 w

void operator<<(const TTypedList& List)

1 J' i# o V+ M* b' b ! ^5 E D) G" T1 T

{

/ c% p6 B8 |8 [# R% ^

AddList(List);

1 I3 F9 l1 Y+ w' n2 _- Z

}

. H$ Q, j* Q7 _3 f

}; // end of TTypedList

9 Q# Y# u7 H4 y! t7 m( J* J5 ]

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

) z3 m. e8 t/ |& v4 a) l; H) t

#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-21 23:50 , Processed in 0.432074 second(s), 52 queries .

回顶部