On 10 July 2015 at 10:18, Matthieu Lacaton <matthieu.laca...@gmail.com>
wrote:

> Hello,
>
> Is it possible with NativeBoost to create a binding for a variadic
> function ?
>
> I've seen the printf example in NBCPrinter but this implementation is kind
> of cheating since it always pass just a %s as format and one already
> formatted string to the C function.
>
> I've written a simple variadic function which adds every integer it
> receives as argument (first argument is for the number of following
> arguments) :
>
> int add(int number,...);
>
> In Pharo I've tried something like this :
>
> *add: *number *arg1: *first *arg2: *second
>> <primitive: #primitiveNativeCall module: #NativeBoostPlugin>
>>
>> ^ self nbCall: #( int add (int number, int first, int second))
>>   module: 'libMyLib.so'
>>
>
> and it works fine with two arguments.
>
> Basically, doing so, I would need one method per number of arguments so
> it's not very cool.
>
> I thought that maybe I could pass an array as argument to my Pharo method
> but I didn't really find a way to figure out how to define the nbCall
> without having a "Generic failure".
>
> *add: *number *args: *anArray
>> <primitive: #primitiveNativeCall module: #NativeBoostPlugin>
>>
>> ^ self nbCall: #( int add (int number, ??? anArray))
>>   module: 'libMyLib.so'
>>
>
> Do you have an idea ?
>
>
In short, there's no marshaller for converting an array of items, to same
number of things on stack. That could solve the problem with your example:
passing array of objects of *same* type. But in general, it is not what C
variadic function(s) standing for. Because they stand for any number of
arguments, of any type.
In C, since all program is compiled statically, compiler knows the number
of passed arguments and their types through compiling each particular call
site(s) to variadic function. Which means that in fact, you are still
supplying all information needed by compiler *before* run time.

In variadic functions, you can pass any arguments of any type,
but for converting each of them, you must tell NB what kind of  marshaller
should be used for it , which means, that it is impossible to know before
run time, since you cannot know how many arguments you may pass, not
speaking about their types.




> Thanks,
>
> Matthieu
>
>
>


-- 
Best regards,
Igor Stasenko.

Reply via email to