[futurebasic] Re: [FB] Dynamic Functions?

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2008 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Fri, 18 Jul 2008 11:19:35 +0200
Max Taylor a écrit :
> Question:
> 
> Is it possible to send parameters of varying numbers and types to LOCAL 
> FN's if all you have is the "address" of the FN's?
> 
> Yes, I know that one can use something like the line below (which is 
> from my MaxClass code),
> 
> DEF FN MxMethod(  objRef as handle, var1 as long ) USING gMethodAddrPtr.
> 
> Then use code like the following to use the above definition.
> 
> IF gMethodAddrPtr then FN MxMethod( objRef, var1 )
> 
> Problem:
> 
> What if I wanted to send a 'short' or a 'pointer' or a 'single', or a 
> 'double' to the above example once it has been DEF'd?
> 
> Could the number and type of the parameters be totally dynamic?
> 
> Is it possible to build such a FN?
> 
> Would we have to use the DEF FN USING or is there another way.
> 
> Any suggestions or thoughts on this would be greatly appreciated.
> 

Max,

My suggestion is most certainly a bit awkward but for a varying type 
var, you could try to define a custom record using a union:

begin record myVaryingTypeVarRec
   begin union
     dim myByte as byte
     dim myShort as short
     dim myLong as long
     dim mySingle as single
     dim myDouble as double
     dim myString as str255
   end union
end record

Then you would have to write this definition:

DEF FN MxMethod( objRef as handle, var1 as ^myVaryingTypeVarRec ) USING 
gMethodAddrPtr

Of course before calling MxMethod you would have to declare a custom var 
of the myVaryingTypeVarRec type and set the relevant field. Each 
indirectly called function would have to deal with the appropriate field 
of that tortuous var.

For varying number of variables I have no suggestion unless you know the 
maximum number of variables than can be used, and even in that case it 
would be pretty clumsy, without counting the waste of memory.

I'm just thinking about another possibility for a varying type variable: 
you could pass only the address of your var onto the function.

DEF FN MxMethod( objRef as handle, @var1 as ptr ) USING gMethodAddrPtr

The caveat being that all your vars must reside in memory.

The second caveat is that unless you make a copy of your variable you 
would work on the original var inside the called function.

If you need to check the var type inside the called function, pass along 
the type of the variable using the typeof function:

DEF FN MxMethod( objRef as handle, @var1 as ptr, varType as int ) USING 
gMethodAddrPtr

Called like this:

fn MxMethod( myObj, myVar, typeof(myVar) )

And perhaps, I should stop drinking wine so early in the morning.

Alain