Board index » delphi » FPC: Array of method pointers or procedures

FPC: Array of method pointers or procedures

Hi,

I'm using FPC 1.0.2 GO32V2, and have a question:
(and, yes, I know it's not the newest version ;)

first some (pseudo)code:

Type
    OpCodeType = (op_hlt,
                  op_nop,
                  op_mov,
                  ...
                  op_brk);
    MyClass = Class
                 private
                    CurrOpCode:OpCodeType;
                     ...
                    procedure Handle_op_hlt;
                    procedure Handle_op_nop;
                    procedure Handle_op_mov;
                     ...
                    procedure Handle_op_brk;
                    Procedure Handler;
                     ...
                 public
                     ...
              End; //MyClass

Procedure MyClass.Handler;
begin
     case CurrOpCode of
           op_hlt:Handle_op_hlt;
           op_nop:Handle_op_nop;
             ...
           op_brk:Handle_op_brk;
     end;    
end; //MyClass.Handler;

Instead of having on huge case, i'd like to:

Procedure MyClass.Handler;
const handlertable:array[OpCodeType] of HandlerProc =
      (Handle_op_hlt, Handle_op_nop, ... );
begin
     HandlerTable[CurrOpCode];
end; //MyClass.Handler;

Unfortunately, this is not possible.

Any way to do this, short of moving the Handle_op_* procedures
out of the class and giving them the class as a parameter?

Or maybe making the Handle_op_* procedures message methods
is the best choice? I haven't looked at that possibility
yet, but does FPC accept
Procedure MyClass.Handle_op_mov(var Msg); Message op_mov;
or
Procedure MyClass.Handle_op_mov(var Msg); Message Ord(op_mov);

Hm. I guess I'll just try those when I get back home.

Thanks for listening my musings :)

--
---------------------+---------------------------------------------
Pasi Kallinen        |"That's what the boys are there for." -Hitler
pka...@cs.joensuu.fi |SciFiFantasyHorrorBooks|Magic:tG|Nethack|RPGs

 

Re:FPC: Array of method pointers or procedures


Quote
In article <978hr4$...@news.cs>, Pasi Kallinen wrote:
>Hi,

