framework/library for property-tree-like data structure with generic get/set-implementation?
Asked Answered
K

4

19

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)

Kristiankristiansand answered 25/5, 2015 at 14:51 Comment(0)
W
1

After years of coding my own container classes I ended up just adopting QVariantMap. This way it pretty much behaves (and is as flexible as) python. Just one interface. Not for performance code though.

If you care to know, I really caved in for Qt as my de facto STL because:

  1. Industry standard - used even in avionics and satellite software
  2. It has been around for decades with little interface change (think about long term support)
  3. It has excellent performance, awesome documentation and enormous user base.
  4. Extensive feature set, way beyond the STL
Winfordwinfred answered 27/5, 2017 at 1:41 Comment(0)
E
0

Would an std::map do the job you are interested in? Have you tried this approach? I don't quite understand what you are trying to do. So please provide a domain example. Cheers.

Escape answered 3/6, 2015 at 12:51 Comment(1)
That would not do it since I want to model a tree-like data structure and a std::map can only map terminal items. E.g. in case myTree.get('path.to.item') is valid and returns a value myTree.get('path.to') implicitly should be valid, too and return a subtree. A possible approach would be some sort of map of maps with every second item containing an optional value and a list of siblings. But this would be what I don't want to reinvent.Kristiankristiansand
B
0

I have some home-cooked code that lets you register custom callbacks for each type in GitHub. It is quite basic and still missing most of the features you would like to have. I'm working on the second version, though. I'm finishing a helper structure that will do most of the job of making callbacks. Tell me if you're interested. Also, you could implement some of those features yourself, as the code to register callbacks is already done. It shouldn't be so difficult.

Borgeson answered 23/8, 2015 at 17:44 Comment(0)
S
0

Using only provided data structures:

First, getters and setters are not native features to c++ you need to call the method one way or another. To make such behaviour occur you can overload assignment operator. I assume you also want to store POD data in your data structure as well. So without knowing the type of the data you're "get"ting, the only option I can think of is to use boost::variant. But still, you have some overloading to do, and you need at least one assignment.

You can check out the documentation. It's pretty straight-forward and easy to understand.

http://www.boost.org/doc/libs/1_61_0/doc/html/variant/tutorial.html


Making your own data structures:

Alternatively, as Dani mentioned, you can come up with your own implementation and keep a register of overloaded methods and so on.

Best

Subtangent answered 25/10, 2016 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.