www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Replace (ie: substitute) a type in varadic args

reply Saurabh Das <saurabh.das gmail.com> writes:
How can I substitute the type of an argument received via a 
varadic template?

For example say I want to generalise this scenario:

auto myConverterFunction1(bool arg1, bool arg2, ubyte arg3, int 
arg4)
{
     return targetFunction(cast(ubyte)arg1, cast(ubyte)arg2, arg3, 
arg4);
}

So I'll have something like:

auto myConverterFunction2(Args...)(Args args)
{
     // Don't know how to do this part...
}

Basically I need to substitute bool for ubyte to interface with 
the Java JNI.

Thanks,
Saurabh
Aug 02 2016
parent reply Sean Campbell <sycam.inc gmail.com> writes:
On Tuesday, 2 August 2016 at 07:24:28 UTC, Saurabh Das wrote:
 How can I substitute the type of an argument received via a 
 varadic template?

 For example say I want to generalise this scenario:

 auto myConverterFunction1(bool arg1, bool arg2, ubyte arg3, int 
 arg4)
 {
     return targetFunction(cast(ubyte)arg1, cast(ubyte)arg2, 
 arg3, arg4);
 }

 So I'll have something like:

 auto myConverterFunction2(Args...)(Args args)
 {
     // Don't know how to do this part...
 }

 Basically I need to substitute bool for ubyte to interface with 
 the Java JNI.

 Thanks,
 Saurabh
Just of the top of my head, using ugly string mixins, this: auto myConverterFunc(Args...)(Args args) { string genCode() { string code = "targetFunction("; foreach (i, Arg; Args) { static if (is(Arg == bool)) code ~= format("cast(ubyte)args[%s]%s", i, i == Args.length ? "" : ","); else code ~= format("args[%s]%s", i, i == Args.length ? "" : ","); } code ~= ");"; return code; } mixin(genCode()); } void targetFunction(ubyte i, ubyte j, uint k, int l) { writefln("i : %s, j : %s, k : %s, l : %s",i,j,k,l); } void main() { myConverterFunc(true,false,10,20); }
Aug 02 2016
parent reply Saurabh Das <saurabh.das gmail.com> writes:
On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:
 On Tuesday, 2 August 2016 at 07:24:28 UTC, Saurabh Das wrote:
 [...]
Just of the top of my head, using ugly string mixins, this: auto myConverterFunc(Args...)(Args args) { string genCode() { string code = "targetFunction("; foreach (i, Arg; Args) { static if (is(Arg == bool)) code ~= format("cast(ubyte)args[%s]%s", i, i == Args.length ? "" : ","); else code ~= format("args[%s]%s", i, i == Args.length ? "" : ","); } code ~= ");"; return code; } mixin(genCode()); } void targetFunction(ubyte i, ubyte j, uint k, int l) { writefln("i : %s, j : %s, k : %s, l : %s",i,j,k,l); } void main() { myConverterFunc(true,false,10,20); }
Thanks. Yes that is one approach. I figured out another approach that seems decent: auto targetFunctionProxy(Args...)(Args args) { import std.meta; return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args); }
Aug 02 2016
parent reply Saurabh Das <saurabh.das gmail.com> writes:
On Tuesday, 2 August 2016 at 08:20:22 UTC, Saurabh Das wrote:
 On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:
 [...]
Thanks. Yes that is one approach. I figured out another approach that seems decent: auto targetFunctionProxy(Args...)(Args args) { import std.meta; return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args); }
PS: I'm unsure if this results in additional copying of arguments in the case where there are no bools in the arguments.
Aug 02 2016
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 2 August 2016 at 08:22:15 UTC, Saurabh Das wrote:
 On Tuesday, 2 August 2016 at 08:20:22 UTC, Saurabh Das wrote:
 On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:
 [...]
Thanks. Yes that is one approach. I figured out another approach that seems decent: auto targetFunctionProxy(Args...)(Args args) { import std.meta; return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args); }
PS: I'm unsure if this results in additional copying of arguments in the case where there are no bools in the arguments.
Even if it does, the optimiser should take care of it. If it doesn't file a bug report.
Aug 02 2016