Muss man denn auch den ganzen ListHelper nachstellen?
Ich dachte eher an sowas:
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Collections;
type
THimiBeforeNotifyEvent<T> = reference
to function(Sender: TObject;
const Item: T): Boolean;
THimiList<T> =
class
strict private
FList: TList<T>;
FOnBeforeNotify: THimiBeforeNotifyEvent<T>;
public
constructor Create;
destructor Destroy;
override;
function Add(
const Value: T): Integer;
{$IFDEF Release} inline;
{$ENDIF}
property OnBeforeNotify: THimiBeforeNotifyEvent<T>
read FOnBeforeNotify
write FOnBeforeNotify;
end;
constructor THimiList<T>.Create;
begin
inherited;
FList := TList<T>.Create;
end;
destructor THimiList<T>.Destroy;
begin
FList.Free;
inherited;
end;
function THimiList<T>.Add(
const Value: T): Integer;
begin
Result := 0;
if Assigned(FOnBeforeNotify)
then
begin
if FOnBeforeNotify(Self, Value)
then
begin
Result := FList.Add(Value);
end;
end;
end;
procedure Main;
var
HimiList: THimiList<Integer>;
begin
HimiList := THimiList<Integer>.Create;
HimiList.OnBeforeNotify :=
function(Sender: TObject;
const Item: Integer): Boolean
begin
Result := (Item
mod 2) = 0;
end;
try
HimiList.Add(1);
HimiList.Add(2);
HimiList.Add(3);
HimiList.Add(4);
finally
HimiList.Free;
end;
end;
begin
try
Main;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
end.
Zugegeben, das wird bei Insert/AddRange() etwas fricklig, aber sonst?