Simplified the Constructor and Resolver example -

no need for range checks as they are handled in Node itself.
This commit is contained in:
Ferdinand Majerech 2011-10-17 13:13:02 +02:00
parent 548480b06b
commit 009017eef0
10 changed files with 48 additions and 69 deletions

Binary file not shown.

View file

@ -88,24 +88,18 @@ of these functions:
Color constructColorMapping(Mark start, Mark end, ref Node node) Color constructColorMapping(Mark start, Mark end, ref Node node)
{ {
int r,g,b; ubyte r,g,b;
bool error = false;
//Might throw if a value is missing or is not an integer. //Might throw if a value is missing is not an integer, or is out of range.
try try
{ {
r = node["r"].get!int; r = node["r"].get!ubyte;
g = node["g"].get!int; g = node["g"].get!ubyte;
b = node["b"].get!int; b = node["b"].get!ubyte;
} }
catch(NodeException e) catch(NodeException e)
{ {
error = true; throw new ConstructorException("Invalid color: " ~ e.msg, start, end);
}
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
{
throw new ConstructorException("Invalid color", start, end);
} }
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b); return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);

View file

@ -280,6 +280,10 @@
conversion is attempted. conversion is attempted.
<br> <br>
Numeric values are range checked, throwing if out of range of
requested type.
<br>
Timestamps are stored as std.datetime.SysTime. Timestamps are stored as std.datetime.SysTime.
Binary values are decoded and stored as ubyte[]. Binary values are decoded and stored as ubyte[].
<br> <br>
@ -308,7 +312,8 @@
<b>Returns:</b><div class="pbr">Value of the node as specified type. <b>Returns:</b><div class="pbr">Value of the node as specified type.
</div> </div>
<b>Throws:</b><div class="pbr">NodeException if unable to convert to specified type.</div> <b>Throws:</b><div class="pbr">NodeException if unable to convert to specified type, or if
the value is out of range of requested type.</div>
</dd> </dd>
<dt class="d_decl">void <a name="getToVar"></a><span class="ddoc_psymbol">getToVar</span>(T)(out T <b>target</b>); <dt class="d_decl">void <a name="getToVar"></a><span class="ddoc_psymbol">getToVar</span>(T)(out T <b>target</b>);

File diff suppressed because one or more lines are too long

View file

