Make AnyMap iterable
This commit is contained in:
parent
86c73dad20
commit
f0bb0d2262
3 changed files with 40 additions and 0 deletions
|
|
@ -229,6 +229,16 @@ public:
|
||||||
const std::string& getString(const std::string& key,
|
const std::string& getString(const std::string& key,
|
||||||
const std::string& default_) const;
|
const std::string& default_) const;
|
||||||
|
|
||||||
|
// Define begin() and end() to allow use with range-based for loops
|
||||||
|
using const_iterator = std::unordered_map<std::string, AnyValue>::const_iterator;
|
||||||
|
const_iterator begin() const {
|
||||||
|
return m_data.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator end() const {
|
||||||
|
return m_data.end();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <class T>
|
template <class T>
|
||||||
const T& get(const std::string& key, const T& default_,
|
const T& get(const std::string& key, const T& default_,
|
||||||
|
|
@ -238,6 +248,10 @@ private:
|
||||||
friend class AnyValue;
|
friend class AnyValue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Define begin() and end() to allow use with range-based for loops
|
||||||
|
AnyMap::const_iterator begin(const AnyValue& v);
|
||||||
|
AnyMap::const_iterator end(const AnyValue& v);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef CANTERA_API_NO_BOOST
|
#ifndef CANTERA_API_NO_BOOST
|
||||||
|
|
|
||||||
|
|
@ -606,4 +606,12 @@ AnyMap AnyMap::fromYamlFile(const std::string& name) {
|
||||||
return node.as<AnyMap>();
|
return node.as<AnyMap>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AnyMap::const_iterator begin(const AnyValue& v) {
|
||||||
|
return v.as<AnyMap>().begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
AnyMap::const_iterator end(const AnyValue& v) {
|
||||||
|
return v.as<AnyMap>().end();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -150,6 +150,24 @@ TEST(AnyMap, conversion_to_anyvalue)
|
||||||
EXPECT_EQ(n.at("strings").asVector<AnyValue>()[0].asString(), "foo");
|
EXPECT_EQ(n.at("strings").asVector<AnyValue>()[0].asString(), "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(AnyMap, iterators)
|
||||||
|
{
|
||||||
|
AnyMap m = AnyMap::fromYamlString(
|
||||||
|
"{a: 1, b: two, c: 3.01, d: {foo: 1, bar: 2}}");
|
||||||
|
std::vector<std::string> keys;
|
||||||
|
for (const auto& item : m) {
|
||||||
|
keys.push_back(item.first);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(keys.size(), (size_t) 4);
|
||||||
|
EXPECT_TRUE(std::find(keys.begin(), keys.end(), "c") != keys.end());
|
||||||
|
keys.clear();
|
||||||
|
|
||||||
|
for (const auto& item : m.at("d")) {
|
||||||
|
keys.push_back(item.first);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(keys.size(), (size_t) 2);
|
||||||
|
EXPECT_TRUE(std::find(keys.begin(), keys.end(), "bar") != keys.end());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(AnyMap, loadYaml)
|
TEST(AnyMap, loadYaml)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue