From 4c44d96fdf70448b3386414543c668bcdc823d8a Mon Sep 17 00:00:00 2001 From: Tom Poole Date: Fri, 19 Jan 2018 16:10:35 +0000 Subject: [PATCH] Added an initialiser list based ValueTree constructor --- .../values/juce_ValueTree.cpp | 14 ++++++ .../values/juce_ValueTree.h | 46 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/modules/juce_data_structures/values/juce_ValueTree.cpp b/modules/juce_data_structures/values/juce_ValueTree.cpp index 56c64fc6b5..21ee4872ef 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.cpp +++ b/modules/juce_data_structures/values/juce_ValueTree.cpp @@ -589,6 +589,20 @@ ValueTree::ValueTree (const Identifier& type) : object (new ValueTree::SharedOb jassert (type.toString().isNotEmpty()); // All objects must be given a sensible type name! } +#if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS +ValueTree::ValueTree (const Identifier& type, + std::initializer_list> properties, + std::initializer_list subTrees) + : ValueTree (type) +{ + for (auto& prop : properties) + setProperty (prop.first, prop.second, nullptr); + + for (auto& tree : subTrees) + addChild (tree, -1, nullptr); +} +#endif + ValueTree::ValueTree (SharedObject* so) noexcept : object (so) { } diff --git a/modules/juce_data_structures/values/juce_ValueTree.h b/modules/juce_data_structures/values/juce_ValueTree.h index 8243b507fb..f1b31969b1 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.h +++ b/modules/juce_data_structures/values/juce_ValueTree.h @@ -80,11 +80,57 @@ public: ValueTree() noexcept; /** Creates an empty ValueTree with the given type name. + Like an XmlElement, each ValueTree has a type, which you can access with getType() and hasType(). */ explicit ValueTree (const Identifier& type); + #if JUCE_COMPILER_SUPPORTS_INITIALIZER_LISTS + /** Creates a value tree from nested lists of properties and ValueTrees. + + This code, + + @code + ValueTree groups + { "ParameterGroups", {}, + { + { "Group", {{ "name", "Tone Controls" }}, + { + { "Parameter", {{ "id", "distortion" }, { "value", 0.5 }}}, + { "Parameter", {{ "id", "reverb" }, { "value", 0.5 }}} + } + }, + { "Group", {{ "name", "Other Controls" }}, + { + { "Parameter", {{ "id", "drywet" }, { "value", 0.5 }}}, + { "Parameter", {{ "id", "gain" }, { "value", 0.5 }}} + } + } + } + }; + @endcode + + produces this tree: + + @verbatim + + + + + + + + + + + @endverbatim + */ + ValueTree (const Identifier& type, + std::initializer_list> properties, + std::initializer_list subTrees = {}); + #endif + /** Creates a reference to another ValueTree. */ ValueTree (const ValueTree&) noexcept;