QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

E# ~) Y& x! J0 _) I( R; T' D" ]

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

/ r9 l- V6 m/ t- x

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

- f7 h/ Z. f( T% s2 n

. `, i) g u( J; `; e# c

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

4 M* E# x" t( k' P: \

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

/ i" @& N( c7 d# E0 n- P+ v

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

. @0 i. j# @$ d% j" Q h3 S

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

# N4 K) B% t; |; G4 h- M0 v* G% A

#include <classes.hpp>

- a. [& _( P G/ c$ L

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

& I7 }, _# t& _' C5 ^( U

template <class T>

8 U* {" n; M" z9 I

class TTypedList : public TList

" g: U8 B8 }. B& }

{

7 n) ?0 F! @( u8 K% l3 Y' H9 [

typedef TList inherited;

- Q9 _; \. r$ U- S# I

private:

: u3 k/ V* L; E- P; W' O

bool FAutoClear;

5 Z+ V, {& ]1 N. z8 f$ E" R

TClass FItemClassType;

+ F+ Y% a( ~, a6 X9 ], B: g; ?

protected:

) ^6 X: C; I: ]! D: i0 o9 g1 ^

T* __fastcall Get(int Index) const

& `8 e% g) h# \# z+ X' g' A

{

2 X3 w- J+ k6 f v+ e' x

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

$ L4 [/ ~$ z- |' h% R5 E

}

6 o0 b( \8 V9 ]5 C% w; p6 r

void __fastcall Put(int Index, T* Item)

8 ?$ o1 Z+ M" G6 k0 {

{

# y# Y: \2 |! M( C2 s6 }) H

inherited:ut(Index, Item);

, l& C+ _4 s! t! D! s

}

" J/ [( c7 D$ V$ T

public:

" X" T! O7 \8 t

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

% B/ O5 e) A* N& n

public:

. ?; Q p: |2 N& c! m

__fastcall TTypedList(bool bAutoClear = true)

" b0 I; X4 ]- v" g; _: l

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

! B3 |/ e! e' T- o! W8 j2 }

{ }

& n3 U' V* {* R, ?4 C* l. h, p

// no destructor needed

$ \0 R* W$ ~7 Z7 |5 k$ w

int __fastcall Add(T* Item)

8 M9 c/ X& ?# x0 k) ~. |

{

) o4 f( J: }# u1 O, Q8 p

return inherited::Add(Item);

. Q% N+ `, f1 w* }6 e

}

+ e: W7 _8 `. H$ s$ k J

int __fastcall AddList(const TTypedList& List)

! O1 u) A* e3 d2 G8 O7 d

{

( N$ G; [! \- o" D7 F3 E

if(this == &List)

! i( n% W5 x( c5 J( P) z

{

8 N; ~. z: X3 B V4 B6 Z& H p

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

9 x+ P: G4 }9 Q& p! P

}

+ o' P d8 h+ t( U: f4 w/ f

if(FItemClassType != List.FItemClassType)

" o$ l: Q# b! \3 p( V

{

, Z, G# K& Z9 C8 B o

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

% a; o4 I0 y \, K: p7 H

Type!");

! k: M5 Z3 Q& i) n+ N: g8 m

}

+ R' M5 h. [2 d- f8 g2 {' P$ D

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

! i8 t1 w; N" `# @+ ?8 t% d

{

% b* Z# F/ l4 _* x! z

Add(List.Get(i));

7 E; I9 H7 e% F8 ]1 O) f/ Z: w

}

2 e0 f. l+ L8 K, j3 a

return List.Count;

! S6 P5 \& p: s- ^( E

}

3 y, Y% I8 a8 Y' P

virtual void __fastcall Clear(void)

& K# Y9 g0 ]. O4 S9 ^. F

{

9 }& B& Y7 w8 |' m

if(FAutoClear)

* y$ y8 e6 I/ x+ b& j* U1 ]

