Module properties name-value pair C++ API framework - reinvention?
Asked Answered
K

2

8

I've often come across APIs that allow users to get and set various parameters that control a module's operation. I now find myself contemplating writing yet another properties API but don't want to reinvent the wheel.

The following is typical basic client code:

setInt("bitrate", 1000);
setEnum("mode", MODE_FAST);
setStr("output file", "music.mp3");

Frequently there are dozens of parameters that can be set and such property sets are often under continuous development.

Some APIs are smarter than others, more advanced features being:

  • Hierarchical grouping of properties
  • Enumeration of properties
  • Numeric parameters with enforced minima and maxima
  • Default parameter values
  • Settings that are enabled, disabled or read only
  • Dynamic parameters - settings that appear, disappear, have min/max set, become enabled, disabled or read only depending on other parameters' state.
  • Properties accessed via UUID key rather than textual name

Beyond the C-style accessors in the sample code above, I've come across frameworks that can:

  • Read/write properties to file (e.g. XML)
  • Read/write settings to Windows Registry
  • Interface with system properties APIs like IPersistPropertyBag
  • Have default dumb GUI implementations, e.g. tree-view or list
  • Have GUI extensions appropriate to minima/maxima/enabled state reducing repetition in GUI code.

I would love to find a well-designed public library that provides a framework for all of the above but so far have drawn a blank. I'm aware of Boost.PropertyTree but it's only really a skeleton. Are there other portable properties API frameworks that I should be aware of?

Kelila answered 26/4, 2011 at 8:1 Comment(4)
I've reinvented this wheel myself three times (for practice), every time running a search on the topic, and still found nothing!.. D'oh...Bourgeoisie
Its questions like this that make me wish I could publish some of the proprietary code I work on... A feature that you don't list and I would like to see in a public API is Thread-Safe.Imagery
@McKay So why not get it published either as a commercial product or open source? Personally I don't need the core to be thread safe in itself any more than I need std::vector to be thread safe but it should be easy to write thread safe libraries and implementations using such an API.Kelila
That may happen one day. But, the current focus is on the larger project. Our tree has the ability to set locking on a per property basis, which with property notification callbacks serves as a simple thread messaging mechanism.Imagery
J
2

One of the key elements of the Qt property system is actually a very solid implementation of a variant type class QVariant that enables you to get rid of the typed setInt, setString ... calls.

If you get a hold of a similar well behaved class e.g. possibly Boost::Variant or something similar a property system is fairly easy to implement using a map of string as the backing part.

One of the conveniences of the qt property system is that you can override setter and getter functions without the user having to know about them. E.g. given your generic property setter being setProperty(name, value). The implementor can in the class declaration denote that the property "speed" has its own setter setSpeed(float value) so that when a user of your class invokes the generic version of setProperty("speed", 100), the system will call setSpeed(100) on your instance.

If you don't need features like that you can probably implement your own property system. Gamasutra has a piece on implementing reflection in C++ that might help you too.

Jacobsohn answered 8/6, 2011 at 16:54 Comment(0)
G
0

If you don't mind having a dependency on Qt Core, then Qt has a fairly well designed property system. This means though that you will be relying on Qt's MOC.

Gut answered 2/6, 2011 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.