1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| unit c;
interface
uses
SysUtils,Classes;
type
TCustomList=class;
TMFiler=class
private
FMParams :TCustomList;
FIndex :integer;
public
constructor Create(AList:TCustomList);
procedure Read(var AItem);
procedure Write(const AItem);
property Idx : integer read FIndex write FIndex;
end;
TCustomList=class
private
FList :TList;
FAlloc :integer;
FOnChange :TNotifyEvent;
FUpdate :boolean;
FChanged :boolean;
FMFiler :TMFiler;
function Get(idx:integer):TMFiler;
procedure Change();
public
constructor Create(AllocBy :integer);
destructor Destroy();override;
procedure Add(const AItem);
procedure Delete(idx:integer);
procedure Clear();
function Count:Integer;
procedure BeginUpdate();
procedure EndUpdate();
property Items[index:integer]:TMFiler read Get; default;
property List : TList read FList;
property Alloc : integer read FAlloc;
property OnChange :TNotifyEvent read FOnChange write FOnChange;
end;
implementation
uses dialogs;
{================================================================
TMFiler +
================================================================}
constructor TMFiler.Create(AList:TCustomList);
begin
if Assigned(AList) then
FMParams := AList;
end;
procedure TMFiler.Read(var AItem);
begin
with FMParams do
System.Move(Pointer(List[FIndex])^, AItem, Alloc);
end;
procedure TMFiler.Write(const AItem);
begin
with FMParams do
System.Move(AItem, Pointer(List[FIndex])^, Alloc);
end;
{================================================================
TCustomList +
================================================================}
constructor TCustomList.Create(AllocBy :integer);
begin
if AllocBy in [1..255] then
FAlloc :=AllocBy
else
raise Exception.Create('');
FMFiler :=TMFiler.Create(Self);
FList:=TList.Create;
end;
destructor TCustomList.Destroy();
begin
Clear();
FList.Free;
inherited;
end;
procedure TCustomList.Change();
begin
if not FUpdate then
try
if Assigned(FOnChange) then
FOnChange(Self);
finally
FChanged :=false;
end
else
FChanged :=true;
end;
procedure TCustomList.BeginUpdate();
begin
FUpdate:=true;
end;
procedure TCustomList.EndUpdate();
begin
FUpdate:=false;
Change();
end;
procedure TCustomList.Delete(idx:integer);
begin
dispose(FList[idx]);
FList.Delete(idx);
Change();
end;
procedure TCustomList.Add(const AItem);
var
NewItem:Pointer;
begin
GetMem(NewItem,FAlloc);
System.Move(AItem, Pointer(NewItem)^, FAlloc);
FList.Add(NewItem);
Change();
end;
procedure TCustomList.Clear();
var
i:integer;
begin
for i:= 0to FList.Count-1 do
try
FreeMem(FList[i]);
except
end;
if FList.Count <>0 then
FList.Clear();
Change();
end;
function TCustomList.Count:Integer;
begin
result:=FList.Count;
end
;
function TCustomList.Get(idx:integer):TMFiler;
begin
FMFiler.Idx:=idx;
result:= FMFiler;
end;
end. |
pour les entier
TCustomList.Create(SizeOf(integer));
pour les double
TCustomList.Create(SizeOf(double));
pour les type record
TCustomList.Create(SizeOf(TMatype));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
var
GList: TCustomList;
procedure TForm1.FormCreate(Sender: TObject);
begin
GList:= TCustomList.Create(SizeOf(integer));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
k:integer;
begin
for i:=0 to 100 do
GList.Add(i);
for i:=0 to 100 do
begin
GList[i].Read(K);
listbox1.Items.Add(inttostr(K));
end;
end; |
++++++++++++++++++++
Partager