Changed the Constructor API (for loading of custom types) to
make it easier to load custom classes/structs. Updated API docs, tutorials and examples accordingly.
This commit is contained in:
parent
5547f62176
commit
548480b06b
19 changed files with 371 additions and 396 deletions
Binary file not shown.
Binary file not shown.
|
@ -20,8 +20,9 @@ Constructor
|
|||
D:YAML uses the `Constructor <../api/dyaml.constructor.html>`_ class to process
|
||||
each node to hold data type corresponding to its tag. *Constructor* stores a
|
||||
function for each supported tag to process it. These functions are supplied by
|
||||
the user using the *addConstructor()* method. *Constructor* is then passed to
|
||||
*Loader*, which parses YAML input.
|
||||
the user using the *addConstructorXXX()* methods, where *XXX* is *Scalar*,
|
||||
*Sequence* or *Mapping*. *Constructor* is then passed to *Loader*, which parses
|
||||
YAML input.
|
||||
|
||||
Struct types have no specific requirements for YAML support. Class types should
|
||||
define the *opEquals()* operator, as this is used in equality comparisons of
|
||||
|
@ -41,17 +42,20 @@ following struct:
|
|||
}
|
||||
|
||||
First, we need a function to construct our data type. It must take two *Mark*
|
||||
structs, which store position of the node in the file, and either a *string*, an
|
||||
array of *Node* or of *Node.Pair*, depending on whether we're constructing our
|
||||
value from a scalar, sequence, or mapping, respectively. In this tutorial, we
|
||||
have functions to construct a color from a scalar, using HTML-like format,
|
||||
RRGGBB, or from a mapping, where we use the following format:
|
||||
{r:RRR, g:GGG, b:BBB} . Code of these functions:
|
||||
structs, which store position of the node in the file, and a reference to *Node*
|
||||
to construct from. The node is guaranteed to contain either a *string*, an array
|
||||
of *Node* or of *Node.Pair*, depending on whether we're constructing our value
|
||||
from a scalar, sequence, or mapping, respectively. In this tutorial, we have
|
||||
functions to construct a color from a scalar, using CSS-like format, RRGGBB, or
|
||||
from a mapping, where we use the following format: {r:RRR, g:GGG, b:BBB} . Code
|
||||
of these functions:
|
||||
|
||||
.. code-block:: d
|
||||
|
||||
Color constructColorScalar(Mark start, Mark end, string value)
|
||||
Color constructColorScalar(Mark start, Mark end, ref Node node)
|
||||
{
|
||||
string value = node.get!string;
|
||||
|
||||
if(value.length != 6)
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
|
@ -82,30 +86,21 @@ RRGGBB, or from a mapping, where we use the following format:
|
|||
return result;
|
||||
}
|
||||
|
||||
Color constructColorMapping(Mark start, Mark end, Node.Pair[] pairs)
|
||||
Color constructColorMapping(Mark start, Mark end, ref Node node)
|
||||
{
|
||||
int r, g, b;
|
||||
r = g = b = -1;
|
||||
bool error = pairs.length != 3;
|
||||
int r,g,b;
|
||||
bool error = false;
|
||||
|
||||
foreach(ref pair; pairs)
|
||||
//Might throw if a value is missing or is not an integer.
|
||||
try
|
||||
{
|
||||
//Key might not be a string, and value might not be an int,
|
||||
//so we need to check for that
|
||||
try
|
||||
{
|
||||
switch(pair.key.get!string)
|
||||
{
|
||||
case "r": r = pair.value.get!int; break;
|
||||
case "g": g = pair.value.get!int; break;
|
||||
case "b": b = pair.value.get!int; break;
|
||||
default: error = true;
|
||||
}
|
||||
}
|
||||
catch(NodeException e)
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
r = node["r"].get!int;
|
||||
g = node["g"].get!int;
|
||||
b = node["b"].get!int;
|
||||
}
|
||||
catch(NodeException e)
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
|
||||
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
|
||||
|
@ -146,8 +141,8 @@ Finally, the code to put it all together:
|
|||
{
|
||||
auto constructor = new Constructor;
|
||||
//both functions handle the same tag, but one handles scalar, one mapping.
|
||||
constructor.addConstructor("!color", &constructColorScalar);
|
||||
constructor.addConstructor("!color-mapping", &constructColorMapping);
|
||||
constructor.addConstructorScalar("!color", &constructColorScalar);
|
||||
constructor.addConstructorMapping("!color-mapping", &constructColorMapping);
|
||||
|
||||
auto loader = Loader("input.yaml");
|
||||
loader.constructor = constructor;
|
||||
|
@ -284,7 +279,7 @@ With the following code, we will add support for dumping the our Color type.
|
|||
}
|
||||
|
||||
First we get the *Color* from the node. Then we convert it to a string with the
|
||||
HTML-like format we've used before. Finally, we use the *representScalar()*
|
||||
CSS-like format we've used before. Finally, we use the *representScalar()*
|
||||
method of *Representer* to get a scalar node ready for output.
|
||||
There are corresponding *representMapping()* and *representSequence()* methods
|
||||
as well, with examples in the
|
||||
|
|
|
@ -94,14 +94,15 @@
|
|||
</table></div>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">void <a name="addConstructor"></a><span class="ddoc_psymbol">addConstructor</span>(T, U)(in string <b>tag</b>, T function(Mark, Mark, U) <b>ctor</b>);
|
||||
<dt class="d_decl">void <a name="addConstructorScalar"></a><span class="ddoc_psymbol">addConstructorScalar</span>(T)(in string <b>tag</b>, T function(Mark, Mark, ref Node) <b>ctor</b>);
|
||||
</dt>
|
||||
<dd><p>Add a constructor function.
|
||||
<dd><p>Add a constructor function from scalar.
|
||||
</p>
|
||||
<p>The function passed must two Marks (determining start and end positions of
|
||||
the node in file) and either a string (if constructing from scalar),
|
||||
an array of Nodes (from sequence) or an array of Node.Pair (from mapping).
|
||||
The value returned by this function will be stored in the resulring node.
|
||||
<p>The function must take two Marks (start and end positions of
|
||||
the node in file) and a reference to Node to construct from.
|
||||
The node contains a string for scalars, Node[] for sequences and
|
||||
Node.Pair[] for mappings.
|
||||
The value returned by this function will be stored in the resulting node.
|
||||
<br>
|
||||
|
||||
Only one constructor function can be set for one tag.
|
||||
|
@ -113,47 +114,61 @@
|
|||
<td valign=top>Constructor function.</td></tr>
|
||||
</table></div>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">void <a name="addConstructorSequence"></a><span class="ddoc_psymbol">addConstructorSequence</span>(T)(in string <b>tag</b>, T function(Mark, Mark, ref Node) <b>ctor</b>);
|
||||
</dt>
|
||||
<dd><p>Add a constructor function from sequence.
|
||||
</p>
|
||||
<b>See Also:</b><div class="pbr">addConstructorScalar</div>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">void <a name="addConstructorMapping"></a><span class="ddoc_psymbol">addConstructorMapping</span>(T)(in string <b>tag</b>, T function(Mark, Mark, ref Node) <b>ctor</b>);
|
||||
</dt>
|
||||
<dd><p>Add a constructor function from a mapping.
|
||||
</p>
|
||||
<b>See Also:</b><div class="pbr">addConstructorScalar</div>
|
||||
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
<dt class="d_decl">YAMLNull <a name="constructNull"></a><span class="ddoc_psymbol">constructNull</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">YAMLNull <a name="constructNull"></a><span class="ddoc_psymbol">constructNull</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a <b>null</b> node.</p>
|
||||
<dd><p>Construct a <b>null</b> <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">YAMLMerge <a name="constructMerge"></a><span class="ddoc_psymbol">constructMerge</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">YAMLMerge <a name="constructMerge"></a><span class="ddoc_psymbol">constructMerge</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a merge node - a node that merges another node into a mapping.</p>
|
||||
<dd><p>Construct a merge <b>node</b> - a <b>node</b> that merges another <b>node</b> into a mapping.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">bool <a name="constructBool"></a><span class="ddoc_psymbol">constructBool</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">bool <a name="constructBool"></a><span class="ddoc_psymbol">constructBool</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a boolean node.</p>
|
||||
<dd><p>Construct a boolean <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">long <a name="constructLong"></a><span class="ddoc_psymbol">constructLong</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">long <a name="constructLong"></a><span class="ddoc_psymbol">constructLong</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct an integer (long) node.</p>
|
||||
<dd><p>Construct an integer (long) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">real <a name="constructReal"></a><span class="ddoc_psymbol">constructReal</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">real <a name="constructReal"></a><span class="ddoc_psymbol">constructReal</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a floating point (real) node.</p>
|
||||
<dd><p>Construct a floating point (real) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">ubyte[] <a name="constructBinary"></a><span class="ddoc_psymbol">constructBinary</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">ubyte[] <a name="constructBinary"></a><span class="ddoc_psymbol">constructBinary</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a binary (base64) node.</p>
|
||||
<dd><p>Construct a binary (base64) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">SysTime <a name="constructTimestamp"></a><span class="ddoc_psymbol">constructTimestamp</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">SysTime <a name="constructTimestamp"></a><span class="ddoc_psymbol">constructTimestamp</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a timestamp (SysTime) node.</p>
|
||||
<dd><p>Construct a timestamp (SysTime) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">string <a name="constructString"></a><span class="ddoc_psymbol">constructString</span>(Mark <b>start</b>, Mark <b>end</b>, string <b>value</b>);
|
||||
<dt class="d_decl">string <a name="constructString"></a><span class="ddoc_psymbol">constructString</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a string node.</p>
|
||||
<dd><p>Construct a string <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="getPairs"></a><span class="ddoc_psymbol">getPairs</span>(string <b>type</b>, Mark <b>start</b>, Mark <b>end</b>, Node[] <b>nodes</b>);
|
||||
|
@ -161,29 +176,29 @@
|
|||
<dd><p>Convert a sequence of single-element mappings into a sequence of pairs.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="constructOrderedMap"></a><span class="ddoc_psymbol">constructOrderedMap</span>(Mark <b>start</b>, Mark <b>end</b>, Node[] <b>nodes</b>);
|
||||
<dt class="d_decl">Pair[] <a name="constructOrderedMap"></a><span class="ddoc_psymbol">constructOrderedMap</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct an ordered map (ordered sequence of key:value pairs without duplicates) node.</p>
|
||||
<dd><p>Construct an ordered map (ordered sequence of key:value pairs without duplicates) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="constructPairs"></a><span class="ddoc_psymbol">constructPairs</span>(Mark <b>start</b>, Mark <b>end</b>, Node[] <b>nodes</b>);
|
||||
<dt class="d_decl">Pair[] <a name="constructPairs"></a><span class="ddoc_psymbol">constructPairs</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a pairs (ordered sequence of key: value pairs allowing duplicates) node.</p>
|
||||
<dd><p>Construct a pairs (ordered sequence of key: value pairs allowing duplicates) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Node[] <a name="constructSet"></a><span class="ddoc_psymbol">constructSet</span>(Mark <b>start</b>, Mark <b>end</b>, Pair[] <b>pairs</b>);
|
||||
<dt class="d_decl">Node[] <a name="constructSet"></a><span class="ddoc_psymbol">constructSet</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a set node.</p>
|
||||
<dd><p>Construct a set <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Node[] <a name="constructSequence"></a><span class="ddoc_psymbol">constructSequence</span>(Mark <b>start</b>, Mark <b>end</b>, Node[] <b>nodes</b>);
|
||||
<dt class="d_decl">Node[] <a name="constructSequence"></a><span class="ddoc_psymbol">constructSequence</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a sequence (array) node.</p>
|
||||
<dd><p>Construct a sequence (array) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="constructMap"></a><span class="ddoc_psymbol">constructMap</span>(Mark <b>start</b>, Mark <b>end</b>, Pair[] <b>pairs</b>);
|
||||
<dt class="d_decl">Pair[] <a name="constructMap"></a><span class="ddoc_psymbol">constructMap</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct an unordered map (unordered set of key: value pairs without duplicates) node.</p>
|
||||
<dd><p>Construct an unordered map (unordered set of key: value pairs without duplicates) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
</dl>
|
||||
|
|
|
@ -138,7 +138,7 @@ struct appears in Phobos.</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 15, 2011.
|
||||
Last updated on Oct 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -104,7 +104,7 @@
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 15, 2011.
|
||||
Last updated on Oct 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -87,7 +87,7 @@
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 15, 2011.
|
||||
Last updated on Oct 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -60,8 +60,9 @@ It is also possible to implicitly resolve custom tags, as we will show later.</p
|
|||
<p>D:YAML uses the <a class="reference external" href="../api/dyaml.constructor.html">Constructor</a> class to process
|
||||
each node to hold data type corresponding to its tag. <em>Constructor</em> stores a
|
||||
function for each supported tag to process it. These functions are supplied by
|
||||
the user using the <em>addConstructor()</em> method. <em>Constructor</em> is then passed to
|
||||
<em>Loader</em>, which parses YAML input.</p>
|
||||
the user using the <em>addConstructorXXX()</em> methods, where <em>XXX</em> is <em>Scalar</em>,
|
||||
<em>Sequence</em> or <em>Mapping</em>. <em>Constructor</em> is then passed to <em>Loader</em>, which parses
|
||||
YAML input.</p>
|
||||
<p>Struct types have no specific requirements for YAML support. Class types should
|
||||
define the <em>opEquals()</em> operator, as this is used in equality comparisons of
|
||||
nodes. Default class <em>opEquals()</em> compares references, which means two identical
|
||||
|
@ -77,14 +78,17 @@ following struct:</p>
|
|||
</pre></div>
|
||||
</div>
|
||||
<p>First, we need a function to construct our data type. It must take two <em>Mark</em>
|
||||
structs, which store position of the node in the file, and either a <em>string</em>, an
|
||||
array of <em>Node</em> or of <em>Node.Pair</em>, depending on whether we’re constructing our
|
||||
value from a scalar, sequence, or mapping, respectively. In this tutorial, we
|
||||
have functions to construct a color from a scalar, using HTML-like format,
|
||||
RRGGBB, or from a mapping, where we use the following format:
|
||||
{r:RRR, g:GGG, b:BBB} . Code of these functions:</p>
|
||||
<div class="highlight-d"><div class="highlight"><pre><span class="n">Color</span> <span class="n">constructColorScalar</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="nb">string</span> <span class="n">value</span><span class="p">)</span>
|
||||
structs, which store position of the node in the file, and a reference to <em>Node</em>
|
||||
to construct from. The node is guaranteed to contain either a <em>string</em>, an array
|
||||
of <em>Node</em> or of <em>Node.Pair</em>, depending on whether we’re constructing our value
|
||||
from a scalar, sequence, or mapping, respectively. In this tutorial, we have
|
||||
functions to construct a color from a scalar, using CSS-like format, RRGGBB, or
|
||||
from a mapping, where we use the following format: {r:RRR, g:GGG, b:BBB} . Code
|
||||
of these functions:</p>
|
||||
<div class="highlight-d"><div class="highlight"><pre><span class="n">Color</span> <span class="n">constructColorScalar</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="nb">string</span> <span class="n">value</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">get</span><span class="p">!</span><span class="nb">string</span><span class="p">;</span>
|
||||
|
||||
<span class="k">if</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">length</span> <span class="p">!=</span> <span class="mi">6</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">"Invalid color: "</span> <span class="p">~</span> <span class="n">value</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">);</span>
|
||||
|
@ -115,30 +119,21 @@ RRGGBB, or from a mapping, where we use the following format:
|
|||
<span class="k">return</span> <span class="n">result</span><span class="p">;</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="n">Node</span><span class="p">.</span><span class="n">Pair</span><span class="p">[]</span> <span class="n">pairs</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="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="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="p">-</span><span class="mi">1</span><span class="p">;</span>
|
||||
<span class="kt">bool</span> <span class="n">error</span> <span class="p">=</span> <span class="n">pairs</span><span class="p">.</span><span class="n">length</span> <span class="p">!=</span> <span class="mi">3</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">bool</span> <span class="n">error</span> <span class="p">=</span> <span class="kc">false</span><span class="p">;</span>
|
||||
|
||||
<span class="k">foreach</span><span class="p">(</span><span class="k">ref</span> <span class="n">pair</span><span class="p">;</span> <span class="n">pairs</span><span class="p">)</span>
|
||||
<span class="c1">//Might throw if a value is missing or is not an integer.</span>
|
||||
<span class="k">try</span>
|
||||
<span class="p">{</span>
|
||||
<span class="c1">//Key might not be a string, and value might not be an int,</span>
|
||||
<span class="c1">//so we need to check for that</span>
|
||||
<span class="k">try</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">switch</span><span class="p">(</span><span class="n">pair</span><span class="p">.</span><span class="n">key</span><span class="p">.</span><span class="n">get</span><span class="p">!</span><span class="nb">string</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">case</span> <span class="s">"r"</span><span class="p">:</span> <span class="n">r</span> <span class="p">=</span> <span class="n">pair</span><span class="p">.</span><span class="n">value</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="k">break</span><span class="p">;</span>
|
||||
<span class="k">case</span> <span class="s">"g"</span><span class="p">:</span> <span class="n">g</span> <span class="p">=</span> <span class="n">pair</span><span class="p">.</span><span class="n">value</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="k">break</span><span class="p">;</span>
|
||||
<span class="k">case</span> <span class="s">"b"</span><span class="p">:</span> <span class="n">b</span> <span class="p">=</span> <span class="n">pair</span><span class="p">.</span><span class="n">value</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="k">break</span><span class="p">;</span>
|
||||
<span class="k">default</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="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="p">{</span>
|
||||
<span class="n">error</span> <span class="p">=</span> <span class="kc">true</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">"r"</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">"g"</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">"b"</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="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="n">error</span> <span class="p">=</span> <span class="kc">true</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"><</span> <span class="mi">0</span> <span class="p">||</span> <span class="n">r</span> <span class="p">></span> <span class="mi">255</span> <span class="p">||</span> <span class="n">g</span> <span class="p"><</span> <span class="mi">0</span> <span class="p">||</span> <span class="n">g</span> <span class="p">></span> <span class="mi">255</span> <span class="p">||</span> <span class="n">b</span> <span class="p"><</span> <span class="mi">0</span> <span class="p">||</span> <span class="n">b</span> <span class="p">></span> <span class="mi">255</span><span class="p">)</span>
|
||||
|
@ -174,8 +169,8 @@ RRGGBB, or from a mapping, where we use the following format:
|
|||
<span class="p">{</span>
|
||||
<span class="k">auto</span> <span class="n">constructor</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Constructor</span><span class="p">;</span>
|
||||
<span class="c1">//both functions handle the same tag, but one handles scalar, one mapping.</span>
|
||||
<span class="n">constructor</span><span class="p">.</span><span class="n">addConstructor</span><span class="p">(</span><span class="s">"!color"</span><span class="p">,</span> <span class="p">&</span><span class="n">constructColorScalar</span><span class="p">);</span>
|
||||
<span class="n">constructor</span><span class="p">.</span><span class="n">addConstructor</span><span class="p">(</span><span class="s">"!color-mapping"</span><span class="p">,</span> <span class="p">&</span><span class="n">constructColorMapping</span><span class="p">);</span>
|
||||
<span class="n">constructor</span><span class="p">.</span><span class="n">addConstructorScalar</span><span class="p">(</span><span class="s">"!color"</span><span class="p">,</span> <span class="p">&</span><span class="n">constructColorScalar</span><span class="p">);</span>
|
||||
<span class="n">constructor</span><span class="p">.</span><span class="n">addConstructorMapping</span><span class="p">(</span><span class="s">"!color-mapping"</span><span class="p">,</span> <span class="p">&</span><span class="n">constructColorMapping</span><span class="p">);</span>
|
||||
|
||||
<span class="k">auto</span> <span class="n">loader</span> <span class="p">=</span> <span class="n">Loader</span><span class="p">(</span><span class="s">"input.yaml"</span><span class="p">);</span>
|
||||
<span class="n">loader</span><span class="p">.</span><span class="n">constructor</span> <span class="p">=</span> <span class="n">constructor</span><span class="p">;</span>
|
||||
|
@ -292,7 +287,7 @@ loading the output.</p>
|
|||
</pre></div>
|
||||
</div>
|
||||
<p>First we get the <em>Color</em> from the node. Then we convert it to a string with the
|
||||
HTML-like format we’ve used before. Finally, we use the <em>representScalar()</em>
|
||||
CSS-like format we’ve used before. Finally, we use the <em>representScalar()</em>
|
||||
method of <em>Representer</em> to get a scalar node ready for output.
|
||||
There are corresponding <em>representMapping()</em> and <em>representSequence()</em> methods
|
||||
as well, with examples in the
|
||||
|
@ -380,7 +375,7 @@ directory of the D:YAML package.</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 15, 2011.
|
||||
Last updated on Oct 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -237,7 +237,7 @@ example in the <tt class="docutils literal"><span class="pre">example/getting_st
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 15, 2011.
|
||||
Last updated on Oct 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -330,7 +330,7 @@ Some of these might change in the future (especially !!map and !!set).</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Oct 15, 2011.
|
||||
Last updated on Oct 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue