Skip to content

Instantly share code, notes, and snippets.

@alepez
Created January 29, 2016 16:40
Show Gist options
  • Save alepez/7294ee5190ab689f0ea6 to your computer and use it in GitHub Desktop.
Save alepez/7294ee5190ab689f0ea6 to your computer and use it in GitHub Desktop.
recursive map of string
class Cfg {
public:
Cfg(std::initializer_list<std::pair<std::string, Cfg>> init) {
for (auto&& it: init) {
children_.insert(it);
}
}
Cfg(std::initializer_list<std::pair<std::string, std::string>> init) {
for (auto&& it: init) {
children_.insert(it);
}
}
Cfg(std::string data) : data_{data} {
}
const Cfg& operator[](const std::string& key) const {
return children_.at(key);
}
operator std::string() const {
return data_;
}
private:
std::string data_;
std::map<std::string, Cfg> children_;
};
TEST(DraftTest, Draft1) {
Cfg cfg{"one"};
ASSERT_EQ("one", static_cast<std::string>(cfg));
}
TEST(DraftTest, Draft2) {
Cfg cfg = {{"foo", "one"}, {"bar", "two"}};
ASSERT_EQ("one", static_cast<std::string>(cfg["foo"]));
}
TEST(DraftTest, Draft3) {
Cfg cfg = {
{"foo", {
{"bar", "three"}
}},
};
ASSERT_EQ("three", static_cast<std::string>(cfg["foo"]["bar"]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment