ValueTreeWrapper


#1

I’ve made a macro to help when making ValueTree wrapper classes. I’d originally done this with a base class, but that can only really automate getting something with a node and some functions; the stuff that’s really tedious is adding constructors and operators, so I used to not bother writing them.

However, if your wrapper class only needs to contain a ValueTree node (i.e. you don’t need any other members), then it’s nice to be able to treat it more like a ValueTree, and pass it around in the same kind of way. As such, this macro gives a class a bunch of ready-made constructors and operators that allow you to use them almost as if they were ValueTrees, but with your own extra functionality.

ValueTreeWrapper.h

The only rules are that (1) you must provide your own default constructor [you might want an invalid node, though you’ll probably want some default node type] and (2) you don’t add any other members [as you’d need your own operators/constructors for them, so this macro wouldn’t be much use]. Oh, and (3) you can’t use this macro in a subclass of an object like it, as the node member would clash. There are ways around that, but I’m not in the mood to make it more complicated yet :slight_smile:

May as well post the code too, it’s only short…

#define DECLARE_VALUETREEWRAPPER(ObjectType) \ \ public: \ \ ObjectType (const ValueTree& sourceNode) : node (sourceNode) {}; \ ObjectType (const ObjectType& other) : node (other.node) {}; \ ObjectType (const Identifier& nodeTypeId) : node (nodeTypeId) {}; \ ObjectType& operator= (const ObjectType& other) \ { \ node = other.node; \ return *this; \ }; \ operator const ValueTree&() const throw() { return node; } \ \ ValueTree getNode () const { return node; } \ \ const var getProperty (const Identifier& id) { return node[id]; } \ \ void setProperty (const Identifier& id, const var& value, UndoManager* undoManager = 0) \ { \ node.setProperty (id, value, undoManager); \ } \ \ protected: \ \ ValueTree node; \ \