View mode: basic / threaded / horizontal-split · Log in · Help
December 05, 2012
wrapping functions with variadic-parameter wrappers
What's the best (least overhead and most portable and safe) way wrapping a
function into a variadic-parameter function?

For instance:

long foo(int i, char c)
{
  /// ...
}

long bar(...)
{
   return foo(/* ??? */);
}

This is necessary for losing the parameter types, but still having the
function callable with its expected parameters. I suspect this could be
done using inline assembler and knowledge of the D ABI to achieve near-zero
overhead.

The wrapper can include dynamic type checking using the automatically
passed _arguments array to ensure type safety.

This is useful for dynamic dispatching.

-- 
Bye,
Gor Gyolchanyan.
December 05, 2012
Re: wrapping functions with variadic-parameter wrappers
On 2012-12-05 11:01, Gor Gyolchanyan wrote:
> What's the best (least overhead and most portable and safe) way wrapping
> a function into a variadic-parameter function?
>
> For instance:
>
> long foo(int i, char c)
> {
>     /// ...
> }
>
> long bar(...)
> {
>      return foo(/* ??? */);
> }
>
> This is necessary for losing the parameter types, but still having the
> function callable with its expected parameters. I suspect this could be
> done using inline assembler and knowledge of the D ABI to achieve
> near-zero overhead.
>
> The wrapper can include dynamic type checking using the automatically
> passed _arguments array to ensure type safety.
>
> This is useful for dynamic dispatching.

It might not be want you need but a variadic template is the easiest 
solution:

long bar (Args ...) (Args args)
{
    return foo(args);
}

-- 
/Jacob Carlborg
December 05, 2012
Re: wrapping functions with variadic-parameter wrappers
A function with variadic template parameters is just a function which takes
a set of compile-time known parameters.
My goal is to have a non-template function taking variadic parameters.

long bar(...)
{
   return foo(...);
}

This is necessary to be able to pass variables to functions without knowing
the type of the functions.


On Wed, Dec 5, 2012 at 7:33 PM, Jacob Carlborg <doob@me.com> wrote:

> On 2012-12-05 11:01, Gor Gyolchanyan wrote:
>
>> What's the best (least overhead and most portable and safe) way wrapping
>> a function into a variadic-parameter function?
>>
>> For instance:
>>
>> long foo(int i, char c)
>> {
>>     /// ...
>> }
>>
>> long bar(...)
>> {
>>      return foo(/* ??? */);
>> }
>>
>> This is necessary for losing the parameter types, but still having the
>> function callable with its expected parameters. I suspect this could be
>> done using inline assembler and knowledge of the D ABI to achieve
>> near-zero overhead.
>>
>> The wrapper can include dynamic type checking using the automatically
>> passed _arguments array to ensure type safety.
>>
>> This is useful for dynamic dispatching.
>>
>
> It might not be want you need but a variadic template is the easiest
> solution:
>
> long bar (Args ...) (Args args)
> {
>     return foo(args);
> }
>
> --
> /Jacob Carlborg
>



-- 
Bye,
Gor Gyolchanyan.
December 05, 2012
Re: wrapping functions with variadic-parameter wrappers
On 12/05/2012 04:40 PM, Gor Gyolchanyan wrote:
> A function with variadic template parameters is just a function which
> takes a set of compile-time known parameters.
> My goal is to have a non-template function taking variadic parameters.
>
> long bar(...)
> {
>      return foo(...);
> }
>
> This is necessary to be able to pass variables to functions without
> knowing the type of the functions.
>
>
> --
> Bye,
> Gor Gyolchanyan.

Something like this?

long bar(...)
{
    if( _arguments[0] == typeid(int) && _arguments[1] == typeid(char) )
    {
        return foo(va_arg!(int)(_argptr), va_arg!(char)(_argptr));
    }
    else
        throw;
}

-- 
Mike Wey
December 05, 2012
Re: wrapping functions with variadic-parameter wrappers
kinda, but with minimal overhead and boilerplate


On Wed, Dec 5, 2012 at 8:32 PM, Mike Wey <mike-wey@example.com> wrote:

> On 12/05/2012 04:40 PM, Gor Gyolchanyan wrote:
>
>> A function with variadic template parameters is just a function which
>> takes a set of compile-time known parameters.
>> My goal is to have a non-template function taking variadic parameters.
>>
>> long bar(...)
>> {
>>      return foo(...);
>> }
>>
>> This is necessary to be able to pass variables to functions without
>> knowing the type of the functions.
>>
>>
>> --
>> Bye,
>> Gor Gyolchanyan.
>>
>
> Something like this?
>
> long bar(...)
> {
>     if( _arguments[0] == typeid(int) && _arguments[1] == typeid(char) )
>     {
>         return foo(va_arg!(int)(_argptr), va_arg!(char)(_argptr));
>     }
>     else
>         throw;
> }
>
> --
> Mike Wey
>



