QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

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

; e( E v- S F

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

" C8 ~3 I8 y- S

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

& u4 d* D- q: P, b$ _! u! a+ e

3 q$ M. V2 l3 ?% f3 X8 H

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

" O, a# M; v0 w; B. B) Z

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

. K2 N+ h' x) z! m

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

; d, [. l5 {' S' ?1 m% j* b' I

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

( |/ C/ C2 [1 ]* W' m/ s

#include <classes.hpp>

( X% d. Q8 P9 _$ H d1 ~

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

( E4 B8 m. e6 H- t; U

template <class T>

- V4 @) M3 r0 X

class TTypedList : public TList

# c8 V/ n* q4 L( u! V5 S7 j2 y

{

& h, H5 G. [' ^; H' }' }

typedef TList inherited;

5 [3 [7 X1 Q0 o; b- q/ E0 c

private:

# x# S+ s9 ]2 @: t5 _. r

bool FAutoClear;

/ e0 b5 X1 ?. n+ R @ D

TClass FItemClassType;

2 }0 V. r- ?% i

protected:

5 v+ c- g( i1 {% f( \

T* __fastcall Get(int Index) const

2 T) A- `4 A# ? E- h

{

) O7 d6 w1 z3 X m, T

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

# X0 r+ l4 u( A6 d) `0 S( n

}

+ M: H2 c9 j6 W8 X1 E3 r- k/ Y

void __fastcall Put(int Index, T* Item)

R7 t5 `7 Q5 o ^! Z$ v8 Z+ ^" }: \

{

3 f; O& _& B+ g5 J

inherited:ut(Index, Item);

2 }3 h3 R- \$ {' a

}

+ W1 Y. g/ w9 X

public:

9 ^- t' g: p+ @: t. u- U, N

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

" w* W8 `2 L4 h+ \9 T3 c

public:

5 N t {5 _$ e" ] o. q/ [+ i/ M

__fastcall TTypedList(bool bAutoClear = true)

% H; T1 q) M! B$ k; }1 {

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

9 m! q8 i0 V7 z% y

{ }

& e: D; Y* [( N7 s* \

// no destructor needed

/ k8 `2 J) f# B: l

int __fastcall Add(T* Item)

* f+ p' j( W% d$ J7 J

{

: M" m( F7 R% \# Q

return inherited::Add(Item);

% E6 W9 e/ q/ Q# l+ a

}

P, d: x3 A4 I) ^/ L6 d. K( `

int __fastcall AddList(const TTypedList& List)

7 ~/ h( _2 f0 X( d& s0 ~9 a% R

{

* }# V# N( ^! ]9 H% l6 u- w

if(this == &List)

, I5 B- a% Y6 u7 A- @) J% i1 f

{

' z+ }+ U2 b. P* O' `' H7 w3 ^

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

; u' i. c! {5 E0 t/ B7 t4 {, u

}

! B$ S; Y7 j% B) g6 S

if(FItemClassType != List.FItemClassType)

9 |' X6 \$ v6 ~; y" G- g8 f

{

+ L" |; |' D; v% Y$ M7 I

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

7 S" i4 v7 X: g

Type!");

% P' ]; u$ g+ z5 Q

}

7 _0 i8 I. y4 n: S. M' g. B6 ~

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

/ Z1 ^5 `/ U: k0 F# k1 P; v8 K w

{

; X! K8 j( O/ K

Add(List.Get(i));

7 P" h0 V, ^# H) W* i) j. M

}

$ j& B' X# L$ ^$ ]4 `" B

return List.Count;

: Q) G. e+ l9 I

}

% v. j/ Z! b- g4 @# f6 G, L: ?

virtual void __fastcall Clear(void)

6 e: R$ N, V: {9 }6 n) W2 \

{

! ^6 m* \# F+ _) j& [

if(FAutoClear)

) v& n }5 }9 O) n+ r

{

. l7 Z4 [' V' R f

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

+ z7 f8 v. s, v2 F- Z

{

1 }) v9 {2 q" N$ [( t. M5 a" A5 Z

delete Get(i);

+ z `7 H S- p( r

}

& W i0 m0 y1 d% L* @

}

`0 D0 Z& s# z

inherited::Clear();

. x* t W& ^* C, }5 Y! v

}

/ d! u) {- b' R0 N E

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

( _) X% B( ]6 z7 d# f

{

( ^9 s* x( k3 r* I) |) u5 f

if(bAutoDelete)

* t8 T+ i5 K, W+ L J8 \2 k9 ?

{

/ Q# J. R e( p9 ^0 V4 \

delete Get(Index);

3 h4 C( W# M4 x& [0 p1 x/ }

}

6 h" x) P% l* B0 K* |: o G& ?

inherited:elete(Index);

6 }$ }3 R7 r) k5 D9 L

}

- I# W. k+ h3 J& z; o0 _) u

T* __fastcall First(void) const

& s! G! O% K0 o2 @/ {

{

; k" R, S% {! n! E `

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

. }+ k9 w$ I' I* c$ R$ b

}

+ }4 T4 D) ^4 Z+ a" ?

int __fastcall IndexOf(T* Item) const

0 [. g8 ^7 j! A6 _" l8 z: i

{

`0 M4 @" L$ M) p0 ]) n

return inherited::IndexOf(Item);

/ ^0 f2 W& \: I0 B' q! g9 X# u

}

/ Y" E/ m- r K( i* R

void __fastcall Insert(int Index, T* Item)

5 r! X2 a. b6 j2 `7 n1 i

{

1 E6 d [5 x9 @/ O; `

inherited::Insert(Index, Item);

* e5 S2 m, x% c K5 n* K9 D# k

}

/ q7 o. G. l P( p. d O

bool __fastcall IsEmpty(void) const

5 t7 t- F. e7 x: o) Q- k

{

. q7 I3 ]6 ~7 p& a& ?

return Count == 0;

% a$ E8 ^8 U7 f4 l2 D

}

% p( e, m& V- S# N. | R6 Y

T* __fastcall Last(void) const

8 g7 m3 B9 n- @3 p! w" O; \( x

{

- c2 g/ J; `/ j4 ^8 {/ n

return (T*)inherited:ast();

% E" z6 u" l+ Q

}

" O C9 A w. `0 |9 y% s

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

( D0 V1 L3 F& J' u: ~! U: [3 ]

{

3 S& B' I' d+ e

int nIndex = inherited::Remove(Item);

& i1 ?( X% ]- {+ s- a, O- {9 t& F

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

k) r1 S9 [, x6 J! t0 ^

{

* z- O+ u6 s+ s7 A- J) o" u

delete Item;

8 p# v7 o+ H8 W5 s7 F, K) o, A

}

* @/ ^+ X, [, U4 d7 O% j

return nIndex;

8 D1 A9 P. C' `2 e" h2 k5 ^

}

; ?# l. T% H4 m0 S- y3 A& d

void operator<<(T* Item)

. T0 l* }+ b# ]# E7 m1 z& x

{

" R/ ~3 P, r- T- a( D9 M

Add((T *)Item);

2 d8 a7 t. L2 Y6 `" j" X+ C

}

! P7 {0 l- c+ X% J9 E

void operator<<(const TTypedList& List)

8 a. H$ @& _5 e 3 B/ P% U9 E' f& e5 t0 K

{

: ^$ M: H* B- H1 e: `2 O+ }

AddList(List);

2 `/ U8 k# q0 k1 Q( i

}

3 O$ v" L- [9 l5 }! B0 W

}; // end of TTypedList

" e$ X4 U7 e, f& S' O9 z. Y" s

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

4 o5 P6 b% y) b y

#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-15 20:15 , Processed in 0.427974 second(s), 57 queries .

回顶部