Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Pointers, objects, functions and execution - is this even possible?

  1. #1
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45

    Pointers, objects, functions and execution - is this even possible?

    Theoretically a pointer is a memory adsress to something in your program; and a function/procedure is stored in memory too. (I think you see where this is going by now). So is there any way to do the following:L

    Code:
    object
       Task = object
          MainProc: Pointer;
       end;
    
    var
       TT: Task;
    
    procedure foo();
    begin
       writeln('HI');
    end;
    
    begin
       TT.MainProc := @foo;
       foo();
    end.
    However I cannot even get this to compile. Theoretically it should work but I have my reservations. I heard you can do this in C or whatever - so I was wondering if it could be done in pascal - since it would make my life sooo much easier. Think about it:
    1 engine manages tasks
    each task has init, main and end procedures and etc
    you would not have to worry about modes and etc - just register your 'task' and let it all run. or something like that.
    any pointers folks? (excuse pun)
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  2. #2
    Generally, the way you do function pointers in Object Pascal is to declare a Function or Procedure type, like so:

    Code:
    Type
        TMyProc = Procedure(X : Integer);
    
    Var
        MyProc : TMyProc;
    
    Procedure Foo(X : Integer);
    Begin
        Writeln('Hi!');
        Writeln(X);
    End;
    
    Begin
        MyProc := @Foo;
        MyProc(10);
    End.
    Last edited by Murmandamus; 16-02-2011 at 06:56 PM.

  3. #3
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Is there no way to use any procedure without defining it? Or putting that into an object? The way you present works, but does sort of defeat the point of the idea...
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  4. #4
    Do you mean something like this:

    Code:
    var
     ExecPtr: Pointer;
    begin
     ExecPtr:= @foo;
    
     asm
      mov eax, ExecPtr
      call eax
     end;
    end;

  5. #5
    Just 1 thing to add to Murmandamus code...

    MyProc := @Foo; // This compiles on Freepascal

    MyProc := Foo; // This compiles on Delphi

  6. #6
    How you will call a procedure without defining it? How compiler will manage arguments of an undefined procedure?
    Nonetheless, you can define a generic procedure (or method and put it into an instance of a class) with variable number of parameters of any type:
    Code:
    type
    TMyProc = Procedure(args: array of const) of object;
    
    Procedure TSomeClass.Foo(args: array of const);
    Begin
        if lengths(args) > 1 then begin
            Writeln(args[0]);
            Writeln(args[1]);
        end;
    End;
    
    var 
      SomeClass: TSomeClass;
      MyProc: TMyProc;
    
    Begin
        MyProc := @SomeClass.Foo;
        MyProc(['Hello!', 10]);
    End.
    A well-known Format() function works this way. You can see it in VCL sources.
    You can also read more on topic "variant open array parameters".

  7. #7
    Proc/fn type must be defined, because procedure parameters must be known to call the given procedure correctly. Proc/fn pointers can be used in objects, of course.

    Edit: too slow.
    Last edited by imcold; 16-02-2011 at 08:22 PM.

  8. #8
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Thanks for all the replies - will start testing ASAP.

    cheers
    code_glitch
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  9. #9
    I didn't test, but if you typecast the pointer to a procedure/function type, you can call it.
    From brazil (:

    Pascal pownz!

  10. #10
    Quote Originally Posted by code_glitch View Post
    Is there no way to use any procedure without defining it? Or putting that into an object? The way you present works, but does sort of defeat the point of the idea...
    Maybe you had on your mind without "declaring" instead of "defining"?

    With anonymous methods in Delphi you can do things like:

    Code:
    initialization
      with TMyClass.Create do
      begin
        DoStuff(
          procedure(value: string)
          begin
            Writeln(value);
          end);
    
        Free;
      end;
    end.

Page 1 of 2 12 LastLast

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
  •