偶在一个老外的文章的基础上写的,应该很有用的说,
3 Z4 l" ]; ]$ J" z, o* g! p/ f以前总是用STL的vector做替代,但是STL的用法实在不是很习惯
//-------------------------------------------------------------------------
#ifndef TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
: c* K. J/ C, [* Q/ S+ g#define TypedListH_0CBFE2E8-E7C5-4D88-9844-1F177F4B00E4
4 K7 a/ H( e' o" a% j//-------------------------------------------------------------------------
! ^# ^6 S0 H; Q6 Q `2 J, }#include <classes.hpp>
//-------------------------------------------------------------------------
template <class T>
/ [5 @. ]3 @& rclass TTypedList : public TList
{
typedef TList inherited;
6 \' @" ?0 y& R, c- y4 {) oprivate:
& X& v: f- V4 \" ]6 r* ]( {( `bool FAutoClear;
( X5 O4 u$ k* K [TClass FItemClassType;
protected:
! D, |& S3 V& I6 FT* __fastcall Get(int Index) const
, d3 }( w: ^4 n* N0 `" E{
X# x' w# e" n) k7 Jreturn (T*)inherited::Get(Index);
V% ?- Q$ I0 R! w: z}
void __fastcall Put(int Index, T* Item)
6 U9 I8 Y: s9 `4 W# D1 I{
inherited:
ut(Index, Item);
}
9 l* Y- E2 U, l8 zpublic:
__property T* Items[int Index] = {read=Get, write=Put};
public:
. ]8 S. F u3 {6 {$ z2 f9 r' x* Z__fastcall TTypedList(bool bAutoClear = true)
: TList(), FAutoClear(bAutoClear), FItemClassType(__classid(T))
$ `/ a7 }) V. ^+ m: ]+ _{ }
. M0 ]3 y7 \6 p8 |* f// no destructor needed
int __fastcall Add(T* Item)
{
return inherited::Add(Item);
( [; h- X C: ]! `}
6 B9 o: F) O8 ~) y- z2 Y; \6 b+ Dint __fastcall AddList(const TTypedList& List)
6 U: q! _- ?/ G4 t3 t& C3 f2 Y& H{
if(this == &List)
{
throw EInvalidPointer("Can't add a TTypedList to himself!");
/ A" U( J ^2 a, t$ B( ~2 I}
if(FItemClassType != List.FItemClassType)
{
throw EConvertError("Can't add a TTypedList with a different ItemClass
2 b8 \9 T9 V7 N4 ^Type!");
}
/ O% M p5 ^8 Lfor(int i=0; i<List.Count; i++)
- M# K5 i9 x" D% q6 P{
Add(List.Get(i));
6 x! q! Y S0 G# R- D: {' T}
return List.Count;
}
j, q- @. p! }6 ivirtual void __fastcall Clear(void)
) n Q' l( O& u9 ~{
1 E- B# f. f7 T4 |7 H( E7 kif(FAutoClear)
" _$ H' x2 s" n9 r Q- C" V{
! `/ j5 j. b* M% H8 l! I- r, cfor(int i=0; i<Count; i++)
{
9 I1 b+ c0 x) B; }! \1 w, Y4 `delete Get(i);
' A! `2 J: y7 y' L' r6 {}
}
inherited::Clear();
" a0 [7 N L+ t3 K/ q: T) N5 H}
- l8 c2 ]7 L/ i* o; l3 e& b: b; avoid __fastcall Delete(int Index, bool bAutoDelete = true)
7 e# [8 e8 P8 O{
if(bAutoDelete)
3 W0 y( Z$ g1 c# [{
delete Get(Index);
" H: y' W8 D& ^: T}
inherited:
elete(Index);
}
2 q p# y0 s. M5 a. B5 S% a9 dT* __fastcall First(void) const
{
1 }$ Y% O( P: ]! b) `" [return (T*)inherited::First();
}
int __fastcall IndexOf(T* Item) const
( S, e& E8 |: z- v{
return inherited::IndexOf(Item);
; s( H1 K/ @2 r) I}
2 A# A/ L- w8 p( e; Xvoid __fastcall Insert(int Index, T* Item)
, l" s: l1 R# O2 Z# l3 [{
inherited::Insert(Index, Item);
}
]4 }4 z" V' p1 X' Mbool __fastcall IsEmpty(void) const
: C9 q& J* ?6 A{
7 D: Q7 K ]9 o$ e u1 wreturn Count == 0;
9 E0 k& _2 o; K4 N}
T* __fastcall Last(void) const
9 l$ G$ E) [5 h{
; j4 S. \8 n }* e3 e return (T*)inherited:
ast();
}
int __fastcall Remove(T* Item, bool bAutoDelete = true)
{
int nIndex = inherited::Remove(Item);
if(bAutoDelete && (nIndex != -1))
{
5 x9 k0 F0 u# ]delete Item;
! ~' z/ O$ t- t% Y) u9 }}
# F7 l) ?; x* ?$ Q3 Q3 O, zreturn nIndex;
( L$ Z3 T: y2 ~: A; Q2 K}
void operator<<(T* Item)
+ a( j/ [' e, _) ?{
Add((T *)Item);
$ g' g1 P" A' H2 r3 Z- P}
void operator<<(const TTypedList& List)
& K$ Z9 t% ?, D) _/ R' l u% F+ y{
AddList(List);
' e5 B$ i! ]2 U7 W& {}
! d% p+ H3 Q0 M* t}; // end of TTypedList
# c6 P3 t7 _1 r//-------------------------------------------------------------------------
: m5 q1 G' Q& h" O3 {) g% l) M#endif
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |