Hope you have fun with it!<br><br>Anyway, here's a much cleaner edition of my magic little template function. There's probably a host of errors and silly mistakes, but this is just a proof of concept more than anything else, really:<br>
<br>import std.file; // getTimes()<br>import std.conv; // to!string()<br>import std.traits; // Parameter types, storage, introspection<br><br>import std.stdio;<br><br>// Note:<br>// Errors are messy, since the function returns an arbitrary string ready to be<br>
// mixed in.<br>//<br>// If there are not enough arguments in args to make the call, autoCall<br>// will re-send the same argument value for each one missing in func's signature.<br>// (this is by accident, not design)<br>
//<br>// Tests could be added to make sure there are enough arguments in the <br>// args tuple however.<br>auto autoCall(alias func, T...)(T args)<br>{<br> alias ParameterStorageClass STC; // storage class enum<br>
alias ParameterTypeTuple!(func) types; // types of parameters<br> alias ParameterStorageClassTuple!(func) params; // storage class of parameters<br> <br> string declString, callString;<br> callString = __traits(identifier, func) ~ "("; // funcName( <br>
<br> foreach (int i, param; params)<br> {<br> if (param == STC.OUT || param == STC.REF)<br> {<br> // e.g. "int var1; "<br> declString ~= types[i].stringof ~ " var" ~ i.stringof ~ "; ";<br>
<br> // e.g. "funcName(" ~= " var1,"<br> callString ~= " var" ~ i.stringof ~ ",";<br> }<br> else<br> {<br> // The non-out & non-ref parameter to func is already present in args,<br>
// fetch it's value and append it to the call string.<br> <br> // need to use static if due to DMD complaints about array bounds<br> // in the else clause<br> static if (args.length == 1)<br>
{<br> // e.g. "funcName(" ~= "r'filename', "<br> callString ~= '"' ~ to!string(args) ~ '"' ~ ",";<br> }<br>
else<br> {<br> callString ~= '"' ~ to!string(args[i]) ~ '"' ~ ",";<br> }<br> }<br> }<br> <br> // remove the remaining comma and close the call string<br>
callString = callString[0 .. $ - 1] ~ ");";<br><br> // return joined strings ready to be mixin()'ed<br> return declString ~= callString;<br>}<br><br>void testMe(string x, string y)<br>{<br> writeln(x, y);<br>
}<br><br>unittest<br>{ <br> mixin(autoCall!(getTimes)(r"C:\\cookies"));<br> mixin(autoCall!(testMe)("test"));<br> <br> writefln("var1: %s, var2: %s, var3: %s", var1, var2, var3);<br>
}<br><br>void main() { }<br><br><br><br>You'll have to replace the string in the first mixin ("cookies") to some file on your drive. I'm not sure if the Linux version of getTimes works the same way though.<br>
<br><br><div class="gmail_quote">On Fri, Aug 6, 2010 at 12:05 AM, Philippe Sigaud <span dir="ltr"><<a href="mailto:philippe.sigaud@gmail.com">philippe.sigaud@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="gmail_quote"><div class="im">On Thu, Aug 5, 2010 at 23:50, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com" target="_blank">andrej.mitrovich@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="gmail_quote"><div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="gmail_quote">
<div><br></div><div><br></div><div>As for demangling, how do you do to get mangled names in the first place?</div><div><div> </div></div></div></blockquote></div><div><br>I was using mangledName!() from std.straits. __traits works prefectly, Thanks!<br>
<br></div></div></blockquote><div><br></div></div><div>Oh, oh, lots of shiny new things in std.traits! </div><div>I didn't specifically look at this module for 2.047, my mistake.</div><div><br></div><div>And, demange(mangledName!foo) gives back a qualified name, nice! I was looking as a way to get that, not two hours ago.</div>
<div><br></div><div>*goes play with it*</div><div><br></div><div>Ah, it doesn't work with templates names, though.</div><div><br></div><div>Thanks Andrej!</div><div><br></div><div>Philippe</div><div><br></div></div>
</blockquote></div><br>