QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

& R. G& c* G0 `4 H

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

9 o" {; p3 v2 J

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

! ]* Q1 u7 `3 y$ x: m6 P

* c6 k! ^' y1 w8 z

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

6 p* ^. W- m( k! I4 k

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

* j# A$ l2 T7 a; R; w

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

5 ]+ q0 v! F- O5 e5 G" n1 k7 O

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

" R+ p: W8 |8 _ c; Z+ ]0 ^% U! u) L7 k

#include <classes.hpp>

- b+ z2 t! Y2 \7 f L3 q/ H& a

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

) t# v8 W* d- A, B. Z+ f& N( s5 M9 Z

template <class T>

# D8 \* F* w' W) Y1 ~3 [) \1 T' O/ z

class TTypedList : public TList

; m* H N' Z' q9 j) V f

{

4 e6 K1 |% @' u* A4 m8 {# E3 V8 E

typedef TList inherited;

! o; @; Z3 B8 j( z$ _8 D" m

private:

9 ~7 d3 T, D$ C( @: D, g1 u- g

bool FAutoClear;

# I5 F+ l9 q5 P

TClass FItemClassType;

' |: @) |5 U* H$ m& Y

protected:

0 A8 o% Q0 O: L4 V

T* __fastcall Get(int Index) const

v: P0 ^. i- s. r3 W

{

" k( Z; J2 @# `- h$ _+ P4 O8 x- l( ^

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

& m5 x' z" Y: d& G! g9 E' X

}

5 {7 R7 u/ G! ?! r: s

void __fastcall Put(int Index, T* Item)

5 j* Z" |2 ?9 y) q

{

* k2 h3 {: U& z" y2 e/ l, s

inherited:ut(Index, Item);

: W, J/ }1 y- b' H

}

+ N" Y* K1 e$ |+ o

public:

2 |" p( r/ [0 m: a O& v/ V

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

7 Q$ m* H" \& ?

public:

4 A9 p8 @/ x: Y) F3 x

__fastcall TTypedList(bool bAutoClear = true)

. v y, L8 a. j. n

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

9 e0 H7 b7 C( H

{ }

: C2 e) B" S; h0 C) F

// no destructor needed

( u( X6 ~ R {$ e$ L

int __fastcall Add(T* Item)

/ c u9 f3 Y9 X- n1 e

{

! E3 e! c( y6 K

return inherited::Add(Item);

" C# h' y! F1 V/ b: d0 \

}

/ {; f' q: \5 Z3 Z3 T& f% h. P# `

int __fastcall AddList(const TTypedList& List)

& A1 [# T% ~6 s: e' |- f

{

. ~5 w7 n0 U9 N9 @" a6 o

if(this == &List)

1 v G" g* `% `/ H

{

3 M$ y1 F' ]3 g4 B# p) I

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

2 c9 @& X; t4 h9 i" q

}

: C% O0 |' x1 u- ]

if(FItemClassType != List.FItemClassType)

+ k1 `/ u& }5 v

{

% g9 q% g5 l$ @+ s$ Z

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

, l4 i H, y% V% k

Type!");

$ q$ u8 Z ~4 S- ^

}

; d! I0 a5 R. I" \& L$ B4 j

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

' w0 x" u7 P) Q: Q! h( @* y6 a

{

/ C; C. ] E5 R& t" v$ X

Add(List.Get(i));

3 s, n" v5 T$ Y1 v# N/ L: _& j; D

}

+ y- o/ J( F5 E

return List.Count;

1 B' ~5 j/ S0 Y! u$ }# ?

}

8 d/ U5 S5 [7 P, O

virtual void __fastcall Clear(void)

& g2 A8 x; c# u. g0 y' o2 E' f: x

{

0 i1 `. l- V. H

if(FAutoClear)

5 O" G( A( s7 {- ]/ |1 n. t9 R4 ?8 z

{

N! k" d/ j2 ~$ e

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

7 _) G2 J1 e+ T8 u+ X) n

{

q7 I& F% K! a h7 Z* T

delete Get(i);

& b! o2 l1 |8 w" L2 D

}

6 E" W2 v" G5 \/ u0 q

}

' h. a' W' R- S1 x4 ]& v( m% a

inherited::Clear();

. T+ V& ?7 e0 V

}

5 q- v* K0 l! E, I! ^$ m$ W

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

q u' s+ m8 {9 @

{

( ?. d" K$ Y* r f

if(bAutoDelete)

' e7 K. D7 S/ c) y1 I0 r

{

6 k& S N- j5 c) {% f0 k

delete Get(Index);

, e4 C4 Q. k7 N8 m. H

}

6 S) L! N, I$ p0 G, P

inherited:elete(Index);

& V( ]# _4 c) U! L; N

}

0 i+ z2 X5 s7 i' {& e v X6 k

T* __fastcall First(void) const

+ e- w" M9 v9 i6 K( |, h

{

- n8 ^( G6 k& Z! Y6 V

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

# |/ S9 X* q9 ?: ]

}

: ^1 K) d R: E. A

int __fastcall IndexOf(T* Item) const

2 ?! d7 b* ?% H

{

% r0 U; Z% t& s

return inherited::IndexOf(Item);

4 c5 f0 i$ f' G

}

/ ~: @% {; i' N

void __fastcall Insert(int Index, T* Item)

$ d/ _6 c. ?6 d8 S/ g4 z

{

( m1 k' o! E/ V E% Y

inherited::Insert(Index, Item);

" |9 b" X5 v- j- v

}

4 c( O ^4 j5 a% B+ t

bool __fastcall IsEmpty(void) const

( [( G3 b, {: R$ U, R

{

7 E' I# D( i7 {% B& z4 f2 G; G

return Count == 0;

# o! b0 w$ h# V0 K

}

4 A$ ? x* J2 q4 k* o' D

T* __fastcall Last(void) const

, A8 R$ p6 f% s I4 |

{

' H0 H: g; f9 R W: {8 Y

return (T*)inherited:ast();

. x; S" r4 f8 `4 O) F3 U8 Z

}

( K9 @! i, t9 C; S! X1 \9 H

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

6 ]1 Z8 S3 D6 l/ E k( B

{

, u8 o2 n; T/ K1 G9 E7 t: u

int nIndex = inherited::Remove(Item);

. e7 B9 }3 W: l' s: }' G

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

: j7 E9 d& _6 @( g

{

$ t8 N3 Y4 L5 d' R2 ?. j$ Z; _4 N+ D, p

delete Item;

2 J5 y4 v- x U2 ^0 }/ K j

}

3 L* \: H& n$ ]2 R1 w) u! P

return nIndex;

7 p0 J v, u/ ~8 T

}

' a5 ?, g+ G: |) Q, |3 ~

void operator<<(T* Item)

( w* N1 ?! w9 S" \$ p* ^$ S! L

{

( {; j6 o( U; K, c8 U; W& I

Add((T *)Item);

$ x5 @ k0 l4 ~

}

0 c. U$ l& T4 l+ J& b# }

void operator<<(const TTypedList& List)

# T- ]: u n; o# I 0 M, w6 D: e, o+ |$ y. F! _

{

! `% v1 l( E {. q* H* }

AddList(List);

C6 m+ k; g& x7 O2 M

}

L) A# t* p/ I* _! Y5 S6 o% R

}; // end of TTypedList

: L4 {. O) i0 e. v) N [- U, u2 L- n

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

8 s1 P% D6 S$ _

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

回顶部