juce-framework / JUCE

JUCE is an open-source cross-platform C++ application framework for desktop and mobile applications, including VST, VST3, AU, AUv3, LV2 and AAX audio plug-ins.
https://juce.com
Other
6.31k stars 1.67k forks source link

Allow daisy-chaining when adding children to ValueTrees #1332

Open ImJimmi opened 5 months ago

ImJimmi commented 5 months ago

Currently, it's possible to daisy-chain property setters on a value tree:

juce::ValueTree {"Tree"}
    .setProperty("foo", 10, nullptr)
    .setProperty("bar", 20, nullptr);

This PR adds this behaviour to the child-adders as well, allowing a whole tree to be constructed this way:

juce::ValueTree {"Root"}
    .setProperty("foo", 10, nullptr)
    .setProperty("bar", 20, nullptr)
    .appendChild (juce::ValueTree {"Branch"}
                      .setProperty ("x", y, nullptr)
                      .appendChild (juce::ValueTree {"Leaf"}, nullptr),
                  nullptr)
    .addChild (juce::ValueTree {"Branch"}, 0, nullptr);

This is useful when loading value-trees from places external to the current call site (.e.g loading state from XML) and then adding on any additional children (e.g. any state that shouldn't be persisted to a file).

It also allows for better const-ness as the tree doesn't need to be changed after initialisation:

juce::ValueTree nonConst {"Tree"};
nonConst.appendChild (juce::ValueTree {"Data"}, nullptr);

const auto isConst = juce::ValueTree {"Tree"}
                         .appendChild (juce::ValueTree {"Data"}, nullptr);
ImJimmi commented 5 months ago

This would go nicely with https://github.com/juce-framework/JUCE/pull/1333 to make constructing value trees even cleaner:

juce::ValueTree {"Root"}
    .setProperty("foo", 10)
    .setProperty("bar", 20)
    .appendChild (juce::ValueTree {"Branch"}
                      .setProperty ("x", y)
                      .appendChild (juce::ValueTree {"Leaf"}))
    .addChild (juce::ValueTree {"Branch"}, 0);