OGLplus  (0.59.0) a C++ wrapper for rendering APIs

eagine/value_tree.cpp

Copyright Matus Chochlik. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt

#include <eagine/main.hpp>
#include <iostream>
#include <memory>
namespace eagine {
auto main(main_ctx& ctx) -> int {
auto& log = ctx.log();
const string_view n_a{"N/A"};
auto path = [](string_view str) {
return basic_string_path(str, EAGINE_TAG(split_by), "/");
};
auto visitor = [&ctx](
valtree::compound& c,
const valtree::attribute& a,
const basic_string_path& p) {
auto ca{c / a};
ctx.log()
.info("visit")
.arg(EAGINE_ID(nested), ca.nested_count())
.arg(EAGINE_ID(values), ca.value_count())
.arg(EAGINE_ID(isLink), EAGINE_ID(bool), ca.is_link())
.arg(EAGINE_ID(canonType), ca.canonical_type())
.arg(EAGINE_ID(path), p.as_string("/", ca.nested_count() > 0))
.arg(EAGINE_ID(name), ca.name());
if(ca.canonical_type() == valtree::value_type::byte_type) {
const auto s{ca.value_count()};
if(s <= 256) {
std::array<byte, 256> temp{};
auto content{ca.fetch_blob(cover(temp))};
ctx.log().info("content").arg(
EAGINE_ID(content), view(content));
}
} else if(ca.canonical_type() == valtree::value_type::string_type) {
if(ca.value_count() == 1) {
std::array<char, 64> temp{};
auto content{ca.fetch_values(cover(temp))};
ctx.log().info("content").arg(
EAGINE_ID(content), string_view(content));
}
}
return true;
};
const string_view json_text(R"({
"attribA" : {
"attribB": 123
},
"attribC" : [
45, "six", 78.9, {"zero": false}
],
"attribD" : "VGhpcyBpcyBhIGJhc2U2NC1lbmNvZGVkIEJMT0IK"
})");
if(auto json_tree{valtree::from_json_text(json_text, ctx)}) {
std::array<byte, 64> temp{};
log.info("parsed from json")
.arg(
EAGINE_ID(attribB),
EAGINE_ID(int),
json_tree.get<int>(path("attribA/attribB")),
n_a)
.arg(
EAGINE_ID(attribC0),
EAGINE_ID(int),
json_tree.get<int>(path("attribC/0")),
n_a)
.arg(
EAGINE_ID(attribC1),
EAGINE_ID(string),
json_tree.get<std::string>(path("attribC/1")),
n_a)
.arg(
EAGINE_ID(attribC2),
EAGINE_ID(float),
json_tree.get<float>(path("attribC/2")),
n_a)
.arg(
EAGINE_ID(attribC3z),
EAGINE_ID(bool),
json_tree.get<bool>(path("attribC/3/zero")),
n_a)
.arg(
EAGINE_ID(attribD),
view(json_tree.fetch_blob(path("attribD"), cover(temp))));
json_tree.traverse(
}
const string_view yaml_text(
"attribA:\n"
" attribB: 123\n"
"attribC: [45, six, 78.9, zero: false]\n");
if(auto yaml_tree{valtree::from_yaml_text(yaml_text, ctx)}) {
log.info("parsed from yaml")
.arg(
EAGINE_ID(attribB),
EAGINE_ID(int),
yaml_tree.get<int>(path("attribA/attribB")),
n_a)
.arg(
EAGINE_ID(attribC0),
EAGINE_ID(int),
yaml_tree.get<int>(path("attribC/0")),
n_a)
.arg(
EAGINE_ID(attribC1),
EAGINE_ID(string),
yaml_tree.get<std::string>(path("attribC/1")),
n_a)
.arg(
EAGINE_ID(attribC2),
EAGINE_ID(float),
yaml_tree.get<float>(path("attribC/2")),
n_a)
.arg(
EAGINE_ID(attribC3z),
EAGINE_ID(bool),
yaml_tree.get<bool>(path("attribC/3/zero")),
n_a);
yaml_tree.traverse(
}
if(auto path_arg{ctx.args().find("--fs-tree").next()}) {
log.info("opening ${root} filesystem tree")
.arg(EAGINE_ID(root), path_arg.get());
if(auto fs_tree{valtree::from_filesystem_path(path_arg, ctx)}) {
fs_tree.traverse(
}
}
return 0;
}
} // namespace eagine
@ string_type
string value type.
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
Common code is placed in this namespace.
Definition: eagine.hpp:21
@ byte_type
Byte/BLOB value type.
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
auto from_filesystem_path(string_view root_path, main_ctx_parent, std::shared_ptr< file_compound_factory >={}) -> compound
Creates a compound representing a filesystem subtree.
#define EAGINE_TAG(NAME)
Macro for defining selector values corresponding to identifier NAME.
Definition: identifier.hpp:375
auto from_json_text(string_view, main_ctx_parent) -> compound
Creates a compound from a JSON text string view.
constexpr const construct_from_t construct_from
The construct-from tag-dispatch constant.
Definition: selector.hpp:47
auto from_yaml_text(string_view, main_ctx_parent) -> compound
Creates a compound from a YAML text string view.
callable_ref< bool(compound &, const attribute &, const basic_string_path &)> visit_handler
Type of traverse/visit handler.
Definition: wrappers.hpp:709

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).