Writing x::yaml::newnodes

Each object that forms a YAML is a x::yaml::newnode, which is a reference to a reference-counted object that represents a sequence, a mapping, a scalar value, or an alias reference.

#include <x/yaml/writer.H>
#include <x/yaml/newdocumentnode.H>

std::vector<x::yaml::newdocumentnode> documents;

x::yaml::newnode list_of_highest_points_to_yaml();

// ...

documents.push_back(x::yaml::make_newdocumentnode
    ([]
    {
        return list_of_highest_points_to_yaml();
    }));

    x::yaml::writer
        ::write(documents, std::ostreambuf_iterator<char>(std::cout));

Several overloaded writer::write() functions take either a container or an input sequence of x::yaml::newdocumentnodes, defined by a beginning or an ending iterator, and an output iterator over chars. There's also a write_one_document() that takes a single x::yaml::newdocumentnode in place of a container or an input sequence.

These functions invoke each x::yaml::newdocumentnode's lambda/functor to get the document's top level sequence/mapping node, and write each YAML document, starting with the top level node, and invoking a functor associated with each node object. All the lambdas and functors get invoked in order, and the reference-counted objects they return go out of scope, and get destroyed, after their YAML structure gets written out, and they are no longer needed. This approach avoids the need to create the structure of the entire YAML document in memory, before writing it out.

The YAML document or documents get written to the output iterator, and the write() functions return the value of the output iterator after writing the documents, if it's needed. They also take an optional parameter that sets non-default output options:

#include <x/yaml/writer.H>
#include <x/yaml/newdocumentnode.H>

auto doc=x::yaml::make_newdocumentnode
    ([]
    {
        return list_of_highest_points_to_yaml();
    });

    x::yaml::writer
        ::write_one_document(x::yaml::writer::options().setindent(2), doc,
                             std::ostreambuf_iterator<char>(std::cout));

This example writes one document to std::cout using an indentation level of two spaces.