I’m working on a GTK application and found there were a few spots where I needed to iterate over a TreeModel. Doing this in GtkD is a bit of a pain since TreeModelT isn’t implemented as a Range and so you are forced to use the model methods rather then the convenience of foreach.
To make my life easier, I created a small struct that can be used to wrap a TreeModelIF interface and allows it to be used in D’s foreach construct as per this example:
foreach(TreeIter iter; TreeIterRange(lsProfiles)) { if (lsProfiles.getValue(iter, COLUMN_UUID).getString() == window.uuid) { lsProfiles.setValue(iter, COLUMN_NAME, profile.name); } }
/** * An implementation of a range that allows using foreach over a range */ struct TreeIterRange { private: TreeModelIF model; TreeIter iter; bool _empty; public: this(TreeModelIF model) { this.model = model; _empty = model.getIterFirst(iter); } @property bool empty() { return _empty; } @property auto front() { return iter; } void popFront() { _empty = !model.iterNext(iter); } /** * Based on the example here https://www.sociomantic.com/blog/2010/06/opapply-recipe/#.Vm8mW7grKEI */ int opApply(int delegate(ref TreeIter iter) dg) { int result = 0; TreeIter iter; bool hasNext = model.getIterFirst(iter); while (hasNext) { result = dg(iter); if (result) break; hasNext = model.iterNext(iter); } return result; } }