Red-Black Trees are now used for duplicate detection, and planned

to be used for unordered map storage. This is because AAs still
don't work correctly and even if they did, require the user to
define both toHash and opCmp/opEquals for every YAML
struct/class. Now only opCmp needs to be defined.
Documentation/tutorials/examples have been updated accordingly.
This commit is contained in:
Ferdinand Majerech 2012-01-23 15:57:26 +01:00
parent 07eadc9403
commit 9596806644
34 changed files with 623 additions and 250 deletions

View file

@ -52,7 +52,7 @@
</dd>
<dt class="d_decl">class <a name="Representer"></a><span class="ddoc_psymbol">Representer</span>;
</dt>
<dd><p>Represents YAML nodes of various data types as scalar, sequence and mapping nodes ready for output.
<dd><p>Represents YAML nodes as scalar, sequence and mapping nodes ready for output.
</p>
<p>This class is used to add support for dumping of custom data types.
<br>
@ -73,26 +73,38 @@
</dd>
<dt class="d_decl">@property void <a name="defaultScalarStyle"></a><span class="ddoc_psymbol">defaultScalarStyle</span>(ScalarStyle <b>style</b>);
</dt>
<dd><p>Set default style for scalars. Invalid means the style is chosen automatically.</p>
<dd><p>Set default style for scalars. If <b>style</b> is <span class="d_inlinecode">ScalarStyle.Invalid</span>, the style is chosen automatically.</p>
</dd>
<dt class="d_decl">@property void <a name="defaultCollectionStyle"></a><span class="ddoc_psymbol">defaultCollectionStyle</span>(CollectionStyle <b>style</b>);
</dt>
<dd><p>Set default style for collections. Invalid means the style is chosen automatically. </p>
<dd><p>Set default style for collections. If <b>style</b> is <span class="d_inlinecode">CollectionStyle.Invalid</span>, the style is chosen automatically. </p>
</dd>
<dt class="d_decl">void <a name="addRepresenter"></a><span class="ddoc_psymbol">addRepresenter</span>(T)(Node function(ref Node, Representer) <b>representer</b>);
</dt>
<dd><p>Add a function to represent nodes with a specific data type.
</p>
<p>The representer function takes references to a Node storing the data
type and to the Representer. It returns the represented node and may
throw a RepresenterException. See the example for more information.
<p>The representer function takes references to a <span class="d_inlinecode">Node</span> storing the data
type and to the <span class="d_inlinecode">Representer</span>. It returns the represented node and may
throw a <span class="d_inlinecode">RepresenterException</span>. See the example for more information.
<br>
<br>
Only one function may be specified for one data type. Default data
types already have representer functions unless disabled in the
Representer constructor.
<span class="d_inlinecode">Representer</span> constructor.
<br>
<br>
Structs and classes must implement the <span class="d_inlinecode">opCmp()</span> operator for D:YAML
support. The signature of the operator that must be implemented
is <span class="d_inlinecode">const int opCmp(ref const MyStruct s)</span> for structs where
<i>MyStruct</i> is the struct type, and <span class="d_inlinecode">int opCmp(Object o)</span> for
classes. Note that the class <span class="d_inlinecode">opCmp()</span> should not alter the compared
values - it is not const for compatibility reasons.
</p>
<b>Parameters:</b><div class="pbr"><table class=parms><tr><td valign=top>representer</td>
@ -106,6 +118,16 @@
<span class="d_keyword">struct</span> MyStruct
{
<span class="d_keyword">int</span> x, y, z;
<span class="d_comment">//Any D:YAML type must have a custom opCmp operator.
</span> <span class="d_comment">//This is used for ordering in mappings.
</span> <span class="d_keyword">const</span> <span class="d_keyword">int</span> opCmp(<span class="d_keyword">ref</span> <span class="d_keyword">const</span> MyStruct s)
{
<span class="d_keyword">if</span>(x != s.x){<span class="d_keyword">return</span> x - s.x;}
<span class="d_keyword">if</span>(y != s.y){<span class="d_keyword">return</span> y - s.y;}
<span class="d_keyword">if</span>(z != s.z){<span class="d_keyword">return</span> z - s.z;}
<span class="d_keyword">return</span> 0;
}
}
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
@ -144,12 +166,16 @@
<span class="d_keyword">this</span>.z = z;
}
<span class="d_comment">///We need custom opEquals for node equality, as default opEquals compares references.
</span> <span class="d_keyword">override</span> <span class="d_keyword">bool</span> opEquals(Object rhs)
<span class="d_comment">//Any D:YAML type must have a custom opCmp operator.
</span> <span class="d_comment">//This is used for ordering in mappings.
</span> <span class="d_keyword">override</span> <span class="d_keyword">int</span> opCmp(Object o)
{
<span class="d_keyword">if</span>(<span class="d_keyword">typeid</span>(rhs) != <span class="d_keyword">typeid</span>(MyClass)){<span class="d_keyword">return</span> <span class="d_keyword">false</span>;}
<span class="d_keyword">auto</span> t = <span class="d_keyword">cast</span>(MyClass)rhs;
<span class="d_keyword">return</span> x == t.x &amp;&amp; y == t.y &amp;&amp; z == t.z;
MyClass s = <span class="d_keyword">cast</span>(MyClass)o;
<span class="d_keyword">if</span>(s <span class="d_keyword">is</span> <span class="d_keyword">null</span>){<span class="d_keyword">return</span> -1;}
<span class="d_keyword">if</span>(x != s.x){<span class="d_keyword">return</span> x - s.x;}
<span class="d_keyword">if</span>(y != s.y){<span class="d_keyword">return</span> y - s.y;}
<span class="d_keyword">if</span>(z != s.z){<span class="d_keyword">return</span> z - s.z;}
<span class="d_keyword">return</span> 0;
}
<span class="d_comment">///Useful for Node.as!string .
@ -204,6 +230,16 @@
<pre class="d_code"> <span class="d_keyword">struct</span> MyStruct
{
<span class="d_keyword">int</span> x, y, z;
<span class="d_comment">//Any D:YAML type must have a custom opCmp operator.
</span> <span class="d_comment">//This is used for ordering in mappings.
</span> <span class="d_keyword">const</span> <span class="d_keyword">int</span> opCmp(<span class="d_keyword">ref</span> <span class="d_keyword">const</span> MyStruct s)
{
<span class="d_keyword">if</span>(x != s.x){<span class="d_keyword">return</span> x - s.x;}
<span class="d_keyword">if</span>(y != s.y){<span class="d_keyword">return</span> y - s.y;}
<span class="d_keyword">if</span>(z != s.z){<span class="d_keyword">return</span> z - s.z;}
<span class="d_keyword">return</span> 0;
}
}
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
@ -211,7 +247,6 @@
<span class="d_keyword">auto</span> value = node.as!MyStruct;
<span class="d_keyword">auto</span> <span class="d_param">scalar</span> = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
<span class="d_keyword">return</span> representer.<span class="d_psymbol">representScalar</span>(<span class="d_string">"!mystruct.tag"</span>, <span class="d_param">scalar</span>);
}
</pre>
</p>
@ -225,7 +260,7 @@
</p>
<b>Parameters:</b><div class="pbr"><table class=parms><tr><td valign=top>string <b>tag</b></td>
<td valign=top>Tag of the <b>sequence</b>.</td></tr>
<td valign=top>Tag of the sequence.</td></tr>
<tr><td valign=top>Node[] <b>sequence</b></td>
<td valign=top>Sequence of nodes.</td></tr>
<tr><td valign=top>CollectionStyle <b>style</b></td>
@ -235,13 +270,23 @@
<b>Returns:</b><div class="pbr">The represented node.
</div>
<b>Throws:</b><div class="pbr">RepresenterException if a child could not be represented.
<b>Throws:</b><div class="pbr"><span class="d_inlinecode">RepresenterException</span> if a child could not be represented.
</div>
<p><b>Example:</b><br>
<pre class="d_code"> <span class="d_keyword">struct</span> MyStruct
{
<span class="d_keyword">int</span> x, y, z;
<span class="d_comment">//Any D:YAML type must have a custom opCmp operator.
</span> <span class="d_comment">//This is used for ordering in mappings.
</span> <span class="d_keyword">const</span> <span class="d_keyword">int</span> opCmp(<span class="d_keyword">ref</span> <span class="d_keyword">const</span> MyStruct s)
{
<span class="d_keyword">if</span>(x != s.x){<span class="d_keyword">return</span> x - s.x;}
<span class="d_keyword">if</span>(y != s.y){<span class="d_keyword">return</span> y - s.y;}
<span class="d_keyword">if</span>(z != s.z){<span class="d_keyword">return</span> z - s.z;}
<span class="d_keyword">return</span> 0;
}
}
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
@ -274,13 +319,23 @@
<b>Returns:</b><div class="pbr">The represented node.
</div>
<b>Throws:</b><div class="pbr">RepresenterException if a child could not be represented.
<b>Throws:</b><div class="pbr"><span class="d_inlinecode">RepresenterException</span> if a child could not be represented.
</div>
<p><b>Example:</b><br>
<pre class="d_code"> <span class="d_keyword">struct</span> MyStruct
{
<span class="d_keyword">int</span> x, y, z;
<span class="d_comment">//Any D:YAML type must have a custom opCmp operator.
</span> <span class="d_comment">//This is used for ordering in mappings.
</span> <span class="d_keyword">const</span> <span class="d_keyword">int</span> opCmp(<span class="d_keyword">ref</span> <span class="d_keyword">const</span> MyStruct s)
{
<span class="d_keyword">if</span>(x != s.x){<span class="d_keyword">return</span> x - s.x;}
<span class="d_keyword">if</span>(y != s.y){<span class="d_keyword">return</span> y - s.y;}
<span class="d_keyword">if</span>(z != s.z){<span class="d_keyword">return</span> z - s.z;}
<span class="d_keyword">return</span> 0;
}
}
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
@ -348,7 +403,7 @@
<div id="copyright">
Copyright &copy; Ferdinand Majerech 2011. Based on <a href="http://www.pyyaml.org">PyYAML</a> by Kirill Simonov. |
Page generated by Autodoc and <a href="http://www.digitalmars.com/d/2.0/ddoc.html">Ddoc</a>.
Page generated by AutoDDoc and <a href="http://www.digitalmars.com/d/2.0/ddoc.html">Ddoc</a>.
</div>
</body>