I wrote a anonymous event creator. I would like to ask if anyone can tell me if I did it the "right" or "best" way, or how I could make it better.
Here is what I did:
Declaration:
TAnonymousEvents<TTEventPointer,TTEventReference>=class
public
class function Create(AEvent:TTEventReference):TTEventPointer;
end;
Implementation:
class function TAnonymousEvents<TTEventPointer,TTEventReference>.Create(AEvent: TTEventReference): TTEventPointer;
type
TVtable = array[0..3] of Pointer;
PVtable = ^TVtable;
PPVtable = ^PVtable;
begin
TMethod(result).Code := PPVtable(AEvent)^^[3];
TMethod(result).Data := Pointer(AEvent);
end;
Usage:
.....
type TNotifyEventReference = reference to procedure (Sender: TObject);
.....
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.OnClick := TAnonymousEvents<TNotifyEvent,TNotifyEventReference>.Create(
procedure (Sender:TObject)
begin
ShowMessage('it works!');
end
);
end;