class mrpt::containers::yaml_ref

Overview

Non-owning mutable reference into a yaml tree node.

Returned by yaml::operator[]. Implicitly converts to yaml (deep copy) for reading; assignments write through to the referenced node.

Invalidated if the parent yaml container reallocates (e.g. a new map key is inserted). Treat it as a temporary handle, not a persistent reference.

[New in MRPT 3.x]

#include <mrpt/containers/yaml.h>

class yaml_ref
{
public:
    // typedefs

    typedef yaml::node_t node_t;
    typedef yaml::scalar_t scalar_t;
    typedef yaml::sequence_t sequence_t;
    typedef yaml::map_t map_t;

    // construction

    yaml_ref();
    yaml_ref(const yaml_ref&);
    yaml_ref(node_t& n);

    // methods

    operator yaml () const;
    node_t& node();
    const node_t& node() const;
    bool isNullNode() const;
    bool isScalar() const;
    bool isMap() const;
    bool isSequence() const;
    std::string typeName() const;

    template <typename T>
    T as() const;

    size_t size() const;
    bool empty() const;
    bool has(const std::string& key) const;
    void clear();
    sequence_t& asSequence();
    const sequence_t& asSequence() const;
    map_t& asMap();
    const map_t& asMap() const;
    map_t asMapRange() const;
    scalar_t& asScalar();
    const scalar_t& asScalar() const;
    bool keyHasComment(const std::string& key) const;
    std::string keyComment(const std::string& key) const;
    const node_t& keyNode(const std::string& key) const;
    node_t& keyNode(const std::string& key);
    size_t erase(const std::string& key);
    bool erase(int index);
    bool hasComment() const;
    bool hasComment(CommentPosition pos) const;
    const std::string& comment() const;
    const std::string& comment(CommentPosition pos) const;

    void comment(
        const std::string& c,
        CommentPosition pos = CommentPosition::RIGHT
        );

    void printAsYAML(
        std::ostream& o,
        const YamlEmitOptions& eo = {}
        ) const;

    void printAsYAML() const;
    yaml_ref operator [] (const std::string& key);
    yaml_ref operator [] (const char* key);
    yaml_cref operator [] (const std::string& key) const;
    yaml_cref operator [] (const char* key) const;
    yaml_ref operator [] (int index);
    yaml_cref operator [] (int index) const;
    yaml_ref operator () (int index);
    yaml_cref operator () (int index) const;
    const std::type_info& scalarType() const;

    template <typename T>
    T getOrDefault(
        const std::string& key,
        const T& def
        ) const;

    template <typename T>
    T& asRef();

    yaml_ref& operator = (bool v);
    yaml_ref& operator = (float v);
    yaml_ref& operator = (double v);
    yaml_ref& operator = (int8_t v);
    yaml_ref& operator = (uint8_t v);
    yaml_ref& operator = (int16_t v);
    yaml_ref& operator = (uint16_t v);
    yaml_ref& operator = (int32_t v);
    yaml_ref& operator = (uint32_t v);
    yaml_ref& operator = (int64_t v);
    yaml_ref& operator = (uint64_t v);
    yaml_ref& operator = (const std::string& v);
    yaml_ref& operator = (const char* v);
    yaml_ref& operator = (const std::string_view& v);
    yaml_ref& operator = (const yaml& v);
    yaml_ref& operator = (const yaml_ref& v);

    template <typename = std::enable_if<!std::is_same_v<std::size_t, uint64_t>&& !std::is_same_v<std::size_t, int64_t>&&          !std::is_same_v<std::size_t, uint32_t>&& !std::is_same_v<std::size_t, int32_t>>>
    yaml_ref& operator = (std::size_t v);

    template <typename T>
    yaml_ref& operator = (const ValueCommentPair<T>& vc);

    operator bool () const;
    operator double () const;
    operator float () const;
    operator int8_t () const;
    operator uint8_t () const;
    operator int16_t () const;
    operator uint16_t () const;
    operator int32_t () const;
    operator uint32_t () const;
    operator int64_t () const;
    operator uint64_t () const;
    operator std::string () const;
    void push_back(double v);
    void push_back(const std::string& v);
    void push_back(uint64_t v);
    void push_back(bool v);
    void push_back(const yaml& v);
};

Methods

template <typename T>
yaml_ref& operator = (const ValueCommentPair<T>& vc)

vcp (value-comment) wrapper