11 std::map<std::string, std::string> AnyValue::s_typenames = {
12 {
typeid(double).name(),
"double"},
13 {
typeid(std::string).name(),
"string"},
14 {
typeid(std::vector<double>).name(),
"vector<double>"},
15 {
typeid(
AnyMap).name(),
"AnyMap"},
22 , m_value(
new boost::any{})
25 AnyValue::~AnyValue() =
default;
27 AnyValue::AnyValue(
AnyValue const& other): m_key(other.m_key),
28 m_value(
new boost::any{*other.m_value}) {
31 AnyValue::AnyValue(
AnyValue&& other): m_key(std::move(other.m_key)),
32 m_value(std::move(other.m_value)) {
39 m_value.reset(
new boost::any{*other.m_value});
46 m_key = std::move(other.m_key);
47 m_value = std::move(other.m_value);
51 AnyValue& AnyValue::operator[](
const std::string& key)
53 return as<AnyMap>()[key];
56 bool AnyValue::hasKey(
const std::string& key)
const {
57 return (is<AnyMap>() && as<AnyMap>().hasKey(key));
60 void AnyValue::setKey(
const std::string &key) { m_key = key; }
62 const std::type_info &AnyValue::type() {
63 return m_value->type();
66 AnyValue &AnyValue::operator=(
const std::string &value) {
71 AnyValue &AnyValue::operator=(
const char *value) {
72 *m_value = std::string(value);
76 const std::string &AnyValue::asString()
const {
77 return as<std::string>();
80 AnyValue &AnyValue::operator=(
double value) {
85 double AnyValue::asDouble()
const {
89 AnyValue &AnyValue::operator=(
bool value) {
94 bool AnyValue::asBool()
const {
98 AnyValue &AnyValue::operator=(
long int value) {
103 AnyValue &AnyValue::operator=(
int value) {
104 *m_value =
static_cast<long int>(value);
108 long int AnyValue::asInt()
const {
109 return as<long int>();
117 *m_value = std::move(value);
121 std::string AnyValue::demangle(
const std::type_info& type)
const 123 if (s_typenames.find(type.name()) != s_typenames.end()) {
124 return s_typenames[type.name()];
132 AnyValue& AnyMap::operator[](
const std::string& key)
134 const auto& slash = boost::ifind_first(key,
"/");
137 const auto& iter = m_data.find(key);
138 if (iter == m_data.end()) {
151 std::string head(key.begin(), slash.begin());
152 std::string tail(slash.end(), key.end());
153 const auto& iter = m_data.find(head);
154 if (iter == m_data.end()) {
159 return value.as<
AnyMap>()[tail];
162 return iter->second.as<
AnyMap>()[tail];
167 AnyValue& AnyMap::at(
const std::string& key)
169 const auto& slash = boost::ifind_first(key,
"/");
171 return m_data.at(key);
173 std::string head(key.begin(), slash.begin());
174 std::string tail(slash.end(), key.end());
175 return m_data.at(head).as<
AnyMap>().at(tail);
179 bool AnyMap::hasKey(
const std::string& key)
const 181 const auto& slash = boost::ifind_first(key,
"/");
183 return (m_data.find(key) != m_data.end());
185 std::string head(key.begin(), slash.begin());
186 std::string tail(slash.end(), key.end());
187 if (m_data.find(head) == m_data.end() || !m_data.at(head).is<
AnyMap>()) {
190 return m_data.at(head).as<
AnyMap>().hasKey(tail);
A wrapper for a variable whose type is determined at runtime.
A map of string keys to values whose type can vary at runtime.
Namespace for the Cantera kernel.