Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


ID: 27673, Offset Type casting for methods with generics

by Jim Ferguson Email: Anonymous


Call a record method with any instance of a record, using a complex but simple generics record method wrapper.
Download Details
FTP  download also available
CDN Login Required to Download. (You will be redirected to the login page if you click on the Download Link)
To download this, you must have registered:
A free membership

For Delphi, Version 12.0  to 13.0 53 downloads
Copyright: No significant restrictions


Size: 537 bytes
Updated on Sun, 11 Apr 2010 07:04:25 GMT
Originally uploaded on Sun, 11 Apr 2010 06:32:59 GMT
SHA1 Hash: 07D6AF3D9797DA03701D218C82EFA06E81C169BC
MD5 Hash: 0A66EEFEB689B34F127D51F097619004

    Explore the files in this upload

Description
So what this code does is allow you to capture the method of the record then easily call it with type checking for a different instance of the same record type.

The following is the code, but the attached file is formated better.

program RecFoo;

uses
Classes,Forms;

{$R *.res}

type
TRecordMethod<V,R> = record
public
Value: V;
function _(const Base: R): V;
procedure Init(AValue: V);
// class operator Implicit(Value: V): TRecordMethod<V,R>; Doesn't work
end;

{ TRecordMethod<V,R> }

procedure TRecordMethod<V, R>.Init(AValue: V);
begin
Value := AValue;
end;

function TRecordMethod<V,R>._(const Base: R): V;
begin
Result := Value;
TMethod(Pointer(@Result)^).Data := @Base;
end;

(* Doesn't work
class operator TRecordMethod<V,R>.Implicit(Value: V): TRecordMethod<V,R>;
begin
Result.Value := Value;
end;
*)

type
TRec = record
procedure Foo(Sender: TObject);
end;

{ TRec }

procedure TRec.Foo(Sender: TObject);
begin
end;

var
RecFoo: TRecordMethod<TNotifyEvent,TRec>;
Rec1,Rec2: TRec;

begin
RecFoo.Init(Rec1.Foo);
RecFoo._(Rec2)(Nil);
end.






   Latest Comments  View All Add New

Move mouse over comment to see the full text

Server Response from: ETNACDC03