( `8 ?! f1 o, j6 j. L* h" ?
偶在一个老外的文章的基础上写的,应该很有用的说,
: h/ E# P# W) X# h% E. Q' S以前总是用STL的vector做替代,但是STL的用法实在不是很习惯
) w0 J) U/ W4 {+ H9 c# g* x0 a
//-------------------------------------------------------------------------
6 ?' h7 A* A3 P#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
//-------------------------------------------------------------------------
0 [9 w; H9 }/ \- C3 }& |#include <classes.hpp>
//-------------------------------------------------------------------------
2 x% L- K v# a+ Ytemplate <class T>
* m8 j' ~0 o/ x3 U4 t! Mclass TTypedList : public TList
) \6 {$ p" y2 C% u3 O5 v/ u* {! p{
; x, U, h9 d7 O; o& wtypedef TList inherited;
private:
% n0 ^9 K- V$ S" vbool FAutoClear;
TClass FItemClassType;
protected:
$ u3 `' D4 W& M3 FT* __fastcall Get(int Index) const
{
! s/ Z' e2 o) hreturn (T*)inherited::Get(Index);
: t# C3 \! l6 j! v* U: V# D1 }}
8 M1 s' z {2 m7 a& @void __fastcall Put(int Index, T* Item)
5 E0 O4 A, E& x9 C0 b) Q{
3 B3 E" }7 L2 S, b inherited:
ut(Index, Item);
}
) n6 V1 d2 V. E5 @# q, Cpublic:
__property T* Items[int Index] = {read=Get, write=Put};
public:
__fastcall TTypedList(bool bAutoClear = true)
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
' I6 y! K+ g+ n{ }
// no destructor needed
int __fastcall Add(T* Item)
% M% H# k( n5 ?1 E( ~{
return inherited::Add(Item);
, a6 j6 G& R: Z8 N}
8 q. N4 D4 v l6 S3 vint __fastcall AddList(const TTypedList& List)
1 r- x5 D+ f: I- P- q# O{
% }, V- _5 P, S+ Y' K5 {if(this == &List)
1 Z/ f& k3 R2 \, p, p7 X{
3 l: R o% Z1 R+ H0 ~throw EInvalidPointer("Can't add a TTypedList to himself!");
1 V4 I/ K0 G3 q+ i3 ?% L. p9 L: V}
if(FItemClassType != List.FItemClassType)
{
" p. _* }; M# E, t( r% f/ [& l6 Jthrow EConvertError("Can't add a TTypedList with a different ItemClass
+ w- t0 I0 c1 O3 a5 O+ aType!");
, N! r) g4 C1 ?- s% b. _. f}
for(int i=0; i<List.Count; i++)
: v; c2 i# k1 _{
! y& m, U4 y% D2 R" _Add(List.Get(i));
}
' b) l" x u1 F1 Z: mreturn List.Count;
}
$ N9 ~5 }0 m# bvirtual void __fastcall Clear(void)
3 _ ?, a# G% }; u% S2 O/ p{
if(FAutoClear)
2 Z0 W m$ i+ e9 _* Z! ?8 E{
, P" a& W$ Z7 U- a7 ~9 ofor(int i=0; i<Count; i++)
2 h' b* l8 m: b& w9 v- n{
delete Get(i);
* z' x7 U8 c3 s$ @6 J}
1 p0 @' O" c @7 I. r}
inherited::Clear();
9 m; F2 F, \6 ^3 t6 n4 E}
4 Z9 x, z- G2 J; N6 h/ ]1 Rvoid __fastcall Delete(int Index, bool bAutoDelete = true)
{
6 `6 [4 H$ ~7 W) X" N8 \) F# Hif(bAutoDelete)
6 b7 ~7 X" W+ c/ Q8 i& K$ b{
delete Get(Index);
}
inherited:
elete(Index);
}
' V" Q3 L9 V- K% w7 E3 n( hT* __fastcall First(void) const
1 k# Q4 `5 t& ]% m Z{
; W+ `; B' ~$ x6 z Dreturn (T*)inherited::First();
" o( q: ^# |. c! [- k" g0 M" c}
* V8 a! i5 J- Iint __fastcall IndexOf(T* Item) const
+ U& o4 |! E0 ]* S, C" j{
return inherited::IndexOf(Item);
}
: c/ q, T" P/ q4 R' ?. L/ g; E/ W2 nvoid __fastcall Insert(int Index, T* Item)
" B a- _9 ?$ v$ g- o* D8 o/ C! z{
" e* V0 t1 I/ m2 minherited::Insert(Index, Item);
, ] O+ `3 k* E9 b}
bool __fastcall IsEmpty(void) const
8 G3 ? g7 S m7 q: Q2 P{
3 q+ p( A) d) K7 }! xreturn Count == 0;
1 t5 e4 \8 j& j/ [}
" s: r6 V4 F+ O2 M& @T* __fastcall Last(void) const
& |- ~1 i4 R9 \; k3 v: c{
return (T*)inherited:
ast();
}
int __fastcall Remove(T* Item, bool bAutoDelete = true)
9 ~/ \% F% @+ o2 P7 g{
int nIndex = inherited::Remove(Item);
if(bAutoDelete && (nIndex != -1))
{
) Q: m+ v; f4 D4 C5 G+ {- I6 Sdelete Item;
}
return nIndex;
}
void operator<<(T* Item)
{
- G, w% i2 j, Y- c" V# G& j$ C3 bAdd((T *)Item);
. l% a: H5 C9 f# N* m9 b4 w$ }}
void operator<<(const TTypedList& List)
# F( ^# T7 \: t{
AddList(List);
% w& H0 }6 ~7 k* r5 p- [& F}
4 ~2 A" K9 M# M* y- y}; // end of TTypedList
//-------------------------------------------------------------------------
#endif
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |