More constness in Node.

This commit is contained in:
Ferdinand Majerech 2011-10-30 10:52:40 +01:00
parent b3a51800f7
commit 508696584e

View file

@ -55,7 +55,11 @@ package enum NodeID : ubyte
} }
///Null YAML type. Used in nodes with _null values. ///Null YAML type. Used in nodes with _null values.
struct YAMLNull{} struct YAMLNull
{
///Used for string conversion.
string toString() const {return "null";}
}
//Merge YAML type, used to support "tag:yaml.org,2002:merge". //Merge YAML type, used to support "tag:yaml.org,2002:merge".
package struct YAMLMerge{} package struct YAMLMerge{}
@ -609,10 +613,10 @@ struct Node
* *
* Throws: NodeException if this is not a sequence nor a mapping. * Throws: NodeException if this is not a sequence nor a mapping.
*/ */
@property size_t length() @property size_t length() const
{ {
if(isSequence) {return as!(Node[]).length;} if(isSequence) {return value_.get!(const Node[]).length;}
else if(isMapping){return as!(Pair[]).length;} else if(isMapping){return value_.get!(const Pair[]).length;}
throw new Error("Trying to get length of a scalar node", startMark_); throw new Error("Trying to get length of a scalar node", startMark_);
} }
@ -1277,19 +1281,12 @@ struct Node
} }
//Determine if the value can be converted to specified type. //Determine if the value can be converted to specified type.
bool convertsTo(T)() bool convertsTo(T)() const
{ {
if(isType!T){return true;} if(isType!T){return true;}
static if(isSomeString!T) //Every type allowed in Value should be convertible to string.
{ static if(isSomeString!T) {return true;}
try
{
auto dummy = value_.coerce!T();
return true;
}
catch(VariantException e){return false;}
}
else static if(isFloatingPoint!T){return isInt() || isFloat();} else static if(isFloatingPoint!T){return isInt() || isFloat();}
else static if(isIntegral!T) {return isInt();} else static if(isIntegral!T) {return isInt();}
else {return false;} else {return false;}
@ -1320,10 +1317,7 @@ struct Node
} }
else else
{ {
try try if(node.as!T == index){return idx;}
{
if(node.as!T == index){return idx;}
}
catch(NodeException e) catch(NodeException e)
{ {
continue; continue;
@ -1334,7 +1328,7 @@ struct Node
} }
//Check if index is integral and in range. //Check if index is integral and in range.
void checkSequenceIndex(T)(T index) void checkSequenceIndex(T)(T index) const
{ {
static if(!isIntegral!T) static if(!isIntegral!T)
{ {
@ -1342,7 +1336,7 @@ struct Node
} }
else else
{ {
enforce(index >= 0 && index < value_.get!(Node[]).length, enforce(index >= 0 && index < value_.get!(const Node[]).length,
new Error("Sequence index out of range: " ~ to!string(index), new Error("Sequence index out of range: " ~ to!string(index),
startMark_)); startMark_));
} }