I'm looking for a data structure which behaves similar to boost::property_tree
but (optionally) leaves the get/set implementation for each value item to the developer.
You should be able to do something like this:
std::function<int(void)> f_foo = ...;
my_property_tree tree;
tree.register<int>("some.path.to.key", f_foo);
auto v1 = tree.get<int>("some.path.to.key"); // <-- calls f_foo
auto v2 = tree.get<int>("some.other.path"); // <-- some fallback or throws exception
I guess you could abuse property_tree
for this but I haven't looked into the implementation yet and I would have a bad feeling about this unless I knew that this is an intended use case.
Writing a class that handles requests like val = tree.get("some.path.to.key")
by calling a provided function doesn't look too hard in the first place but I can imagine a lot of special cases which would make this quite a bulky library.
Some extra features might be:
subtree-handling: not only handle terminal keys but forward certain subtrees to separate implementations. E.g.
tree.register("some.path.config", some_handler); // calls some_handler.get<int>("network.hostname") v = tree.get<int>("some.path.config.network.hostname");
search among values / keys
- automatic type casting (like in
boost::property_tree
) - "path overloading", e.g. defaulting to a property_tree-implementation for paths without registered callback.
Is there a library that comes close to what I'm looking for? Has anyone made experiences with using boost::property_tree
for this purpose? (E.g. by subclassing or putting special objects into the tree like described here)
std::map
can only map terminal items. E.g. in casemyTree.get('path.to.item')
is valid and returns a valuemyTree.get('path.to')
implicitly should be valid, too and return a subtree. A possible approach would be some sort of map of maps with everysecond
item containing an optional value and a list of siblings. But this would be what I don't want to reinvent. – Kristiankristiansand