Recent

Author Topic: how to do indirection  (Read 2698 times)

PascalDragon

  • Sr. Member
  • ****
  • Posts: 302
  • Compiler Developer
Re: how to do indirection
« Reply #15 on: February 05, 2019, 09:04:05 am »
Yes, it would probably be best to forbid that mode in units... :-X

Thaddy

  • Hero Member
  • *****
  • Posts: 7798
Re: how to do indirection
« Reply #16 on: February 05, 2019, 09:23:57 am »
But note ISO standard 10206 (a.k.a. extended pascal) supports modules and I believe FPC has already some features from extended pascal?
« Last Edit: February 05, 2019, 09:26:33 am by Thaddy »
Ad Brexinitum (can't help it)

PascalDragon

  • Sr. Member
  • ****
  • Posts: 302
  • Compiler Developer
Re: how to do indirection
« Reply #17 on: February 06, 2019, 10:08:10 am »
But that is a different mode.  ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 7798
Re: how to do indirection
« Reply #18 on: February 06, 2019, 10:20:20 am »
Ok. You are able to block it for units, so plz do!  :D
My point was just that if modules get supported in ExtPas mode a similar concept like units is in place.

(And extpas is an ISO mode, although maybe much like $delphi and $delphiunicode, Oh well, no: the ISO modes are downwards compatible)
« Last Edit: February 06, 2019, 10:23:49 am by Thaddy »
Ad Brexinitum (can't help it)

ahydra

  • New member
  • *
  • Posts: 19
Re: how to do indirection
« Reply #19 on: February 11, 2019, 05:42:35 am »
What Jamie and furious wrote is the normal way of doing this for non-OO routines. For OO, you can do some pretty powerful things with TMethod and CodePointer types. CodePointer is an "untyped function pointer" and TMethod can construct a class instance method call out of a CodePointer and a class instance. Then you typecast the TMethod to a function pointer of the appropriate signature and call it, like this:

Code: Pascal  [Select]
  1. type
  2.   TFunc = function(const x: int64): int64;
  3.  
  4. var
  5.   c: CodePointer;
  6.   m: TMethod;
  7.   value: int64;
  8.  
  9. if condition then c := @TMyClass.FooImplementation else c := @TMyClass.BarImplementation; // these can be instance methods or class methods. they have signature matching TFunc
  10.  
  11. ...
  12.  
  13. m.Code = c;
  14. m.Data = instance_of_tmyclass;
  15. value := TFunc(m)(12345);
  16.