Constructor.addConstructorMapping

Add a constructor function from a mapping.

class Constructor
void
addConstructorMapping
@safe nothrow
(
T
)
(
const string tag
,
T function(
ref Node
)
ctor
)

Examples

1 import std.string;
2 
3 import dyaml.all;
4 
5 struct MyStruct
6 {
7    int x, y, z;
8 
9    //Any D:YAML type must have a custom opCmp operator.
10    //This is used for ordering in mappings.
11    const int opCmp(ref const MyStruct s)
12    {
13        if(x != s.x){return x - s.x;}
14        if(y != s.y){return y - s.y;}
15        if(z != s.z){return z - s.z;}
16        return 0;
17    }
18 }
19 
20 MyStruct constructMyStructMapping(ref Node node)
21 {
22    //node is guaranteed to be mapping.
23    //!mystruct {"x": x, "y": y, "z": z}
24    return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
25 }
26 
27 void main()
28 {
29    auto loader = Loader("file.yaml");
30    auto constructor = new Constructor;
31    constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
32    loader.constructor = constructor;
33    Node node = loader.load();
34 }

See Also

addConstructorScalar

Meta