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:
parent
07eadc9403
commit
9596806644
34 changed files with 623 additions and 250 deletions
|
@ -3,7 +3,7 @@ Custom YAML data types
|
|||
======================
|
||||
|
||||
Sometimes you need to serialize complex data types such as classes. To do this
|
||||
you could use plain nodes such as mappings with class data members. YAML also
|
||||
you could use plain nodes such as mappings with classes' fields. YAML also
|
||||
supports custom types with identifiers called *tags*. That is the topic of this
|
||||
tutorial.
|
||||
|
||||
|
@ -23,11 +23,13 @@ functions to process each supported tag. These are supplied by 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 - this is used in equality comparisons of
|
||||
nodes. Default class *opEquals()* compares references, which means two identical
|
||||
objects might be considered unequal. (Default struct *opEquals()* compares
|
||||
byte-by-byte, sometimes you might want to override that as well.)
|
||||
Structs and classes must implement the *opCmp()* operator for YAML support. This
|
||||
is used for duplicate detection in mappings, sorting and equality comparisons of
|
||||
nodes. The signature of the operator that must be implemented is
|
||||
``const int opCmp(ref const MyStruct s)`` for structs where *MyStruct* is the
|
||||
struct type, and ``int opCmp(Object o)`` for classes. Note that the class
|
||||
*opCmp()* should not alter the compared values - it is not const for compatibility
|
||||
reasons.
|
||||
|
||||
We will implement support for an RGB color type. It is implemented as the
|
||||
following struct:
|
||||
|
@ -39,6 +41,14 @@ following struct:
|
|||
ubyte red;
|
||||
ubyte green;
|
||||
ubyte blue;
|
||||
|
||||
const int opCmp(ref const Color c)
|
||||
{
|
||||
if(red != c.red) {return red - c.red;}
|
||||
if(green != c.green){return green - c.green;}
|
||||
if(blue != c.blue) {return blue - c.blue;}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
First, we need a function to construct our data type. The function will take a
|
||||
|
|
|
@ -43,7 +43,7 @@ Do this by typing the following command into the console::
|
|||
|
||||
dmd cdc.d
|
||||
|
||||
Now you can use CDC to compile D:YAML.
|
||||
Now compile D:YAML with CDC.
|
||||
To do this on Unix/Linux, use the following command::
|
||||
|
||||
./cdc
|
||||
|
@ -101,8 +101,8 @@ into the file:
|
|||
Explanation of the code
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
First, we import the *yaml* module. This is the only module you need to import
|
||||
to use D:YAML - it automatically imports all needed modules.
|
||||
First, we import the *yaml* module. This is the only D:YAML module you need to
|
||||
import - it automatically imports all needed modules.
|
||||
|
||||
Next we load the file using the *Loader.load()* method. *Loader* is a struct
|
||||
used for parsing YAML documents. The *load()* method loads the file as
|
||||
|
@ -146,18 +146,18 @@ formatted differently. Comments are not preserved, either.
|
|||
Compiling
|
||||
^^^^^^^^^
|
||||
|
||||
To compile your project, you must give DMD the directories containing import
|
||||
modules and the library. You also need to tell it to link with D:YAML. The import
|
||||
directory should be the D:YAML package directory. You can specify it using the
|
||||
``-I`` option of DMD. The library directory should be where you put the compiled
|
||||
D:YAML library. On Unix/Linux you can specify it using the ``-L-L`` option, and
|
||||
link with D:YAML using the ``-L-l`` option. On Windows, the import directory is
|
||||
used as the library directory. To link with the library on Windows, just add the
|
||||
path to it relative to the current directory.
|
||||
To compile your project, DMD needs to know which directories contain the
|
||||
imported modules and the library. You also need to tell it to link with D:YAML.
|
||||
The import directory should be the D:YAML package directory. You can specify it
|
||||
using the ``-I`` option of DMD. The library directory should point to the
|
||||
compiled library. On Unix/Linux you can specify it using the ``-L-L`` option,
|
||||
and link with D:YAML using the ``-L-l`` option. On Windows, the import directory
|
||||
is used as the library directory. To link with the library on Windows, just add
|
||||
the path to it relative to the current directory.
|
||||
|
||||
For example, if you extracted and compiled D:YAML in ``/home/xxx/dyaml``, your
|
||||
project is in ``/home/xxx/dyaml-project``, and you are currently in that
|
||||
directory, you can compile the project with the following command on Unix/Linux::
|
||||
directory, compile the project with the following command on Unix/Linux::
|
||||
|
||||
dmd -I../dyaml -L-L../dyaml -L-ldyaml main.d
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue