www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - wrapping functions with variadic-parameter wrappers

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d0407166d7b36b704d0180ff1
Content-Type: text/plain; charset=UTF-8

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.

--f46d0407166d7b36b704d0180ff1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

What&#39;s the best (least overhead and most portable and safe) way wrappin=
g a function into a variadic-parameter function?<div><br></div><div>For ins=
tance:<br></div><div><div><br></div><div>long foo(int i, char c)</div><div>
{</div><div>=C2=A0 =C2=A0/// ...<br>}</div><div><br></div><div>long bar(...=
)<br>{</div><div>=C2=A0 =C2=A0 return foo(/* ??? */);<br>}<br><div><div><br=
</div><div>This is necessary for losing the parameter types, but still hav=

d be done using inline assembler and knowledge of the D ABI to achieve near= -zero overhead.</div> <div><br></div><div>The wrapper can include dynamic type checking using the= automatically passed _arguments array to ensure type safety.</div><div><br=
</div><div>This is useful for dynamic dispatching.</div><div><br></div>

</div></div></div> --f46d0407166d7b36b704d0180ff1--
Dec 05 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Dec 05 2012
parent Mike Wey <mike-wey example.com> writes:
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
Dec 05 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d0420a695457d6b04d01cce1d
Content-Type: text/plain; charset=UTF-8

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. --f46d0420a695457d6b04d01cce1d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable A function with variadic template parameters is just a function which takes= a set of compile-time known parameters.<div>My goal is to have a non-templ= ate function taking variadic parameters.</div><div><br></div><div>long bar(= ...)</div> <div>{</div><div>=C2=A0 =C2=A0 return foo(...);<br>}</div><div><br></div><d= iv>This is necessary to be able to pass variables to functions without know= ing the type of the functions.</div><div class=3D"gmail_extra"><br><br><div= class=3D"gmail_quote"> On Wed, Dec 5, 2012 at 7:33 PM, Jacob Carlborg <span dir=3D"ltr">&lt;<a hre= f=3D"mailto:doob me.com" target=3D"_blank">doob me.com</a>&gt;</span> wrote= :<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-le= ft:1px #ccc solid;padding-left:1ex"> <div class=3D"HOEnZb"><div class=3D"h5">On 2012-12-05 11:01, Gor Gyolchanya= n wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> What&#39;s the best (least overhead and most portable and safe) way wrappin= g<br> a function into a variadic-parameter function?<br> <br> For instance:<br> <br> long foo(int i, char c)<br> {<br> =C2=A0 =C2=A0 /// ...<br> }<br> <br> long bar(...)<br> {<br> =C2=A0 =C2=A0 =C2=A0return foo(/* ??? */);<br> }<br> <br> This is necessary for losing the parameter types, but still having the<br> function callable with its expected parameters. I suspect this could be<br> done using inline assembler and knowledge of the D ABI to achieve<br> near-zero overhead.<br> <br> The wrapper can include dynamic type checking using the automatically<br> passed _arguments array to ensure type safety.<br> <br> This is useful for dynamic dispatching.<br> </blockquote> <br></div></div> It might not be want you need but a variadic template is the easiest soluti= on:<br> <br> long bar (Args ...) (Args args)<br> {<br> =C2=A0 =C2=A0 return foo(args);<br> }<span class=3D"HOEnZb"><font color=3D"#888888"><br> <br> -- <br> /Jacob Carlborg<br> </font></span></blockquote></div><br><br clear=3D"all"><div><br></div>-- <b= r>Bye,<br>Gor Gyolchanyan.<br> </div> --f46d0420a695457d6b04d01cce1d--
Dec 05 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--bcaec54ee8a4ee5ac404d0207679
Content-Type: text/plain; charset=UTF-8

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. --bcaec54ee8a4ee5ac404d0207679 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable kinda, but with minimal overhead and boilerplate<div class=3D"gmail_extra">= <br><br><div class=3D"gmail_quote">On Wed, Dec 5, 2012 at 8:32 PM, Mike Wey= <span dir=3D"ltr">&lt;<a href=3D"mailto:mike-wey example.com" target=3D"_b= lank">mike-wey example.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div class=3D"im">On 12/05/2012 04:40 PM, Go= r Gyolchanyan wrote:<br> </div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"><div class=3D"im"> A function with variadic template parameters is just a function which<br> takes a set of compile-time known parameters.<br> My goal is to have a non-template function taking variadic parameters.<br> <br> long bar(...)<br> {<br> =C2=A0 =C2=A0 =C2=A0return foo(...);<br> }<br> <br> This is necessary to be able to pass variables to functions without<br> knowing the type of the functions.<br> <br> <br></div> --<br> Bye,<br> Gor Gyolchanyan.<br> </blockquote> <br> Something like this?<br> <br> long bar(...)<br> {<br> =C2=A0 =C2=A0 if( _arguments[0] =3D=3D typeid(int) &amp;&amp; _arguments[1]= =3D=3D typeid(char) )<br> =C2=A0 =C2=A0 {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 return foo(va_arg!(int)(_argptr), va_arg!(char)= (_argptr));<br> =C2=A0 =C2=A0 }<br> =C2=A0 =C2=A0 else<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 throw;<br> }<span class=3D"HOEnZb"><font color=3D"#888888"><br> <br> -- <br> Mike Wey<br> </font></span></blockquote></div><br><br clear=3D"all"><div><br></div>-- <b= r>Bye,<br>Gor Gyolchanyan.<br> </div> --bcaec54ee8a4ee5ac404d0207679--
Dec 05 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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.
Dec 05 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--bcaec552428a480a2b04d02a3690
Content-Type: text/plain; charset=UTF-8

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

 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

 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. --bcaec552428a480a2b04d02a3690 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Thu, Dec 6, 2012 at 10:02 AM, Jonathan M Davis <span dir=3D"ltr">&lt;<a = href=3D"mailto:jmdavisProg gmx.com" target=3D"_blank">jmdavisProg gmx.com</= a>&gt;</span> wrote:<br><div class=3D"gmail_extra"><div class=3D"gmail_quot= e"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left= :1px #ccc solid;padding-left:1ex"> <div class=3D"im">On Wednesday, December 05, 2012 19:40:44 Gor Gyolchanyan = wrote:<br> &gt; A function with variadic template parameters is just a function which = takes<br> &gt; a set of compile-time known parameters.<br> &gt; My goal is to have a non-template function taking variadic parameters.= <br> &gt;<br> &gt; long bar(...)<br> &gt; {<br> &gt; =C2=A0 =C2=A0 return foo(...);<br> &gt; }<br> &gt;<br> &gt; This is necessary to be able to pass variables to functions without kn= owing<br> &gt; the type of the functions.<br> <br> </div>Then read the section on variadic functions and pick which type works= best for<br> you: <a href=3D"http://dlang.org/function.html" target=3D"_blank">http://dl= ang.org/function.html</a><br> <span class=3D"HOEnZb"><font color=3D"#888888"><br> - Jonathan M Davis<br> </font></span><br> <br> P.S. Please stop top posting. It&#39;s generally considered rude in lists l= ike<br> this.<br> </blockquote></div><br>Sorry, force of habit. Gmail puts me at the top and = hides the message by default,</div><div class=3D"gmail_extra">I read about = the d-style variadic functions and I also read the ABI.</div><div class=3D"= gmail_extra"> There are some vast differences in the ABI (the parameters are pushed in re= verse order, the caller must clean the stack...).</div><div class=3D"gmail_= extra">I thought that you guys have a better understanding of what&#39;s go= ing on, so you might help me with this.</div> <div class=3D"gmail_extra">If done carefully it could be a very valuable ad= dition to std.functional.</div><div class=3D"gmail_extra"><div><br></div>--= <br>Bye,<br>Gor Gyolchanyan.<br> </div> --bcaec552428a480a2b04d02a3690--
