Many libraries provide us getter and setters to access and modify there processed variables. One of the libraries is Boost PropertyTree
. I want to wrap getter and setter in boost::property_tree::ptree
with operator[]
.
Source:
class XMLDocument {
boost::property_tree::ptree ptree_;
public:
XMLDocument(const std::string &content) {
std::stringstream ss;
boost::property_tree::read_xml(ss << content, ptree_);
}
template <typename T> T &operator[](const std::string &path) { /* TODO */ }
}
int main() {
XMLDocument xml("<foo>bar</foo><baz>qux</baz>");
std::cout << xml["foo"] << std::endl; // Access the element.
xml["baz"] = "quux"; // Modify the element.
std::cout << xml["baz"] << std::endl;
}
Output:
bar
quux
How can I do?