Loading...
Searching...
No Matches
juce_ValueTree.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API ValueTree final
{
JUCE_PUBLIC_IN_DLL_BUILD (class SharedObject)
public:
//==============================================================================
ValueTree() noexcept;
explicit ValueTree (const Identifier& type);
ValueTree (const Identifier& type,
std::initializer_list<NamedValueSet::NamedValue> properties,
std::initializer_list<ValueTree> subTrees = {});
ValueTree (const ValueTree&) noexcept;
ValueTree (ValueTree&&) noexcept;
ValueTree& operator= (const ValueTree&);
~ValueTree();
bool operator== (const ValueTree&) const noexcept;
bool operator!= (const ValueTree&) const noexcept;
bool isEquivalentTo (const ValueTree&) const;
//==============================================================================
bool isValid() const noexcept { return object != nullptr; }
ValueTree createCopy() const;
void copyPropertiesFrom (const ValueTree& source, UndoManager* undoManager);
void copyPropertiesAndChildrenFrom (const ValueTree& source, UndoManager* undoManager);
//==============================================================================
Identifier getType() const noexcept;
bool hasType (const Identifier& typeName) const noexcept;
//==============================================================================
const var& getProperty (const Identifier& name) const noexcept;
var getProperty (const Identifier& name, const var& defaultReturnValue) const;
const var* getPropertyPointer (const Identifier& name) const noexcept;
const var& operator[] (const Identifier& name) const noexcept;
ValueTree& setProperty (const Identifier& name, const var& newValue, UndoManager* undoManager);
bool hasProperty (const Identifier& name) const noexcept;
void removeProperty (const Identifier& name, UndoManager* undoManager);
void removeAllProperties (UndoManager* undoManager);
int getNumProperties() const noexcept;
Identifier getPropertyName (int index) const noexcept;
Value getPropertyAsValue (const Identifier& name, UndoManager* undoManager,
bool shouldUpdateSynchronously = false);
//==============================================================================
int getNumChildren() const noexcept;
ValueTree getChild (int index) const;
ValueTree getChildWithName (const Identifier& type) const;
ValueTree getOrCreateChildWithName (const Identifier& type, UndoManager* undoManager);
ValueTree getChildWithProperty (const Identifier& propertyName, const var& propertyValue) const;
void addChild (const ValueTree& child, int index, UndoManager* undoManager);
void appendChild (const ValueTree& child, UndoManager* undoManager);
void removeChild (const ValueTree& child, UndoManager* undoManager);
void removeChild (int childIndex, UndoManager* undoManager);
void removeAllChildren (UndoManager* undoManager);
void moveChild (int currentIndex, int newIndex, UndoManager* undoManager);
bool isAChildOf (const ValueTree& possibleParent) const noexcept;
int indexOf (const ValueTree& child) const noexcept;
ValueTree getParent() const noexcept;
ValueTree getRoot() const noexcept;
ValueTree getSibling (int delta) const noexcept;
//==============================================================================
struct Iterator
{
Iterator (const ValueTree&, bool isEnd);
Iterator& operator++();
bool operator== (const Iterator&) const;
bool operator!= (const Iterator&) const;
ValueTree operator*() const;
using difference_type = std::ptrdiff_t;
using value_type = ValueTree;
using reference = ValueTree&;
using pointer = ValueTree*;
using iterator_category = std::forward_iterator_tag;
private:
SharedObject** internal = nullptr;
};
Iterator begin() const noexcept;
Iterator end() const noexcept;
//==============================================================================
std::unique_ptr<XmlElement> createXml() const;
static ValueTree fromXml (const XmlElement& xml);
static ValueTree fromXml (const String& xmlText);
String toXmlString (const XmlElement::TextFormat& format = {}) const;
//==============================================================================
void writeToStream (OutputStream& output) const;
static ValueTree readFromStream (InputStream& input);
static ValueTree readFromData (const void* data, size_t numBytes);
static ValueTree readFromGZIPData (const void* data, size_t numBytes);
//==============================================================================
class JUCE_API Listener
{
public:
virtual ~Listener() = default;
virtual void valueTreePropertyChanged (ValueTree& treeWhosePropertyHasChanged,
const Identifier& property);
virtual void valueTreeChildAdded (ValueTree& parentTree,
ValueTree& childWhichHasBeenAdded);
virtual void valueTreeChildRemoved (ValueTree& parentTree,
ValueTree& childWhichHasBeenRemoved,
int indexFromWhichChildWasRemoved);
virtual void valueTreeChildOrderChanged (ValueTree& parentTreeWhoseChildrenHaveMoved,
int oldIndex, int newIndex);
virtual void valueTreeParentChanged (ValueTree& treeWhoseParentHasChanged);
virtual void valueTreeRedirected (ValueTree& treeWhichHasBeenChanged);
};
void addListener (Listener* listener);
void removeListener (Listener* listener);
ValueTree& setPropertyExcludingListener (Listener* listenerToExclude,
const Identifier& name, const var& newValue,
UndoManager* undoManager);
void sendPropertyChangeMessage (const Identifier& property);
//==============================================================================
template <typename ElementComparator>
void sort (ElementComparator& comparator, UndoManager* undoManager, bool retainOrderOfEquivalentItems)
{
if (object != nullptr)
{
OwnedArray<ValueTree> sortedList;
createListOfChildren (sortedList);
ComparatorAdapter<ElementComparator> adapter (comparator);
sortedList.sort (adapter, retainOrderOfEquivalentItems);
reorderChildren (sortedList, undoManager);
}
}
int getReferenceCount() const noexcept;
#if JUCE_ALLOW_STATIC_NULL_VARIABLES && ! defined (DOXYGEN)
/* An invalid ValueTree that can be used if you need to return one as an error condition, etc. */
[[deprecated ("If you need an empty ValueTree object, just use ValueTree() or {}.")]]
static const ValueTree invalid;
#endif
private:
//==============================================================================
friend class SharedObject;
ReferenceCountedObjectPtr<SharedObject> object;
ListenerList<Listener> listeners;
template <typename ElementComparator>
struct ComparatorAdapter
{
ComparatorAdapter (ElementComparator& comp) noexcept : comparator (comp) {}
int compareElements (const ValueTree* const first, const ValueTree* const second)
{
return comparator.compareElements (*first, *second);
}
private:
ElementComparator& comparator;
JUCE_DECLARE_NON_COPYABLE (ComparatorAdapter)
};
void createListOfChildren (OwnedArray<ValueTree>&) const;
void reorderChildren (const OwnedArray<ValueTree>&, UndoManager*);
explicit ValueTree (ReferenceCountedObjectPtr<SharedObject>) noexcept;
explicit ValueTree (SharedObject&) noexcept;
};
} // namespace juce
juce::Identifier
Definition juce_Identifier.h:39
juce::InputStream
Definition juce_InputStream.h:37
juce::ListenerList
Definition juce_ListenerList.h:81
juce::OutputStream
Definition juce_OutputStream.h:38
juce::OwnedArray
Definition juce_OwnedArray.h:50
juce::OwnedArray::sort
void sort(ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) noexcept
Definition juce_OwnedArray.h:868
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::String
Definition juce_String.h:53
juce::UndoManager
Definition juce_UndoManager.h:52
juce::ValueTree::Listener
Definition juce_ValueTree.h:481
juce::ValueTree::Listener::valueTreeRedirected
virtual void valueTreeRedirected(ValueTree &treeWhichHasBeenChanged)
Definition juce_ValueTree.cpp:1102
juce::ValueTree::Listener::valueTreeChildRemoved
virtual void valueTreeChildRemoved(ValueTree &parentTree, ValueTree &childWhichHasBeenRemoved, int indexFromWhichChildWasRemoved)
Definition juce_ValueTree.cpp:1099
juce::ValueTree::Listener::valueTreeChildOrderChanged
virtual void valueTreeChildOrderChanged(ValueTree &parentTreeWhoseChildrenHaveMoved, int oldIndex, int newIndex)
Definition juce_ValueTree.cpp:1100
juce::ValueTree::Listener::~Listener
virtual ~Listener()=default
juce::ValueTree::Listener::valueTreeParentChanged
virtual void valueTreeParentChanged(ValueTree &treeWhoseParentHasChanged)
Definition juce_ValueTree.cpp:1101
juce::ValueTree::Listener::valueTreePropertyChanged
virtual void valueTreePropertyChanged(ValueTree &treeWhosePropertyHasChanged, const Identifier &property)
Definition juce_ValueTree.cpp:1097
juce::ValueTree::Listener::valueTreeChildAdded
virtual void valueTreeChildAdded(ValueTree &parentTree, ValueTree &childWhichHasBeenAdded)
Definition juce_ValueTree.cpp:1098
juce::ValueTree
Definition juce_ValueTree.h:72
juce::ValueTree::begin
Iterator begin() const noexcept
Definition juce_ValueTree.cpp:888
juce::ValueTree::createXml
std::unique_ptr< XmlElement > createXml() const
Definition juce_ValueTree.cpp:995
juce::ValueTree::toXmlString
String toXmlString(const XmlElement::TextFormat &format={}) const
Definition juce_ValueTree.cpp:1026
juce::ValueTree::isValid
bool isValid() const noexcept
Definition juce_ValueTree.h:176
juce::ValueTree::addListener
void addListener(Listener *listener)
Definition juce_ValueTree.cpp:969
juce::ValueTree::setPropertyExcludingListener
ValueTree & setPropertyExcludingListener(Listener *listenerToExclude, const Identifier &name, const var &newValue, UndoManager *undoManager)
Definition juce_ValueTree.cpp:766
juce::ValueTree::ValueTree
ValueTree() noexcept
Definition juce_ValueTree.cpp:578
juce::ValueTree::fromXml
static ValueTree fromXml(const XmlElement &xml)
Definition juce_ValueTree.cpp:1000
juce::ValueTree::end
Iterator end() const noexcept
Definition juce_ValueTree.cpp:889
juce::ValueTree::removeListener
void removeListener(Listener *listener)
Definition juce_ValueTree.cpp:980
juce::ValueTree::isEquivalentTo
bool isEquivalentTo(const ValueTree &) const
Definition juce_ValueTree.cpp:653
juce::ValueTree::sendPropertyChangeMessage
void sendPropertyChangeMessage(const Identifier &property)
Definition juce_ValueTree.cpp:988
juce::ValueTree::sort
void sort(ElementComparator &comparator, UndoManager *undoManager, bool retainOrderOfEquivalentItems)
Definition juce_ValueTree.h:594
juce::Value
Definition juce_Value.h:51
juce::XmlElement
Definition juce_XmlElement.h:83
juce::var
Definition juce_Variant.h:42
juce::ValueTree::Iterator
Definition juce_ValueTree.h:403