@ -121,24 +121,18 @@ of these functions:</p>
<span class="n">Color</span> <span class="n">constructColorMapping</span><span class="p">(</span><span class="n">Mark</span> <span class="n">start</span><span class="p">,</span> <span class="n">Mark</span> <span class="n">end</span><span class="p">,</span> <span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span> <span class="n">Color</span> <span class="n">constructColorMapping</span><span class="p">(</span><span class="n">Mark</span> <span class="n">start</span><span class="p">,</span> <span class="n">Mark</span> <span class="n">end</span><span class="p">,</span> <span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
<span class="p">{</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">r</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">b</span><span class="p">;</span> <span class="kt">ubyte</span> <span class="n">r</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">b</span><span class="p">;</span>
<span class="kt">bool</span> <span class="n">error</span> <span class="p">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="c1">//Might throw if a value is missing or is not an integer.</span> <span class="c1">//Might throw if a value is missing is not an integer, or is out of range.</span>
<span class="k">try</span> <span class="k">try</span>
<span class="p">{</span> <span class="p">{</span>
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;r&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">int</span><span class="p">;</span> <span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;r&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;g&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">int</span><span class="p">;</span> <span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;g&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;b&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">int</span><span class="p">;</span> <span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;b&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">catch</span><span class="p">(</span><span class="n">NodeException</span> <span class="n">e</span><span class="p">)</span> <span class="k">catch</span><span class="p">(</span><span class="n">NodeException</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span> <span class="p">{</span>
<span class="n">error</span> <span class="p">=</span> <span class="kc">true</span><span class="p">;</span> <span class="k">throw</span> <span class="k">new</span> <span class="n">ConstructorException</span><span class="p">(</span><span class="s">&quot;Invalid color: &quot;</span> <span class="p">~</span> <span class="n">e</span><span class="p">.</span><span class="n">msg</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="n">error</span> <span class="p">||</span> <span class="n">r</span> <span class="p">&lt;</span> <span class="mi">0</span> <span class="p">||</span> <span class="n">r</span> <span class="p">&gt;</span> <span class="mi">255</span> <span class="p">||</span> <span class="n">g</span> <span class="p">&lt;</span> <span class="mi">0</span> <span class="p">||</span> <span class="n">g</span> <span class="p">&gt;</span> <span class="mi">255</span> <span class="p">||</span> <span class="n">b</span> <span class="p">&lt;</span> <span class="mi">0</span> <span class="p">||</span> <span class="n">b</span> <span class="p">&gt;</span> <span class="mi">255</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">ConstructorException</span><span class="p">(</span><span class="s">&quot;Invalid color&quot;</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">);</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">return</span> <span class="n">Color</span><span class="p">(</span><span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">r</span><span class="p">,</span> <span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">g</span><span class="p">,</span> <span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">b</span><span class="p">);</span> <span class="k">return</span> <span class="n">Color</span><span class="p">(</span><span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">r</span><span class="p">,</span> <span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">g</span><span class="p">,</span> <span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">b</span><span class="p">);</span>

View file

@ -88,24 +88,18 @@ of these functions:
Color constructColorMapping(Mark start, Mark end, ref Node node) Color constructColorMapping(Mark start, Mark end, ref Node node)
{ {
int r,g,b; ubyte r,g,b;
bool error = false;
//Might throw if a value is missing or is not an integer. //Might throw if a value is missing is not an integer, or is out of range.
try try
{ {
r = node["r"].get!int; r = node["r"].get!ubyte;
g = node["g"].get!int; g = node["g"].get!ubyte;
b = node["b"].get!int; b = node["b"].get!ubyte;
} }
catch(NodeException e) catch(NodeException e)
{ {
error = true; throw new ConstructorException("Invalid color: " ~ e.msg, start, end);
}
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
{
throw new ConstructorException("Invalid color", start, end);
} }
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b); return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);

View file

@ -456,6 +456,9 @@ struct Node
* If the specifed type does not match type in the node, * If the specifed type does not match type in the node,
* conversion is attempted. * conversion is attempted.
* *
* Numeric values are range checked, throwing if out of range of
* requested type.
*
* Timestamps are stored as std.datetime.SysTime. * Timestamps are stored as std.datetime.SysTime.
* Binary values are decoded and stored as ubyte[]. * Binary values are decoded and stored as ubyte[].
* *
@ -483,7 +486,8 @@ struct Node
* *
* Returns: Value of the node as specified type. * Returns: Value of the node as specified type.
* *
* Throws: NodeException if unable to convert to specified type. * Throws: NodeException if unable to convert to specified type, or if
* the value is out of range of requested type.
*/ */
@property T get(T)() @property T get(T)()
{ {

View file

@ -46,24 +46,18 @@ Color constructColorScalar(Mark start, Mark end, ref Node node)
Color constructColorMapping(Mark start, Mark end, ref Node node) Color constructColorMapping(Mark start, Mark end, ref Node node)
{ {
int r,g,b; ubyte r,g,b;
bool error = false;
//Might throw if a value is missing or is not an integer. //Might throw if a value is missing is not an integer, or is out of range.
try try
{ {
r = node["r"].get!int; r = node["r"].get!ubyte;
g = node["g"].get!int; g = node["g"].get!ubyte;
b = node["b"].get!int; b = node["b"].get!ubyte;
} }
catch(NodeException e) catch(NodeException e)
{ {
error = true; throw new ConstructorException("Invalid color: " ~ e.msg, start, end);
}
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
{
throw new ConstructorException("Invalid color", start, end);
} }
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b); return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);

View file

@ -46,24 +46,18 @@ Color constructColorScalar(Mark start, Mark end, ref Node node)
Color constructColorMapping(Mark start, Mark end, ref Node node) Color constructColorMapping(Mark start, Mark end, ref Node node)
{ {
int r,g,b; ubyte r,g,b;
bool error = false;
//Might throw if a value is missing or is not an integer. //Might throw if a value is missing is not an integer, or is out of range.
try try
{ {
r = node["r"].get!int; r = node["r"].get!ubyte;
g = node["g"].get!int; g = node["g"].get!ubyte;
b = node["b"].get!int; b = node["b"].get!ubyte;
} }
catch(NodeException e) catch(NodeException e)
{ {
error = true; throw new ConstructorException("Invalid color: " ~ e.msg, start, end);
}
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
{
throw new ConstructorException("Invalid color", start, end);
} }
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b); return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);