QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

) O7 }# P; A7 _. K# I+ ?

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

$ l0 r) T- c& s m

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

& A" v7 Y; i# Q6 y6 p

3 c2 U% n9 f5 c. E5 ]

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

5 s* Q; N9 |* H) r! R" A, e* l- J

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

9 X; |2 O+ A& t

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

- k8 X3 B- g$ Y2 J; Z* {# f

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

7 c$ X' ~+ e) e$ H

#include <classes.hpp>

1 X+ i" N, e( O2 Y

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

: Q) g. { a3 J

template <class T>

& n1 B* d4 D7 S$ ?, `3 t

class TTypedList : public TList

1 L. L0 @$ e" @4 l2 i! R" n

{

" c' d2 \2 n# Z7 L4 c4 G

typedef TList inherited;

0 O" j- J; j& h4 K

private:

' B4 c$ T+ d3 h( u: v+ m5 [

bool FAutoClear;

9 v( ?/ z4 R0 G

TClass FItemClassType;

9 b0 C$ j! P8 y% d) }

protected:

" \; A+ p2 o+ P$ S+ ]7 k( r" I. L# _

T* __fastcall Get(int Index) const

- ?# i* i P$ X# ~8 b) C- b) n

{

% n8 }( ]5 [0 b" _6 G

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

9 Z/ q2 I# j! H3 n* G

}

" X/ P: [$ T6 R. e

void __fastcall Put(int Index, T* Item)

; R$ O9 c; Z% w- s: w

{

' ~! |- u( X- I! _) ]7 [7 r6 Q

inherited:ut(Index, Item);

# ~$ C, E; c5 X/ @& M- L

}

. r, R2 K- K# N$ }

public:

`9 f( m$ P e; I

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

* M7 Y0 h h1 f

public:

, A' s$ ?6 N: ^

__fastcall TTypedList(bool bAutoClear = true)

( j5 p6 a+ S. r3 |. |9 f' c

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

; y# D- D' W4 U) D0 ~

{ }

( o$ T$ @7 z! r3 {

// no destructor needed

$ p: p) P. W7 C J! _1 K

int __fastcall Add(T* Item)

k% w& q, i$ h. W

{

) w6 q& b: y7 A7 {$ W

return inherited::Add(Item);

! S1 K- E, U% p \; W% S

}

8 G1 C( e% I1 U+ k) n" U

int __fastcall AddList(const TTypedList& List)

# C; `/ v, P5 s9 R4 c, O+ W

{

* E9 T* | x6 g

if(this == &List)

" H6 t1 K: H/ d' `! K J0 \

{

" r. R" K9 E7 o) X) O7 \

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

* c4 |; S1 Y7 x' G5 g9 U" I6 p% P

}

* \/ g( I% t% [( A5 t: i

if(FItemClassType != List.FItemClassType)

" o" `" g0 @: `+ m% p

{

3 x7 B6 q6 l& h9 A5 ^% C5 o; G+ b# ~/ r

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

8 I+ H4 N; l( _7 t- Y7 A0 P

Type!");

$ R. E1 T; T1 }* B

}

* Q9 Q- n1 j: F% @& i' Y

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

' Y: D. H& [- ?& h4 \2 V2 S; C) G

{

6 } |9 d" t) `+ E) a

Add(List.Get(i));

4 b; T, z7 A' ~" Z! N0 U. u/ X- F$ ?

}

O2 z6 [$ E3 O3 `

return List.Count;

/ l3 ~9 `2 y3 s/ N8 O: U

}

9 A" T& p( E% [0 Q- l1 y6 m2 W- _

virtual void __fastcall Clear(void)

9 y6 S2 g! q1 w5 m; q3 M- ?

{

! j6 G4 P% u# T( k

if(FAutoClear)

" U$ z% a* N7 l2 \1 l; \

{

; t" g8 a' L5 H4 }- x+ T

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

/ i& [) k2 \9 [2 j9 |7 B+ u

{

$ F& C5 C3 P% p) e. t2 p

delete Get(i);

( Y# b5 U% Q$ S9 r, M- E! v

}

0 i, V+ U4 U( j

}

, w) B4 U$ H# J! B" r) W' A# ]

inherited::Clear();

6 R: h1 r' g7 X) d. T

}

. M: `( X. h) t/ E5 ]

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

; b, l- U. O- z3 D( O6 T8 e) Z7 v

{

3 N/ k) _. j$ n' c8 Y. P

if(bAutoDelete)

7 \1 h2 g3 H4 C+ p

{

$ S X' C2 d- \! j. X8 K7 f

delete Get(Index);

/ i5 O# ^0 @6 w3 H, F5 ~

}

/ V0 H9 h' U" r( }+ x7 t" ?% U1 K

inherited:elete(Index);

# C- i7 O' ~; j/ V$ R

}

) \: r6 M& K6 f3 M0 K3 S

T* __fastcall First(void) const

$ o- |: e& y0 w) Y* Y

{

( D* M4 _9 Y0 I) s

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

# n+ d2 {0 w. s: b: z; x" l$ T& Y! g

}

# K, G# W) \, c0 F- t2 r5 ]7 J4 n

int __fastcall IndexOf(T* Item) const

( I" \8 a7 J2 u% T

{

5 l# `( b) B u1 E/ v3 i

return inherited::IndexOf(Item);

% {) y; d' w/ s- S; {0 O

}

! S8 F" l6 P$ L' \0 t! V% t

void __fastcall Insert(int Index, T* Item)

+ a; x$ M' r8 }: r5 @

{

' ^7 F" x5 q6 p' s9 h9 f3 H

inherited::Insert(Index, Item);

Z, y8 I* Z. ]( N; ^3 s, ]

}

/ J4 @$ k f* A; V- X& X* G) n% J

bool __fastcall IsEmpty(void) const

- S9 t# x8 ~9 _: M5 [! j, L' d

{

3 Q& z. X7 b# m+ V @" V, x

return Count == 0;

7 |6 }/ S" e) i7 O

}

1 ^ m# o3 K& U7 L( J! S' r1 J) r3 u

T* __fastcall Last(void) const

1 g s4 A j: D& [

{

" D9 I# J5 `3 F% x1 G N

return (T*)inherited:ast();

. q6 f7 i% k7 i

}

+ ]7 K# I3 @! S; Q/ e" _

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

+ n# r+ K* K. O M- S9 O3 K. |1 X

{

J! F3 b; [4 N: |# y& ]

int nIndex = inherited::Remove(Item);

. c1 ]# B+ S$ L5 p$ ~9 b: O

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

* K+ m, i6 t4 r C& A l

{

0 d( C2 `9 g2 h: X0 k1 _+ |

delete Item;

0 ~% z, q' ]( b5 Q5 b& U

}

8 H& g) c4 f: `4 P, s- e. {

return nIndex;

' ?- \, {3 Z# i. W% S

}

3 x% m! A. v, N7 i V- _

void operator<<(T* Item)

& d( Z* ]4 L; A, R9 d

{

) O w; G& c8 d; @2 ^) o) V

Add((T *)Item);

' V7 _; j/ T r, w" h

}

: B, Q Q( _6 H( C3 g

void operator<<(const TTypedList& List)

- C( a, j( j' Y$ G8 z 7 L6 u3 S$ j1 h7 E1 W$ l

{

3 @ d6 o/ s+ ]( v3 f% R

AddList(List);

4 A0 w. S/ Q. o% a; e

}

$ G3 O7 {" k3 d: G* e* l

}; // end of TTypedList

& D8 j U5 q+ ~1 b! b8 B

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

& ?; K) t: q. v! Z2 s* g

#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 02:05 , Processed in 0.420558 second(s), 51 queries .

回顶部