QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

. p1 S8 Q# z/ T; E" C

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

2 M; H2 J1 ]+ n. V8 R

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

. Q: S+ I6 ~4 S

4 s# v- h M+ O* n( K! Y7 z

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

- }# X, G/ K# a/ @& e

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

, l4 n+ B6 v/ l; I

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

% e; M! `/ Z3 {# K5 G R% I% ~

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

, _" S! C+ N0 W ]1 Z2 u0 i6 p

#include <classes.hpp>

* q% m( V7 x9 t8 r# Z6 x

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

! O1 w3 m2 d7 J% L. C

template <class T>

* z: z; ~) h: h! A0 g: s: ?8 X* Z

class TTypedList : public TList

! R0 V; t* B/ m: m/ n4 Z( }0 ~/ B7 a

{

: e- N( j Q9 i+ y

typedef TList inherited;

" G3 s: `' `* n6 Y# F

private:

W" `: c" `3 ]9 C9 O

bool FAutoClear;

' }- p% h3 K$ M1 M$ w

TClass FItemClassType;

3 ]( O6 Q: a2 e! u \

protected:

* @' I+ J. z9 n' D1 K/ h$ Y# I" h

T* __fastcall Get(int Index) const

2 w. x9 j! ^. R x- K

{

# l+ H- `- i+ G- \: R* H4 ^

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

2 X ^6 ]* K2 _: x* J, S

}

$ I9 k% Z5 i6 E5 @9 h1 ]: t

void __fastcall Put(int Index, T* Item)

7 p; R* h. Y& @ D' Y

