User defined constructors now can construct all default types (e.g. float).

This commit is contained in:
kiith-sa 2012-09-22 22:45:37 +02:00
parent edf3e2a799
commit c7e97b6113
2 changed files with 24 additions and 2 deletions

View file

@ -369,7 +369,7 @@ final class Constructor
return (ref Node n) return (ref Node n)
{ {
static if(Node.Value.allowed!T){return Node.Value(ctor(n));} static if(Node.allowed!T){return Node.value(ctor(n));}
else {return Node.userValue(ctor(n));} else {return Node.userValue(ctor(n));}
}; };
} }

View file

@ -1336,6 +1336,28 @@ struct Node
return Value(cast(YAMLObject)new YAMLContainer!T(value)); return Value(cast(YAMLObject)new YAMLContainer!T(value));
} }
//Construct Node.Value from a type it can store directly (after casting if needed)
static Value value(T)(T value) @trusted if(allowed!T)
{
static if(Value.allowed!T)
{
return Value(value);
}
else static if(isIntegral!T)
{
return Value(cast(long)(value));
}
else static if(isFloatingPoint!T)
{
return Value(cast(real)(value));
}
else static if(isSomeString!T)
{
return Value(to!string(value));
}
else static assert(false, "Unknown value type. Is value() in sync with allowed()?");
}
/* /*
* Equality test with any value. * Equality test with any value.
* *