ezconfig
Create C++ Objects from Yaml and Json
Loading...
Searching...
No Matches
hana.hpp
1// Copyright (c) 2023 Petter Nilsson. MIT License. https://github.com/pettni/ezconfig
2
3#pragma once
4
5#include <boost/hana/at_key.hpp>
6#include <boost/hana/for_each.hpp>
7#include <boost/hana/keys.hpp>
8#include <boost/hana/map.hpp>
9#include <yaml-cpp/yaml.h>
10
11#include "hana_fwd.hpp"
12
13namespace YAML {
14
15template<typename T>
16 requires(boost::hana::Struct<T>::value)
17bool convert<T>::decode(const Node & yaml, T & t)
18{
19 boost::hana::for_each(boost::hana::keys(t), [&](auto key) {
20 using ValT = std::decay_t<decltype(boost::hana::at_key(t, key))>;
21 char const * key_c = boost::hana::to<char const *>(key);
22 boost::hana::at_key(t, key) = yaml[key_c].template as<ValT>();
23 });
24
25 return true;
26}
27
28template<typename... Ts>
29bool convert<std::variant<Ts...>>::decode(const Node & yaml, std::variant<Ts...> & obj)
30{
31 const auto & hana_map = ::ezconfig::variant_hana_maps<std::variant<Ts...>>::value;
32
33 bool ret = false;
34 boost::hana::for_each(hana_map, [&](const auto & entry) {
35 if (boost::hana::first(entry) == yaml.Tag()) {
36 using type = std::decay_t<decltype(boost::hana::second(entry))>::type;
37 obj = yaml.template as<type>();
38 ret = true;
39 }
40 });
41
42 return ret;
43}
44
45} // namespace YAML
YAML forward declarations.
Definition yaml_fwd.hpp:16
Type trait for std::variant<> yaml decoding.
Definition hana_fwd.hpp:36