{

# Z3 e7 I8 n5 b# U

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

: E$ k8 K0 O$ e% ?- |; o t

{

( n) X& @3 N2 _" a

delete Get(i);

T1 M* K" n: T6 }* S. f9 ^3 L$ z. W

}

: C3 i% j7 R9 |2 d0 d1 J

}

7 u. ^( O+ s+ S; _! y* N

inherited::Clear();

+ P5 L+ A7 A3 Y: k) N& _% i

}

' q7 T) i1 H* i$ U2 d7 `8 S {. _3 ^

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

; t. c7 T& @7 i6 i+ ]- k

{

3 Z0 R9 Y/ z' H5 X

if(bAutoDelete)

7 x+ p9 d; U& Z7 S8 ^+ T( h5 U! S

{

' ^* n7 N. }; \

delete Get(Index);

( ^9 {# U4 f" Y3 O$ }/ A

}

: e* ]; s: K- G- ~+ D1 B* c# _1 a

inherited:elete(Index);

& F0 Z3 I1 D4 @+ R' N

}

: l8 L2 w% l- j5 u( p+ c% u

T* __fastcall First(void) const

- `9 K" T1 {: {# Z6 M

{

( L% G% h7 d3 ^1 D; w

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

1 n( e) c% l: K

}

, H& }3 U2 J* l$ F

int __fastcall IndexOf(T* Item) const

* Z" D0 |$ ]5 K; B/ X! k+ O* A' w

{

" G0 d' k/ s8 `- R; q# s

return inherited::IndexOf(Item);

) `/ I9 W; Y1 T: w

}

" U3 M' _8 Z3 t$ _6 x1 F7 C5 Y3 w

void __fastcall Insert(int Index, T* Item)

/ {( _3 y# t* }3 X. ?' o

{

" [- S" `1 z1 j" H0 q

inherited::Insert(Index, Item);

* S3 w7 d" J5 M2 I4 E0 a

}

! g2 b' M. _. w: w! j

bool __fastcall IsEmpty(void) const

$ w3 Y, R) }; J1 N9 d# ]1 x

{

$ ?4 b. _9 W9 o: p

return Count == 0;

- u3 J3 r ^: _% N( d7 y/ E

}

& R/ E' r$ s0 u

T* __fastcall Last(void) const

4 O( s1 `% r- c/ {" o

{

/ c0 b4 m3 z% u7 ]7 }* c7 N' t. h

return (T*)inherited:ast();

2 c2 X8 `7 y# c) X# M- c

}

. u% O; g: u4 h5 h- Q( {

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

+ s4 s8 Q% b9 @0 E

{

; K8 X4 ]& @; |4 G6 J

int nIndex = inherited::Remove(Item);

* T" y5 Q( r1 i, V5 `$ j

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

3 x6 a' C" P* {; ~5 {

{

% P& Z. n: N4 p- i) C

delete Item;

- a5 W9 A4 r, H

}

* ]0 G( i: c. U9 a9 j# ]

return nIndex;

+ \# f# a8 [4 |! K

}

. X% y. r' [/ w1 |4 I

void operator<<(T* Item)

- F1 i! H/ A* a7 s( [

{

* t2 L' q5 Z! E% b

Add((T *)Item);

& ]; _( Z. C. }6 m. u. K, W# R

}

) J0 g0 N/ Z0 T# h! w# g; T8 Z9 N

void operator<<(const TTypedList& List)

! u- `, g7 C9 L, p. h" T 2 r' `) S3 ]5 B1 e) L3 x

{

5 ^3 r/ M8 W n' r) w

AddList(List);

5 M9 v* e" T6 X9 v) g& \( h

}

, h; `: B6 x' n/ E v( I

}; // end of TTypedList

9 S# ]( [, g1 @6 X

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

6 d9 ]1 _& k; m$ J9 l0 B$ H& ^

#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-6-11 01:52 , Processed in 0.299987 second(s), 52 queries .

回顶部