FPC: Array of method pointers or procedures 
Author Message
 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



Wed, 13 Aug 2003 22:52:20 GMT  
 FPC: Array of method pointers or procedures

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

>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

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.


Thu, 14 Aug 2003 00:07:46 GMT  
 FPC: Array of method pointers or procedures

Quote:


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



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



Thu, 14 Aug 2003 00:21:46 GMT  
 FPC: Array of method pointers or procedures

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.)

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.


Thu, 14 Aug 2003 02:08:25 GMT  
 FPC: Array of method pointers or procedures

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.)

>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.


Thu, 14 Aug 2003 03:24:07 GMT  
 FPC: Array of method pointers or procedures


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



Mon, 18 Aug 2003 17:59:47 GMT  
 FPC: Array of method pointers or procedures

Quote:


>     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.


Mon, 18 Aug 2003 19:09:06 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How can I use methods as procedure pointers?

2. Needed: Code for array of pointers to procedures

3. Passing a pointer to a procedure/function as a parameter in a procedure

4. Invalid Pointer Operation after FPC 1.00 installation and an Ansistring problem

5. Pointer to method

6. How to declare dynamic array in fpc ??

7. FPC: Repetition of simple procedures

8. array of pointers

9. Array with pointer variable access

10. Question on passing pointer to procedure - question.zip (0/1)

11. Question on passing pointer to procedure - question.zip (0/1)

12. array of pointers...

 

 
Powered by phpBB® Forum Software