3 #ifndef CT_ANYMAP_INL_H 4 #define CT_ANYMAP_INL_H 8 #include <boost/any.hpp> 9 #include <boost/algorithm/string.hpp> 17 const T &AnyValue::as()
const {
19 return boost::any_cast<
const T&>(*m_value);
20 }
catch (boost::bad_any_cast&) {
21 if (m_value->type() ==
typeid(void)) {
23 throw CanteraError(
"AnyValue::as",
"Key '{}' not found", m_key);
25 throw CanteraError(
"AnyValue::as",
26 "Key '{}' contains a '{}',\nnot a '{}'.",
27 m_key, demangle(m_value->type()), demangle(
typeid(T)));
35 return boost::any_cast<T&>(*m_value);
36 }
catch (boost::bad_any_cast&) {
37 if (m_value->type() ==
typeid(void)) {
39 throw CanteraError(
"AnyValue::as",
"Key '{}' not found", m_key);
41 throw CanteraError(
"AnyValue::as",
42 "Key '{}' contains a '{}',\nnot a '{}'.",
43 m_key, demangle(m_value->type()), demangle(
typeid(T)));
49 bool AnyValue::is()
const {
50 return m_value->type() ==
typeid(T);
54 AnyValue &AnyValue::operator=(
const std::vector<T> &value) {
60 const std::vector<T> &AnyValue::asVector()
const {
61 return as<std::vector<T>>();
65 std::vector<T> &AnyValue::asVector() {
66 return as<std::vector<T>>();
70 AnyValue& AnyValue::operator=(
const std::unordered_map<std::string, T> items) {
72 AnyMap& dest = as<AnyMap>();
73 for (
const auto& item : items) {
74 dest[item.first] = item.second;
80 AnyValue& AnyValue::operator=(
const std::map<std::string, T> items) {
82 AnyMap& dest = as<AnyMap>();
83 for (
const auto& item : items) {
84 dest[item.first] = item.second;
90 inline AnyMap& AnyValue::as<AnyMap>() {
94 if (m_value->type() ==
typeid(void)) {
97 return boost::any_cast<AnyMap&>(*m_value);
98 }
catch (boost::bad_any_cast&) {
99 throw CanteraError(
"AnyValue::as",
100 "value of key '{}' is a '{}',\nnot an 'AnyMap'.",
101 m_key, demangle(m_value->type()));
106 std::map<std::string, T> AnyValue::asMap()
108 std::map<std::string, T> dest;
109 for (
const auto& item : as<AnyMap>().m_data) {
111 dest[item.first] = boost::any_cast<T>(*item.second.m_value);
112 }
catch (boost::bad_any_cast&) {
113 throw CanteraError(
"AnyValue::asMap",
114 "Value of key '{}' is not a '{}'",
115 item.first, demangle(
typeid(T)));
Namespace for the Cantera kernel.