May 04, 2024, 06:07:52 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Function Pointers

Started by Steven Picard, January 02, 2008, 09:32:00 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Steven Picard

Is EBasic capable of function pointers like Aurora?  If not this would be a great addition.  It would allow me to use it for many things like Window's Service, COM objects (so I can create the vPtr to the vTable which holds the interfaces (function pointers), etc.).

LarryMc

This should be in the EBasic forum instead of the Creative Basic forum since it was an EBasic question.

At any rate, from the EB Help file:

QuoteIndirectly calling subroutines

Emergence BASIC supports indirectly calling subroutines through function pointers. A function pointer is a UINT variable that contains the address of a subroutine. The subroutine can be local or global. A common use for this is to create an array of subroutines, that all accept identical parameters, to be called by an index. The DELCARE statement needs to be used to set up a parameter template.

DECLARE fnTemplate(param as UINT),INT

You can use any valid name you wish for the template. After a subroutine is defined you can get the address of that subroutine using the & operator.

SUB Addition(param1 as FLOAT, param2 as FLOAT), FLOAT
    RETURN  param1 + param2
ENDSUB

SUB Subtraction(param1 as FLOAT, param2 as FLOAT), FLOAT
    RETURN param1 - param2
ENDSUB

DEF fnArray[2] as UINT
fnArray[0] = &Addition
fnArray[1] = &Subtraction

Call the subroutine indirectly by using the ! operator, supplying the template name, variable containing the subroutine address, and any parameters

PRINT !<fnTemplate>fnArray[0](1.0, 3.0)

The DECLARE statement is only used as a template and will not look for a matching SUB when used as the parameter template for indirectly calling a subroutine. For a complete example see sample program: indirect_functions.eba


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Steven Picard

Whoops!  How'd I end posting this here?  :o

Thanks for the answer, Larry.  I didn't see anything in the help file for Ebasic on function pointers.  I just didn't know EBasic supported that.

I have some code I wrote in C++ that I want to convert over to EBasic and I was going to choose EBasic over Aurora so other people can make use of the code.

Perhaps someone can move this to the correct topic in case anyone else looks for this information?

Ionic Wind Support Team

Moved to the correct category.

It's in the users guide in the subroutines category towords the end of the page.

Paul.
Ionic Wind Support Team