ValueTree and infinite recursion



I’m wondering what is the expected behaviour of this:

ValueTree a = ...;
ValueTree b = ...;
a.addChild(b, -1, 0);
b.addChild(a, -1, 0);


If you try to add a child that already has a parent, it’ll assert to warn you, and will remove itself from its current parent before being added to its new one. (I think it says this in the comments for addChild() ?)


Well, it’s because I’ve come to the assert I was wondering what would happen if I removed it.
The idea is that I’m serializing objects to XML with this, and some of the object cross refer to each other.
So, I was wondering if the code could kind of handle the pointer to node that contains a pointer to initial node kind.

Put differently, is it possible to have a kind of weak reference value tree, that’s only used as a placeholder for export/import ?


No, they need to have only one parent, otherwise things could get extremely confused, not to mention the problems of having to check for infinite recursion. Not really sure how a weak reference would wor, but in a data structure as fundamental as this, I think “keep it simple” is the best motto!


You could quite easily use an ID to refer to another node in the tree.

You could go one step further, and store a DynamicObject which holds an actual reference to the node in question (so you don’t have to look it up every time you access it). When it comes to serialising the tree, you’d turn that reference object into some form of locator (e.g. an ID or a path); when reading it in, you’d have a pass that replaces these locators with dynamic references to the nodes they describe.