Dec 05 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Dec 06 2012
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--485b390f7a163bb1b704d02bdbd4
Content-Type: text/plain; charset=UTF-8

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,

 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. --485b390f7a163bb1b704d02bdbd4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I know, that variadic templates are safer, but this use case requires that = the parameter types and quantities should be statically unknown. It&#39;s a= ll part of dynamic dispatch that I&#39;m implementing.<div><br></div><div> For all we know they might interact with C. As far as I read on <a href=3D"= http://dlang.org">dlang.org</a> it will require tons of ASM to adapt variad= ic ABI to non-variadic ABI.</div><div class=3D"gmail_extra"><br><br><div cl= ass=3D"gmail_quote"> On Thu, Dec 6, 2012 at 1:14 PM, Jonathan M Davis <span dir=3D"ltr">&lt;<a h= ref=3D"mailto:jmdavisProg gmx.com" target=3D"_blank">jmdavisProg gmx.com</a=
&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 =

<div class=3D"im">On Thursday, December 06, 2012 11:40:24 Gor Gyolchanyan w= rote:<br> &gt; I read about the d-style variadic functions and I also read the ABI.<b= r> &gt; There are some vast differences in the ABI (the parameters are pushed = in<br> &gt; reverse order, the caller must clean the stack...).<br> &gt; I thought that you guys have a better understanding of what&#39;s goin= g on, so<br> &gt; you might help me with this.<br> &gt; If done carefully it could be a very valuable addition to std.function= al.<br> <br> </div>I think that at this point, almost everyone just uses variadic templa= tes when<br> they want a variadic function. Upon occasion, a typesafe variadic function<= br> makes sense, but C style variadics basically never make sense unless you&#3= 9;re<br> interacting with C, and in general, variadic templates are vastly superior = to<br> D variadics, so it&#39;s the variadic templates get used. And as most of ju= st use<br> the variadic templates, in the case of many of us, our knowledge of D style= <br> variadics is low.<br> <span class=3D"HOEnZb"><font color=3D"#888888"><br> - Jonathan M Davis<br> </font></span></blockquote></div><br><br clear=3D"all"><div><br></div>-- <b= r>Bye,<br>Gor Gyolchanyan.<br> </div> --485b390f7a163bb1b704d02bdbd4--
Dec 06 2012