mirror of
https://github.com/Cantera/cantera.git
synced 2025-02-25 18:55:29 -06:00
Add AnyMap.keys_str function
This commit is contained in:
@@ -237,6 +237,10 @@ public:
|
||||
|
||||
bool hasKey(const std::string& key) const;
|
||||
|
||||
//! Return a string listing the keys in this AnyMap, e.g. for use in error
|
||||
//! messages
|
||||
std::string keys_str() const;
|
||||
|
||||
bool getBool(const std::string& key, bool default_) const;
|
||||
long int getInt(const std::string& key, long int default_) const;
|
||||
double getDouble(const std::string& key, double default_) const;
|
||||
|
||||
@@ -565,7 +565,8 @@ const AnyValue& AnyMap::at(const std::string& key) const
|
||||
try {
|
||||
return m_data.at(key);
|
||||
} catch (std::out_of_range& err) {
|
||||
throw CanteraError("AnyMap::at", "Key '{}' not found", key);
|
||||
throw CanteraError("AnyMap::at",
|
||||
"Key '{}' not found.\nExisting keys: {}", key, keys_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,6 +575,21 @@ bool AnyMap::hasKey(const std::string& key) const
|
||||
return (m_data.find(key) != m_data.end());
|
||||
}
|
||||
|
||||
std::string AnyMap::keys_str() const
|
||||
{
|
||||
fmt::memory_buffer b;
|
||||
auto iter = this->begin();
|
||||
if (iter != this->end()) {
|
||||
format_to(b, "{}", iter->first);
|
||||
++iter;
|
||||
}
|
||||
while (iter != this->end()) {
|
||||
format_to(b, ", {}", iter->first);
|
||||
++iter;
|
||||
}
|
||||
return to_string(b);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
const T& AnyMap::get(const std::string& key, const T& default_,
|
||||
std::function<const T&(const AnyValue*)> getter) const
|
||||
|
||||
@@ -48,11 +48,17 @@ TEST(AnyMap, map_conversion) {
|
||||
AnyMap m;
|
||||
m["compound"]["first"] = "bar";
|
||||
m["compound"]["second"] = "baz";
|
||||
m["empty"] = AnyMap();
|
||||
|
||||
auto x = m["compound"].asMap<std::string>();
|
||||
EXPECT_EQ(x.size(), (size_t) 2);
|
||||
EXPECT_EQ(x["first"], "bar");
|
||||
EXPECT_EQ(x["second"], "baz");
|
||||
std::string keys = m["compound"].as<AnyMap>().keys_str();
|
||||
EXPECT_NE(keys.find("first"), npos);
|
||||
EXPECT_NE(keys.find("second"), npos);
|
||||
EXPECT_EQ(keys.size(), (size_t) 13);
|
||||
EXPECT_EQ(m["empty"].as<AnyMap>().keys_str(), "");
|
||||
|
||||
std::map<std::string, double> zz{{"a", 9.0}, {"b", 13.5}};
|
||||
m["foo"] = zz;
|
||||
|
||||
Reference in New Issue
Block a user