>I'm using FPC 1.0.2 GO32V2, and have a question:
>(and, yes, I know it's not the newest version ;)

>first some (pseudo)code:

>Type
>    OpCodeType = (op_hlt,
>                  op_nop,
>                  op_mov,
>                  ...
>                  op_brk);
>    MyClass = Class
>                 private
>                    CurrOpCode:OpCodeType;
>                     ...
>                    procedure Handle_op_hlt;
>                    procedure Handle_op_nop;
>                    procedure Handle_op_mov;
>                     ...
>                    procedure Handle_op_brk;
>                    Procedure Handler;
>                     ...
>                 public
>                     ...
>              End; //MyClass

>Procedure MyClass.Handler;
>begin
>     case CurrOpCode of
>           op_hlt:Handle_op_hlt;
>           op_nop:Handle_op_nop;
>             ...
>           op_brk:Handle_op_brk;
>     end;    
>end; //MyClass.Handler;

>Instead of having on huge case, i'd like to:

>Procedure MyClass.Handler;
>const handlertable:array[OpCodeType] of HandlerProc =
>      (Handle_op_hlt, Handle_op_nop, ... );

One of the FPC incompabilities to keep the FPC Delphi mode (-S2) more TP
compatible, and allow better hybrid language (pascal/object pascal)
combination. This is all documented.

try

(@handle_op_hlt,@handle_op_nop,...)

Quote
>Or maybe making the Handle_op_* procedures message methods
>is the best choice? I haven't looked at that possibility
>yet, but does FPC accept
>Procedure MyClass.Handle_op_mov(var Msg); Message op_mov;
>or
>Procedure MyClass.Handle_op_mov(var Msg); Message Ord(op_mov);

Afaik the win32 version does accept messages, but it would be overkill for
something like this, and unportable too.

Re:FPC: Array of method pointers or procedures


Marco van de Voort <mar...@toad.stack.nl> wrote:

Quote

> In article <978hr4$...@news.cs>, Pasi Kallinen wrote:

snipsnip

>>Procedure MyClass.Handler;
>>const handlertable:array[OpCodeType] of HandlerProc =
>>      (Handle_op_hlt, Handle_op_nop, ... );

> One of the FPC incompabilities to keep the FPC Delphi mode (-S2) more TP
> compatible, and allow better hybrid language (pascal/object pascal)
> combination. This is all documented.

> try

> (@handle_op_hlt,@handle_op_nop,...)

Thanks.

I must've had a brainburp or something as this completely
escaped me, even though I used it in the same program
earlier.

Quote
>>yet, but does FPC accept
>>Procedure MyClass.Handle_op_mov(var Msg); Message op_mov;
>>or
>>Procedure MyClass.Handle_op_mov(var Msg); Message Ord(op_mov);

> Afaik the win32 version does accept messages, but it would be overkill for
> something like this, and unportable too.

Ah! Didn't realize the messages-stuff is not portable. Can't use
that then.
(I thought the message-system was done internally by FPC, and not
riding on the Windows' messages handler.)

--
---------------------+---------------------------------------------
Pasi Kallinen        |"That's what the boys are there for." -Hitler
pka...@cs.joensuu.fi |SciFiFantasyHorrorBooks|Magic:tG|Nethack|RPGs

Re:FPC: Array of method pointers or procedures


Quote
In article <978n2q$...@news.cs>, Pasi Kallinen wrote:
>Marco van de Voort <mar...@toad.stack.nl> wrote:
>>>yet, but does FPC accept
>>>Procedure MyClass.Handle_op_mov(var Msg); Message op_mov;
>>>or
>>>Procedure MyClass.Handle_op_mov(var Msg); Message Ord(op_mov);

>> Afaik the win32 version does accept messages, but it would be overkill for
>> something like this, and unportable too.

>Ah! Didn't realize the messages-stuff is not portable. Can't use
>that then.
>(I thought the message-system was done internally by FPC, and not
>riding on the Windows' messages handler.)

Exceptions are FPC internal, and so will be variant support.  Messages are
not, but the unix platforms have signal handling, which in some (very
limited ) way can compensate that.

Re:FPC: Array of method pointers or procedures


In article <slrn99fu4p.9bg.mar...@toad.stack.nl>, Marco van de Voort wrote:

Quote
>In article <978n2q$...@news.cs>, Pasi Kallinen wrote:
>>Marco van de Voort <mar...@toad.stack.nl> wrote:

>>>>yet, but does FPC accept
>>>>Procedure MyClass.Handle_op_mov(var Msg); Message op_mov;
>>>>or
>>>>Procedure MyClass.Handle_op_mov(var Msg); Message Ord(op_mov);

>>> Afaik the win32 version does accept messages, but it would be overkill for
>>> something like this, and unportable too.

>>Ah! Didn't realize the messages-stuff is not portable. Can't use
>>that then.
>>(I thought the message-system was done internally by FPC, and not
>>riding on the Windows' messages handler.)

>Exceptions are FPC internal, and so will be variant support.  Messages are
>not, but the unix platforms have signal handling, which in some (very
>limited ) way can compensate that.

Ouch, there is also compiler messaging support on other systems it seems.
You just have to create your own handler, and convert OS events to the
messaging system.

Re:FPC: Array of method pointers or procedures


On 24 Feb 2001, Pasi Kallinen wrote:

Quote
> Hi,

> I'm using FPC 1.0.2 GO32V2, and have a question:
> (and, yes, I know it's not the newest version ;)

> first some (pseudo)code:

> Type
>     OpCodeType = (op_hlt,
>                   op_nop,
>                   op_mov,
>                   ...
>                   op_brk);
>     MyClass = Class
>                  private
>                     CurrOpCode:OpCodeType;
>                      ...
>                     procedure Handle_op_hlt;
>                     procedure Handle_op_nop;
>                     procedure Handle_op_mov;
>                      ...
>                     procedure Handle_op_brk;
>                     Procedure Handler;
>                      ...
>                  public
>                      ...
>               End; //MyClass

I would suggest you to rethink your design:

Type
     OpCodeType = (op_hlt,
                   op_nop,
                   op_mov,
                   ...
                   op_brk);

     MyClass = Class
                  private
                     CurrOpCode:OpCodeType;
                     ...
                     procedure Handle_op; virtual;
               end;

    MyNopClass = Class(MyClass)
                     procedure Handle_op; override;
                 end;

etc. This is inhernetly the same as using a table of function pointers,
but syntactically much cleaner.

I think the way to declare procedure/function variables of object methods
is to use "procedure of object" or something like that, but I'm not
familiar with that.

Jonas

Re:FPC: Array of method pointers or procedures


In article <Pine.LNX.4.21.0103011056090.1523-100...@zeus.rug.ac.be>, Jonas

Quote
Maebe wrote:

>On 24 Feb 2001, Pasi Kallinen wrote:
>     MyClass = Class
>                  private
>                     CurrOpCode:OpCodeType;
>                     ...
>                     procedure Handle_op; virtual;
>               end;

>    MyNopClass = Class(MyClass)
>                     procedure Handle_op; override;
>                 end;

>etc. This is inhernetly the same as using a table of function pointers,
>but syntactically much cleaner.

>I think the way to declare procedure/function variables of object methods
>is to use "procedure of object" or something like that, but I'm not
>familiar with that.

That's true, and I haven't found problems in that functionality for a while,
so it is considered stable.

Other Threads