-- 
Bye,
Gor Gyolchanyan.
December 06, 2012
Re: wrapping functions with variadic-parameter wrappers
On Wednesday, December 05, 2012 19:40:44 Gor Gyolchanyan wrote:
> A function with variadic template parameters is just a function which takes
> a set of compile-time known parameters.
> My goal is to have a non-template function taking variadic parameters.
> 
> long bar(...)
> {
>     return foo(...);
> }
> 
> This is necessary to be able to pass variables to functions without knowing
> the type of the functions.

Then read the section on variadic functions and pick which type works best for 
you: http://dlang.org/function.html

- Jonathan M Davis


P.S. Please stop top posting. It's generally considered rude in lists like 
this.
December 06, 2012
Re: wrapping functions with variadic-parameter wrappers
On Thu, Dec 6, 2012 at 10:02 AM, Jonathan M Davis <jmdavisProg@gmx.com>wrote:

> On Wednesday, December 05, 2012 19:40:44 Gor Gyolchanyan wrote:
> > A function with variadic template parameters is just a function which
> takes
> > a set of compile-time known parameters.
> > My goal is to have a non-template function taking variadic parameters.
> >
> > long bar(...)
> > {
> >     return foo(...);
> > }
> >
> > This is necessary to be able to pass variables to functions without
> knowing
> > the type of the functions.
>
> Then read the section on variadic functions and pick which type works best
> for
> you: http://dlang.org/function.html
>
> - Jonathan M Davis
>
>
> P.S. Please stop top posting. It's generally considered rude in lists like
> this.
>

Sorry, force of habit. Gmail puts me at the top and hides the message by
default,
I read about the d-style variadic functions and I also read the ABI.
There are some vast differences in the ABI (the parameters are pushed in
reverse order, the caller must clean the stack...).
I thought that you guys have a better understanding of what's going on, so
you might help me with this.
If done carefully it could be a very valuable addition to std.functional.

-- 
Bye,
Gor Gyolchanyan.
December 06, 2012
Re: wrapping functions with variadic-parameter wrappers
On Thursday, December 06, 2012 11:40:24 Gor Gyolchanyan wrote:
> I read about the d-style variadic functions and I also read the ABI.
> There are some vast differences in the ABI (the parameters are pushed in
> reverse order, the caller must clean the stack...).
> I thought that you guys have a better understanding of what's going on, so
> you might help me with this.
> If done carefully it could be a very valuable addition to std.functional.

I think that at this point, almost everyone just uses variadic templates when 
they want a variadic function. Upon occasion, a typesafe variadic function 
makes sense, but C style variadics basically never make sense unless you're 
interacting with C, and in general, variadic templates are vastly superior to 
D variadics, so it's the variadic templates get used. And as most of just use 
the variadic templates, in the case of many of us, our knowledge of D style 
variadics is low. 

- Jonathan M Davis
December 06, 2012
Re: wrapping functions with variadic-parameter wrappers
I know, that variadic templates are safer, but this use case requires that
the parameter types and quantities should be statically unknown. It's all
part of dynamic dispatch that I'm implementing.

For all we know they might interact with C. As far as I read on
dlang.orgit will require tons of ASM to adapt variadic ABI to
non-variadic ABI.


On Thu, Dec 6, 2012 at 1:14 PM, Jonathan M Davis <jmdavisProg@gmx.com>wrote:

> On Thursday, December 06, 2012 11:40:24 Gor Gyolchanyan wrote:
> > I read about the d-style variadic functions and I also read the ABI.
> > There are some vast differences in the ABI (the parameters are pushed in
> > reverse order, the caller must clean the stack...).
> > I thought that you guys have a better understanding of what's going on,
> so
> > you might help me with this.
> > If done carefully it could be a very valuable addition to std.functional.
>
> I think that at this point, almost everyone just uses variadic templates
> when
> they want a variadic function. Upon occasion, a typesafe variadic function
> makes sense, but C style variadics basically never make sense unless you're
> interacting with C, and in general, variadic templates are vastly superior
> to
> D variadics, so it's the variadic templates get used. And as most of just
> use
> the variadic templates, in the case of many of us, our knowledge of D style
> variadics is low.
>
> - Jonathan M Davis
>



-- 
Bye,
Gor Gyolchanyan.
Top | Discussion index | About this forum | D home