5#ifndef GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
6#define GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
12#include <yaml-cpp/yaml.h>
14#include <ginkgo/core/config/property_tree.hpp>
26inline gko::config::pnode parse_yaml(
const YAML::Node& input)
28 auto parse_array = [](
const auto& arr) {
29 gko::config::pnode::array_type nodes;
30 for (
const auto& it : arr) {
31 nodes.emplace_back(parse_yaml(it));
33 return gko::config::pnode{nodes};
35 auto parse_map = [](
const auto& map) {
36 gko::config::pnode::map_type nodes;
38 for (
const auto& yaml_item : map) {
43 auto node = parse_yaml(yaml_item.second);
44 if (node.get_tag() == gko::config::pnode::tag_t::array) {
45 for (
const auto& arr : node.get_array()) {
46 if (arr.get_tag() != gko::config::pnode::tag_t::map) {
47 throw std::runtime_error(
48 "YAML only accepts merge key << to merge item "
50 YAML::Dump(yaml_item.second));
52 for (
const auto& item : arr.get_map()) {
53 nodes[item.first] = item.second;
56 }
else if (node.get_tag() == gko::config::pnode::tag_t::map) {
57 for (
const auto& item : node.get_map()) {
58 nodes[item.first] = item.second;
61 throw std::runtime_error(
"can not handle this alias: " +
62 YAML::Dump(yaml_item.second));
65 nodes[key] = parse_yaml(yaml_item.second);
68 return gko::config::pnode{nodes};
71 auto parse_data = [](
const auto& data) {
72 if (std::int64_t value;
73 YAML::convert<std::int64_t>::decode(data, value)) {
74 return gko::config::pnode{value};
76 if (
bool value; YAML::convert<bool>::decode(data, value)) {
77 return gko::config::pnode{value};
79 if (
double value; YAML::convert<double>::decode(data, value)) {
80 return gko::config::pnode{value};
82 if (std::string value;
83 YAML::convert<std::string>::decode(data, value)) {
84 return gko::config::pnode{value};
86 std::string content = YAML::Dump(data);
87 throw std::runtime_error(
88 "property_tree can not handle the node with content: " + content);
91 if (input.IsSequence()) {
92 return parse_array(input);
95 return parse_map(input);
97 return parse_data(input);
125inline gko::config::pnode parse_yaml_file(std::string filename)
127 return parse_yaml(YAML::LoadFile(filename));
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:307