Dragging ValueTrees


#1

Hi all, I was wondering if anybody could offer any advice for using ValueTrees as the SourceDetails::description var member?

My use case is that I have a music library stored as a ValueTree in a similar way to the juce demo. When I drag from the library table to my player components these need to access the related tree items to load the file and modify properties and child trees to edit thing such as loop and cue points.

At the moment I have this working with an incredibly ugly hack by storing the parent tree’s address as a pointer_sized_int var and then an array of ID’s which relate to the child tree’s IDs. When this is dropped I have to dynamic_cast the address back to a ValueTree and then search the tree for the desired children. This not only looks hideous but is pretty slow and probably quite unsafe.

Is there anybody doing this type of thing? What would be ideal would be some var constructors to hold ValueTrees but I understand these aren’t really primitive types. Getting access to the underlying ValueTree::SharedObject would also make this possible using the existing var(ReferenceCountedObject *object) constructor but again is not really how the ValueTree is intended to be used.

Any pointers would be much appreciated.


#2

A var can hold a pointer to any ReferenceCountedObject, so there’s no need to hack it with pointer casts.


#3

Yes, but ValueTree is not itself a ReferenceCountedObject, it holds a private SharedObject member which does the reference counting. I’ve had to create a wrapper class to hold a value tree in a ReferenceCountedObject so it can be passed around as a var. Seems long winded with a lot of reference counting going on but works for now.

[code]class ReferenceCountedValueTree : public ReferenceCountedObject
{
public:

ReferenceCountedValueTree (ValueTree treeToReference)
:   tree (treeToReference)
{
}

~ReferenceCountedValueTree()
{
}

void setValueTree (ValueTree newTree)
{
    tree = newTree;
}

ValueTree getValueTree()
{
    return tree;
}

typedef ReferenceCountedObjectPtr<ReferenceCountedValueTree> Ptr;

private:

ValueTree tree;

};[/code]

Any better ideas are welcome,
Cheers.


#4

No, that’s about the simplest/best/safest way to do it.

If it was trivial to store a ValueTree in a var, it would open a wormhole of worms, as it would be easy to break the strict direction of containership that ValueTrees allow; i.e., a ValueTree could hold a reference-counted link to itself or its parent, causing circular lifetime issues. By using a specific object for the purpose (and it really is very simple), your code is explicit, and you are less likely to fall into a trap.

I’ve had to use a similar (effectively identical) construct before, and will do so again. Have no fear! Just don’t get carried away using it to hold ValueTrees in ValueTrees [unless you really know what you’re doing :slight_smile: ]


#5

Thanks for the advice, it seems to work fine and as you say is fairly simple.

I heard that if you store a ValueTree as a property of itself the git repository will self distruct! Now that’s a jucequake.