Xped
Loading...
Searching...
No Matches
TomlHelpers.hpp
Go to the documentation of this file.
1#ifndef XPED_TOML_HELPERS_HPP_
2#define XPED_TOML_HELPERS_HPP_
3
4#include <any>
5#include <exception>
6#include <map>
7#include <string>
8
9#include "toml.hpp"
10
11#include "Xped/PEPS/Bonds.hpp"
12#include "Xped/Util/Param.hpp"
13
14namespace Xped::util {
15
16template <typename T, typename = typename std::enable_if_t<boost::describe::has_describe_enumerators<T>::value>>
17T enum_from_toml(const toml::value& t)
18{
19 T out;
20 bool success = boost::describe::enum_from_string(std::string(t.as_string()).c_str(), out);
21 if(not success) { throw std::invalid_argument("Bad conversion from toml input to enum."); }
22 return out;
23}
24
25std::map<std::string, Param> params_from_toml(const toml::value& t)
26{
27 std::map<std::string, Param> params;
28 for(const auto& [k, v] : t.as_table()) {
29 if(v.is_floating()) {
30 params[k] = Param{.value = static_cast<double>(v.as_floating())};
31 } else if(v.is_array()) {
32 if(v.at(0).is_floating()) {
33 params[k] = Param{.value = toml::get<std::vector<double>>(v)};
34 } else if(v.at(0).is_array()) {
35 params[k] = Param{.value = toml::get<std::vector<std::vector<double>>>(v)};
36 }
37 } else {
38 throw std::invalid_argument("Bad model parameters.");
39 }
40 }
41 return params;
42}
43} // namespace Xped::util
44
45#endif
Definition: FusionTree.hpp:15
T enum_from_toml(const toml::value &t)
Definition: TomlHelpers.hpp:17
std::map< std::string, Param > params_from_toml(const toml::value &t)
Definition: TomlHelpers.hpp:25
Definition: Param.hpp:9
std::any value
Definition: Param.hpp:10