Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 861
paperjam Avatar asked Apr 26 '11 08:04

paperjam


1 Answers

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.

like image 69
Harald Scheirich Avatar answered Oct 02 '22 19:10

Harald Scheirich