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:
Beyond the C-style accessors in the sample code above, I've come across frameworks that can:
IPersistPropertyBag
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With