{

3 j5 v6 O- L# @

inherited:ut(Index, Item);

( Z \6 s$ ^+ p' m) F- b) h

}

6 m. a' f1 d, p

public:

6 q; Z+ B4 C% l' J4 {

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

/ ]* y2 |+ Z$ [

public:

6 c; |! u! j# L. E0 a) M5 {

__fastcall TTypedList(bool bAutoClear = true)

1 e. a% @* M6 n

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

2 `& N. t$ v; X& o+ t: e p

{ }

% y: S+ A0 k$ Z7 J# N. s9 L3 H

// no destructor needed

4 |: }7 Z. C8 r; F. O4 `

int __fastcall Add(T* Item)

; C0 }( k! D# q6 L& v& ?7 {

{

4 U [5 O" q* O, }1 |2 \

return inherited::Add(Item);

4 \# ^! k- {/ v# f

}

. Q$ s" x3 X8 R; G( H6 G# u

int __fastcall AddList(const TTypedList& List)

- E0 m0 G6 m6 b% q( [( V1 n% _# ?, H

{

4 K7 s5 C4 d+ ^

if(this == &List)

- V4 U, C/ T/ C! z* }

{

, b, P4 |, z g7 S/ Z: m! @1 ~/ o

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

x! b" w+ @" H, \

}

! ^" m" `" \1 I) x/ M

if(FItemClassType != List.FItemClassType)

. F* j3 a8 w5 \3 w& M

{

) r; j, R r( P

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

! u3 W; X5 E* k

Type!");

$ x0 w5 H" y J# a

}

% m5 r7 K" o. c8 [) y2 _/ O1 T, `

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

, Y- r( e. @$ o7 P- w! r

{

' P c7 T! ]- f. p+ U6 k

Add(List.Get(i));

! S! D7 b+ { t$ \) h

}

I( l- h2 h% P: l0 {

return List.Count;

: q6 m2 [( v4 z; _% T5 X

}

! L; r1 {4 m7 _" a* J1 E, R

virtual void __fastcall Clear(void)

) {: h7 q' m& L( T- g' z; ~

{

* A' t# K7 h( v; F

if(FAutoClear)

) U W! a& S, ]2 l+ L

{

/ |5 W) u2 \9 o( q. O7 x

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

" l4 n: ?7 A/ \. l/ `' H1 b _. _

{

$ {0 T# q# G) _0 {

delete Get(i);

, |1 l- M8 M4 }. w' i) ~

}

0 `8 ?) z% ?, t" t) u

}

7 d7 M' v0 U# j" m: e

inherited::Clear();

% A& P: _ g3 f7 k+ E( g

}

2 S. Q4 J7 V7 {* ?% T0 G G0 T i

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

* R- l) z' L2 X+ ?* [2 q' t

{

& P: V/ [0 x3 r7 y* d* S; C

if(bAutoDelete)

# k. X& U' n" o& Z7 ?1 J

{

1 g) b! _( h C% m

delete Get(Index);

% V) f, Z& N9 O$ q" A

}

& _ Y, Q% k4 k0 Y9 H

inherited:elete(Index);

# n$ [" d$ N. v( v! z* }7 {+ k

}

, I$ f- d t B0 Y

T* __fastcall First(void) const

V4 y! {) N9 {1 E( V2 C& q

{

+ V2 O3 J6 c" g. c$ C

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

: A7 d8 X; ^+ h) u: x

}

0 A+ }3 k$ O- E4 `

int __fastcall IndexOf(T* Item) const

4 C' K$ t3 t' A! Y7 ~0 ^- {6 P

{

1 K2 ` v3 ~ T; M2 h8 {$ X

return inherited::IndexOf(Item);

- J' l5 b! w1 l% s# i

}

$ [! w9 y. a' W$ U }

void __fastcall Insert(int Index, T* Item)

8 w0 G6 k: N0 z$ ^# M8 s6 [( i0 w6 ~% O* k# i

{

4 G/ w$ F5 K9 G' f

inherited::Insert(Index, Item);

! ^1 V$ h) l* v2 z: R( I) \

}

2 i5 s, \% V3 x8 I, c) Y& L

bool __fastcall IsEmpty(void) const

3 R# d7 X- ^+ Q( l- y

{

2 e: k9 i( M9 }) j; ]5 e) Z

return Count == 0;

" H6 Q" r) s; M4 q, w

}

% H/ a9 h$ q. [& N

T* __fastcall Last(void) const

- L9 u, ]9 u2 | ^) u' I% M! W

{

( F$ k( T4 e+ ^

return (T*)inherited:ast();

- Q) r: }0 ]' ~5 y% M+ P) ]

}

1 Z! \; C4 H( `) j6 J) i

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

$ `7 j: I) d/ Q T4 i' ~+ k* h+ C

{

8 X1 ]9 C" f, a8 {: ~

int nIndex = inherited::Remove(Item);

0 K/ F" ~" y0 R. I1 y$ Y9 N& ]2 W7 }0 @

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

$ K5 U, l! [% Z2 X5 `6 F9 ?' I

{

- J) z; R1 r0 f% B! N# f

delete Item;

8 G& P) I- ~8 \

}

7 l, ^6 M- G1 N& t' V$ C7 G3 l

return nIndex;

0 p3 E( Q5 i, X. ]" k/ W

}

; h9 F3 G) ?, l' T h1 \

void operator<<(T* Item)

u, @7 r3 \+ @* Y ?

{

1 c6 @: A0 U v( H

Add((T *)Item);

# `4 i f( f8 _! O% O0 V. }

}

% Q9 B) X) W6 n8 D5 A# e

void operator<<(const TTypedList& List)

& V F1 W/ o9 w6 m, g8 R! ^# U5 I9 U1 d. D$ L/ O. ?/ y

{

4 j0 D2 v' M/ P, E& h/ f

AddList(List);

; v, o0 t& _* E) i: o; H

}

6 X$ n" _; U2 C. Y2 p- N- s

}; // end of TTypedList

0 P; q# m$ x6 F9 V

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

3 I1 E% }) q6 Z$ U) p3 a* G

#endif

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

fastpost qq
收缩
  • 电话咨询

  • 04714969085

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

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

蒙公网安备 15010502000194号

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

GMT+8, 2026-12-4 03:49 , Processed in 0.278129 second(s), 52 queries .

回顶部