偶在一个老外的文章的基础上写的,应该很有用的说,
以前总是用STL的vector做替代,但是STL的用法实在不是很习惯
- v. G, I9 y2 t6 I& T! \& C
//-------------------------------------------------------------------------
/ c: z f, o% ^8 u/ T* G4 a& e/ d#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
//-------------------------------------------------------------------------
! f- G6 b' F5 |5 |#include <classes.hpp>
//-------------------------------------------------------------------------
8 h7 U( v( g9 O; _4 W7 ]template <class T>
class TTypedList : public TList
{
$ q. N5 s5 I9 [0 a$ O3 vtypedef TList inherited;
private:
8 d5 g" Q3 [" l. E/ Bbool FAutoClear;
3 c7 M0 ~6 [# h$ f& YTClass FItemClassType;
protected:
; P+ r/ `# A& [, A1 e: h+ vT* __fastcall Get(int Index) const
{
return (T*)inherited::Get(Index);
) m* Z4 V a- I% t0 r}
void __fastcall Put(int Index, T* Item)
+ M. S" C2 q; ]4 x- Q w6 g/ ]; ]; |{
1 }! X6 t7 [+ r$ K3 { inherited:
ut(Index, Item);
}
public:
__property T* Items[int Index] = {read=Get, write=Put};
# |3 \, @( e( d/ N7 {: {9 N* [public:
! [9 r9 o! R2 ]__fastcall TTypedList(bool bAutoClear = true)
2 b8 \5 x6 Q% s0 ~5 N1 l- _: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
{ }
// no destructor needed
. R4 _3 H% F* ? r: o. rint __fastcall Add(T* Item)
- I$ I$ O+ E2 N+ V- z6 t3 Z{
g5 V/ C5 Z+ N" g- Q6 L4 V+ q. \return inherited::Add(Item);
}
, q+ Y+ Y/ Z4 B+ K; Mint __fastcall AddList(const TTypedList& List)
{
if(this == &List)
4 f' d+ Q: A( V) i- T{
throw EInvalidPointer("Can't add a TTypedList to himself!");
/ G! U7 v' D. w6 ]" ^5 t2 ~1 Q7 R/ ]}
4 D' u0 D" j/ |: Y( W0 e9 T4 zif(FItemClassType != List.FItemClassType)
{
: ^& [4 X( ~6 r, x: ^% p' Nthrow EConvertError("Can't add a TTypedList with a different ItemClass
3 @% }& u" S% i1 HType!");
7 W0 u4 E- M$ `/ X* M7 l}
B/ U$ t1 |) X* c7 Xfor(int i=0; i<List.Count; i++)
{
Add(List.Get(i));
$ K: g6 x) H7 {% u/ G& H/ q}
6 \+ u& i5 I. D: L. h+ D/ @& Creturn List.Count;
' L0 P, ~# q v9 ]- W}
" g' d" q6 J7 e: N# l$ U& `. Vvirtual void __fastcall Clear(void)
, F3 b8 K* K q) @1 z3 m6 F0 {{
1 Q h& \0 y& i! B3 c3 t, Dif(FAutoClear)
( n; T4 Q. Y7 J2 g2 S7 U{
for(int i=0; i<Count; i++)
{
delete Get(i);
}
, I1 C) y1 X* I" ^% L8 n}
inherited::Clear();
}
void __fastcall Delete(int Index, bool bAutoDelete = true)
{
if(bAutoDelete)
{
delete Get(Index);
+ g: X; ]: r6 e8 A# C# f}
inherited:
elete(Index);
}
K# b4 y0 _' R6 W2 W+ p% _T* __fastcall First(void) const
0 U9 b9 B/ c% t- B7 t+ z0 |* W& \% h{
) ?) K" K3 q2 I" [, S2 O5 N: jreturn (T*)inherited::First();
}
int __fastcall IndexOf(T* Item) const
{
7 ]* K, y) _2 {9 A7 F! \return inherited::IndexOf(Item);
, A; o+ K2 ~( ?}
& p W2 r% f; _( T; z; t; r7 V, Fvoid __fastcall Insert(int Index, T* Item)
{
inherited::Insert(Index, Item);
}
8 L1 l5 w! Z' g; I; u- Pbool __fastcall IsEmpty(void) const
{
return Count == 0;
}
T* __fastcall Last(void) const
{
return (T*)inherited:
ast();
}
0 j5 s L+ I a; Y* ]8 Iint __fastcall Remove(T* Item, bool bAutoDelete = true)
4 p, [# }) e) N' e. q! [+ o{
: F+ v' f2 m' h2 n$ ~int nIndex = inherited::Remove(Item);
if(bAutoDelete && (nIndex != -1))
{
delete Item;
1 v" L$ {7 t$ F" K- ?/ @4 E}
- `8 ]) N+ v* ]9 C/ {return nIndex;
' |; Z* A/ f- J}
void operator<<(T* Item)
{
% Y7 ]- ?4 B: {3 uAdd((T *)Item);
# H2 r! f' }2 a* m: s# v$ c}
void operator<<(const TTypedList& List)
& ^/ C$ i9 t+ Q- t+ s9 g$ s{
- b# z2 g8 v0 r# q3 q1 ?( OAddList(List);
}
}; // end of TTypedList
//-------------------------------------------------------------------------
8 b) W7 S- T5 _) j& @ q#endif
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |