Program Listing for File YAMLUtil.hpp

Return to documentation for file (include/lvr2/util/YAMLUtil.hpp)

#ifndef LVR2_IO_YAML_UTIL_HPP
#define LVR2_IO_YAML_UTIL_HPP

#include <string>
#include <yaml-cpp/yaml.h>
#include <iostream>

#include "lvr2/util/Timestamp.hpp"

namespace YAML {

inline const Node & cnode(const Node &n) {
    return n;
}
/*
Node MergeNodes(Node a, Node b)
{
    if (!b.IsMap()) {
        // If b is not a map, merge result is b, unless b is null
        return b.IsNull() ? a : b;
    }
    if (!a.IsMap()) {
        // If a is not a map, merge result is b
        return b;
    }
    if (!b.size()) {
        // If a is a map, and b is an empty map, return a
        return a;
    }
    // Create a new map 'c' with the same mappings as a, merged with b
    auto c = Node(NodeType::Map);
    for (auto n : a) {
        if (n.first.IsScalar()) {
        const std::string & key = n.first.Scalar();
        auto t = Node(cnode(b)[key]);
        if (t) {
            c[n.first] = MergeNodes(n.second, t);
            continue;
        }
        }
        c[n.first] = n.second;
    }
    // Add the mappings from 'b' not already in 'c'
    for (auto n : b) {
        if (!n.first.IsScalar() || !cnode(c)[n.first.Scalar()]) {
        c[n.first] = n.second;
        }
    }
    return c;
}
*/


} // namespace YAML

namespace YAML_UTIL
{
inline bool ValidateNodeTag(const YAML::Node& node, const char* decoder_name, const char* tag_name, const char* required_value)
{
    if(!node[tag_name])
    {
        std::cout << lvr2::timestamp << "[YAML::convert<" << decoder_name << "> - decode] "
                    << "Node has no '" << tag_name << "' Tag" << std::endl;
        return false;
    }
    if (node[tag_name].as<std::string>() != required_value)
    {
        // different hierarchy level
        std::cout << lvr2::timestamp << "[YAML::convert<" << decoder_name << "> - decode] "
                    << "Nodes " << tag_name << " '" << node[tag_name].as<std::string>()
                    << "' is not '" <<  required_value << "'" << std::endl;
        return false;
    }

    return true;
}

inline bool ValidateEntityAndType(const YAML::Node& node, const char* converter_name, const char* entity, const char* type)
{
    // If either one of these Tags is missing or does not match the Value return false
    if (!ValidateNodeTag(node, converter_name, "entity", entity) ||
        !ValidateNodeTag(node, converter_name, "type", type))
    {
        return false;
    }

    return true;
}


inline bool ValidateNodeTagSilent(const YAML::Node& node, const char* decoder_name, const char* tag_name, const char* required_value)
{
    if(!node[tag_name])
    {
        return false;
    }
    if (node[tag_name].as<std::string>() != required_value)
    {
        return false;
    }

    return true;
}

inline bool ValidateEntityAndTypeSilent(const YAML::Node& node, const char* converter_name, const char* entity, const char* type)
{
    // If either one of these Tags is missing or does not match the Value return false
    if (!ValidateNodeTagSilent(node, converter_name, "entity", entity) ||
        !ValidateNodeTagSilent(node, converter_name, "type", type))
    {
        return false;
    }

    return true;
}



} // namespace YAML_UTIL

#endif // LVR2_IO_YAML_UTIL_HPP