On 09/04/2011 18:13, Andrej Mitrovic wrote:
Let's say I have this structure:
struct PAINTSTRUCT
{
bool state;
}
And somewhere in my main code I want to print out the value of state. But I
also want to know what I'm printing out. So usually I'd do this:
void main()
{
PAINTSTRUCT ps;
writefln("ps.state = %s", ps.state);
}
Has anyone written a function which will automatically print out both the
variable name and any object it might be part of, and then the value?
E.g. I'd like to use a function like this:
writeField(ps.state);
And if state is false it would print out to the console:
"ps.state = false"
I can't really touch PRINTSTRUCT because it's already defined as a WinAPI
structure and messing with that would be bad, so I can't implement toString()
or any helper functions within the struct. I need an outside function which
could do this automatically for any object/struct type.
Off the top of my head (untested):
----
void print(T)(T t) if (is(T == struct) || is(T == class))
{
foreach (i, field; t.tupleof)
{
writefln(T.tupleof[i].stringof ~ " = %s", field);
}
}
----
--
Robert
http://octarineparrot.com/