Calling ValueTree::getChild(int) the ValueTree is passed by copy as his underling pointer

Hi, I’d like to understand how to refer to the same value tree in a scenario like this below.

I’m sure the string searched is equal to a property contained by a ValueTree, but it seems that calling ValueTree::getChild(int) the ValueTree is passed by copy as his underling pointer … while reading in doc the underling pointer should be the same

ValueTree Engine::getTeatroValueTree(String const teatroNome) const
    for (int i = 0; i < treeStructure.getNumChildren(); i++)
        ValueTree regioneValueTree(treeStructure.getChild(i));
        for (int i2 = 0; i2 < regioneValueTree.getNumChildren(); i2++)
            ValueTree provinciaValueTree(regioneValueTree.getChild(i2));
            for (int i3 = 0; i3 < regioneValueTree.getNumChildren(); i3++)
                ValueTree teatroValueTree(provinciaValueTree.getChild(i3));
                if (teatroNome == teatroValueTree.getProperty(IDs::nomeProvincia).toString()) { return teatroValueTree; DBG("I'm here"); }
    return ValueTree();

Can anyone give me an help with this ?

really thank you in advice

A ValueTree is a wrapper for the actual data. How are you discerning this issue? to clarify, yes getChild returns a copy of the ValueTree object, but, as mentioned, this object still refers to the same underlying data. You have to be very specific if you want to actual copy ValueTree data.

1 Like

@cpr2323 Really thank you for your reply, the problem is that I don’t understand how to manage a ValueTree Structure.

Let’s suppose I have a rootValueTree with a child who in turn has another child, so:

and let’s suppose that childOfChild has a property called “Property” with value “value”

when I write this:

VlueTree child(rootValueTree.getChild(0));
VlueTree childOfChild(child.getChild(0));

I expect it to print: value, while childOfChild has no that property…

I can’t help based on that small snippet, but for me, developing and understanding of things can sometimes be aided by looking at the data. If you are using Visual Studio you can install and use the juce.natvis portion of this repo (GitHub - jcredland/juce-toys: Debugging utilities in a JUCE Module, plus NatVis and LLDB customizations) to look at your ValueTree in the debugger. That extensions helps the debugger understand the format of the ValueTree (and other juce related datatypes), so you can easily navigate and examine them. Or, you can run your ValueTrees through some function that dumps them our for viewing. Here is a quick and dirty one that I use.

void dumpValueTreeContent (ValueTree vt, bool displayProperties, int indentLevel, std::function<void (String)> displayFunction)
     jassert (displayFunction != nullptr);
     String indentString { std::string (indentLevel, ' ') };
     displayFunction (indentString + vt.getType ().toString ());
     if (displayProperties)
         for (auto propIndex { 0 }; propIndex < vt.getNumProperties (); ++propIndex)
             const auto propertyName { vt.getPropertyName (propIndex) };
             displayFunction (indentString + "  " + propertyName + " = '" + vt.getProperty (propertyName).toString () + "'");
     for (const auto& child : vt)
         dumpValueTreeContent (child, displayProperties, indentLevel + 1, displayFunction);
1 Like

If you just need something quick and dirty and don’t mind reading XML, DBG (myValueTree.toXmlString()); is pretty handy, too!

@chopin20 How are you creating the ValueTree? Assuming the tree was constructed properly, your code to print the property value should work as expected.

Here’s a minimal example that’s similar to what you’re trying to do, and it prints the string “Value” when the DBG statement is hit:

juce::ValueTree tree ("Root", {}, {
    juce::ValueTree { "Child", {}, {
        juce::ValueTree { "InnerChild", {{ "Property", "Value" }}}

auto child = tree.getChild (0);
auto innerChild = child.getChild (0);
DBG (innerChild.getProperty ("Property").toString());