Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: Linking a procedure to an object for remote execution

  1. #21
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Linking a procedure to an object for remote execution

    Finally got finishing up with my test run. It seems to work rather well actually. So big thank you to JSoftware! (And of course everyone else)

    I had to use a wee bit of voodoo to get it to store the procedure addresses for later use though. No biggie however as it was easy as pi...

    [pascal]type
    TGUIControl = class(TObject)
    ...
    hasOnClick: Boolean;
    OnClick: TProcedure;
    procedure GetOnClick(proc: TProcedure);
    end;

    procedure TGUIControl.GetOnClick(proc: TProcedure);
    begin
    hasOnClick := True;
    OnClick := proc;
    end;[/pascal]

    So thats how I did it.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #22

    Linking a procedure to an object for remote execution

    Hmm why not just assign OnClick with nil in your constructor and then have
    if assigned(OnClick) then OnClick();
    when you want to call the procedure?

    Edit:
    I would do it like this:
    [pascal]
    type
    TGUIControl = class(TObject)
    private
    fOnClick: TProcedure;
    public//or published if you like
    ...
    property OnClick: TProcedure read fOnClick write fOnClick default nil;
    end;
    [/pascal]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #23
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Linking a procedure to an object for remote execution

    Yes, I supposed I could simply do that. However I wasn't sure that the Nil value would stick after some memory got moved around... much like floating point values shift and change on assignment.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #24

    Linking a procedure to an object for remote execution

    FPC is 100% compatible to delphi in Delphi mode (no need for @ altough using @ is much nicer IMHO, makes things clear).

    What you're describing are basic callbacks.

    Say you got:

    [pascal]
    procedure SomeProcedure;
    begin
    // user defined stuff
    end;

    var
    StoredProc: TProcedure; // defined in system, but you can type specific ones as you need
    [/pascal]

    An you want to assign SomeProcedure to StoredProc. If you use delphi mode you do:

    StoredProc := SomeProcedure;

    in FPC modes (e.g.: ObjFpc) you do:

    StoredProc := @SomeProcedure;

    If you use OOP, you need to use "procedure of object" type for the procedure variable e.g:

    [pascal]

    type
    TUserClass = class
    public
    procedure OnClick;
    end;

    TCallBack = procedure of object;

    var
    StoredProc: TCallBack;

    [/pascal]

    Assignment is then basically the same, you just use [@]object.method;

    You can check if it's assigned by checking the StoredProc for nil (no need to set it to nil in objects or globals, both FPC and Delphi nil their variables in these cases) e.g.:

    [pascal]

    if Assigned(StoredProc) then
    StoredProc();

    [/pascal]

    Notice the (). They are important especially if the StoredProc is a function returning something valid (a pointer would be a nice trap). To make sure the compiler stores the result of the function CALL instead of it's address (in delphi mode only, FPC has it clear) you need to put the () there even if it doesn't take address.

    For example:

    [pascal]

    type
    TStoredFunc = function: Pointer;

    var
    StoredFunc: TStoredFunc; // let's say it got assigned elsewhere
    p: Pointer;

    begin
    if Assigned(StoredFunc) then
    p := StoredFunc; // error in delphi mode, p will contain address of StoredFunc
    end;

    [/pascal]
    Feel the power of Open Source.
    <br />Feel the power of Free Pascal.

Page 3 of 3 FirstFirst 123

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •