I've been looking at this off and on for a while now. And I've got some working but slightly ugly code for ReferenceCountedObjects which does exactly the right thing and passes all the tests I can think of.
However - I was just looking at extending this into maintaining a one way synchronisation of a ValueTree to another thread, and it's not easy. Making changes to a ValueTree is not a lock-free operation. So I can't just send updates as they happen and have the critical thread apply them with the normal methods.
So I figured it might be good (for small trees at least) to just create a deep copy of the ValueTree if anything changes and pass it over. But addChild() is also not lock-free!
I think my solution for now is just to always copy the whole ValueTree over each time, so there's no adding or removing anything.
However feature requests:
- it'd be nice to just update a single property value that's changed in the clone of the value tree. That'd be a lot easier if I could get a pointer to the underlying value tree object to use in a std::map ...
- If ValueTree::addChild could be made lock-free then this would be useful too and I think could get away with copying less of the tree each time. But that requires ReferenceCountedArray's add() method to not allocate any memory. This could be fixed by specifying the number of children to reserve space for when the tree is created.
Or is there some other neat trick I've missed that makes this all much easier.