Results 1 to 6 of 6

Thread: Problem storing method types as longwords

  1. #1

    Problem storing method types as longwords

    I want to store the pointer of a method inside a longword, like this:

    type
    TMyMethod = procedure of object;

    var
    Storage: LongWord;

    begin
    Storage := LongWord(@AnObject.ItsMethod);
    ..

    Later I want to retrieve it like this:

    var
    Method: TMyMethod;

    begin
    LongWord(@Method) := Storage;

    I have tested a code like this, and the method is called. The problem is that Self = nil. I suppose Delphi stores more than just the pointer to the method, it should also store the corresponding Self. How can I store both pointers in a type which is not TMyMethod, and how can I call the function to let it know which Self it should use? :?

  2. #2

    Re: Problem storing method types as longwords

    Quote Originally Posted by cronodragon
    I want to store the pointer of a method inside a longword, like this:

    type
    TMyMethod = procedure of object;

    var
    Storage: LongWord;

    begin
    Storage := LongWord(@AnObject.ItsMethod);
    ..

    Later I want to retrieve it like this:

    var
    Method: TMyMethod;

    begin
    LongWord(@Method) := Storage;

    I have tested a code like this, and the method is called. The problem is that Self = nil. I suppose Delphi stores more than just the pointer to the method, it should also store the corresponding Self. How can I store both pointers in a type which is not TMyMethod, and how can I call the function to let it know which Self it should use? :?
    Are you out of your mind mixing pointers with 32 bit unsigned integer types? that code WILL break on any non-32 bit platform, what's wrong with just using a pointer type and use that
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  3. #3

    Re: Problem storing method types as longwords

    Quote Originally Posted by Delfi
    Are you out of your mind mixing pointers with 32 bit unsigned integer types? that code WILL break on any non-32 bit platform, what's wrong with just using a pointer type and use that
    I don't think there is something wrong about it, both types have the same size SizeOf(Pointer) = SizeOf(LongWord). Also I'm compiling it with Delphi for Win32, and I don't intend the code to be multiplatform... there would be a lot of things I'd have to change, starting with DirectX 9.

    The method is being called correctly (I tested it), the problem is Self is not being passed. Any ideas?

    EDIT: HAHAH! Thanks Delfi, I just tested the sizes of those types to make sure my post was right, and found the size of a method type is 8, not 4. Using Int64 to store it should fix the problem. :lol:

  4. #4

    Problem storing method types as longwords

    A method pointer contains not only a pointer to a method but also a pointer to an object (self). If you cast it to Int64 you can get it to work but it's all implementation specific and may not work with other compilers and other versions of Delphi.

  5. #5

    Problem storing method types as longwords

    FPC is compatible with this (methodvars). But FPC also has 64-bits versions.

    Note that 64-bits Windows is here, and Free Pascal runs on Win64, and that also has DirectX.

    In this day of age this kind of typecasts is not smart, specially because there are afaik no speed benefits to be gained.

  6. #6

    Problem storing method types as longwords

    Take a look at the TMethod type in Delphi. As Mirage mentioned, a method consist of pointers to data (pointer to self) and code (pointer to method entry). Using TMethod makes it easier to do the type casting. Using it you can do something like this if you wish:[pascal]function TMyClass.ExecProc(aProcName: string): Boolean;
    Var
    Proc : procedure of object;
    Begin
    TMethod(Proc).data := Self;
    TMethod(Proc).code := MethodAddress(aProcName);
    Result := True;
    if not Assigned(Proc) Then
    Result := False
    else
    Proc;
    end;[/pascal]You can call published methods by name for example.

    Hope this helps.
    Jarrod Davis
    Technical Director @ Piradyne Games

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
  •