From c7e97b6113830343ad0ec801915bf97bbef3dca4 Mon Sep 17 00:00:00 2001 From: kiith-sa Date: Sat, 22 Sep 2012 22:45:37 +0200 Subject: [PATCH] User defined constructors now can construct all default types (e.g. float). --- dyaml/constructor.d | 4 ++-- dyaml/node.d | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/dyaml/constructor.d b/dyaml/constructor.d index 6ccc3d0..039c8d5 100644 --- a/dyaml/constructor.d +++ b/dyaml/constructor.d @@ -369,8 +369,8 @@ final class Constructor return (ref Node n) { - static if(Node.Value.allowed!T){return Node.Value(ctor(n));} - else {return Node.userValue(ctor(n));} + static if(Node.allowed!T){return Node.value(ctor(n));} + else {return Node.userValue(ctor(n));} }; } diff --git a/dyaml/node.d b/dyaml/node.d index 4e09031..de3ed71 100644 --- a/dyaml/node.d +++ b/dyaml/node.d @@ -1336,6 +1336,28 @@ struct Node 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. *