ValueTree getRoot


#1

Sorry, maybe I'm blind, but I'm missing a getRoot method for ValueTree.

Should be possible in a few lines of code, if it's not already there. And it would be quite handy.

BTW, ValueTree is guaranted to be acyclic, isn't it?

Thanks, daniel


#2

Yes, it's acyclic.

It's an interesting question as to why, having made very heavy use of ValueTree in many different cases, I've never needed to know the root. I've a feeling that if you need to know the root, there could be some bad design going on, but I can't quite put my finger on why that is. Perhaps explain what the reason is why you need to get it?

Of course you could also easily implement it as a free function, it's a one-liner e.g.

ValueTree getRoot (const ValueTree& v)    { return v.getParent().isValid() ? getRoot (v.getParent()) : v; }

#3

You can smell that ;-) It's not really bad design but improvable...

When a leave changes I recalculate the whole tree, that's why I needed the root node. But I probably will optimize it to only recalculate from the parent node on...

I implemented it similar as you propose (just not that elegant...) but I thought, it is a quite common use...

Anyway, I can live without...

Thanks, daniel


#4

No, it's not bad design at all! Using a free function for things like this is totally OK. It's not optimal in terms of implementation, but wouldn't be a problem in practice.

The thing that I wonder about this is why I've never needed to do this. For Components, it's really common to need getTopLevelComponent, but for ValueTrees it just feels wrong somehow.