Add variants of AnyValue.asMap for accessing lists of maps
This commit is contained in:
parent
71bf44d11f
commit
e28c9add21
2 changed files with 39 additions and 0 deletions
|
|
@ -103,6 +103,18 @@ public:
|
|||
template<class T>
|
||||
std::map<std::string, T> asMap() const;
|
||||
|
||||
//! Access a vector<AnyMap> as a mapping using the value of `name` from each
|
||||
//! item as the key in the new mapping.
|
||||
/*!
|
||||
* For example, for the list:
|
||||
* ```
|
||||
* [{name: O2, weight: 32}, {name: CH4, weight: 16}]
|
||||
* ```
|
||||
* calling `asMap("name")` will create a map with keys ``O2`` and ``CH4``.
|
||||
*/
|
||||
std::unordered_map<std::string, const AnyMap*> asMap(const std::string& name) const;
|
||||
std::unordered_map<std::string, AnyMap*> asMap(const std::string& name);
|
||||
|
||||
//! @see AnyMap::applyUnits
|
||||
void applyUnits(const UnitSystem& units);
|
||||
|
||||
|
|
|
|||
|
|
@ -361,6 +361,33 @@ AnyValue& AnyValue::operator=(AnyMap&& value) {
|
|||
return *this;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, const AnyMap*> AnyValue::asMap(
|
||||
const std::string& name) const
|
||||
{
|
||||
std::unordered_map<std::string, const AnyMap*> mapped;
|
||||
for (const auto& item : asVector<AnyMap>()) {
|
||||
auto key = item.at(name).asString();
|
||||
if (mapped.count(key)) {
|
||||
throw CanteraError("AnyValue::asMap", "Duplicate key '{}'", key);
|
||||
}
|
||||
mapped.emplace(std::make_pair(key, &item));
|
||||
}
|
||||
return mapped;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, AnyMap*> AnyValue::asMap(const std::string& name)
|
||||
{
|
||||
std::unordered_map<std::string, AnyMap*> mapped;
|
||||
for (auto& item : asVector<AnyMap>()) {
|
||||
auto key = item.at(name).asString();
|
||||
if (mapped.count(key)) {
|
||||
throw CanteraError("AnyValue::asMap", "Duplicate key '{}'", key);
|
||||
}
|
||||
mapped.emplace(std::make_pair(key, &item));
|
||||
}
|
||||
return mapped;
|
||||
}
|
||||
|
||||
void AnyValue::applyUnits(const UnitSystem& units)
|
||||
{
|
||||
if (is<AnyMap>()) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue