The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. This class acts as a wrapper around a property inside a ValueTree.
  24. If the property inside the ValueTree is missing or empty the ValueWithDefault will automatically
  25. return a default value, which can be specified when initialising the ValueWithDefault.
  26. @tags{DataStructures}
  27. */
  28. class ValueWithDefault
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an unitialised ValueWithDefault. Initialise it using one of the referTo() methods. */
  33. ValueWithDefault() : undoManager (nullptr) {}
  34. /** Creates an ValueWithDefault object. The default value will be an empty var. */
  35. ValueWithDefault (ValueTree& tree, const Identifier& propertyID,
  36. UndoManager* um)
  37. : targetTree (tree),
  38. targetProperty (propertyID),
  39. undoManager (um),
  40. defaultValue()
  41. {
  42. }
  43. /** Creates an ValueWithDefault object. The default value will be defaultToUse. */
  44. ValueWithDefault (ValueTree& tree, const Identifier& propertyID,
  45. UndoManager* um, const var& defaultToUse)
  46. : targetTree (tree),
  47. targetProperty (propertyID),
  48. undoManager (um),
  49. defaultValue (defaultToUse)
  50. {
  51. }
  52. /** Creates a ValueWithDefault object from another ValueWithDefault object. */
  53. ValueWithDefault (const ValueWithDefault& other)
  54. : targetTree (other.targetTree),
  55. targetProperty (other.targetProperty),
  56. undoManager (other.undoManager),
  57. defaultValue (other.defaultValue)
  58. {
  59. }
  60. //==============================================================================
  61. /** Returns the current value of the property. If the property does not exist or is empty,
  62. returns the default value.
  63. */
  64. var get() const noexcept
  65. {
  66. if (isUsingDefault())
  67. return defaultValue;
  68. return targetTree[targetProperty];
  69. }
  70. /** Returns the current property as a Value object. */
  71. Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
  72. /** Returns the current default value. */
  73. var getDefault() const { return defaultValue; }
  74. /** Sets the default value to a new var. */
  75. void setDefault (const var& newDefault)
  76. {
  77. if (defaultValue != newDefault)
  78. defaultValue = newDefault;
  79. }
  80. /** Returns true if the property does not exist or is empty. */
  81. bool isUsingDefault() const
  82. {
  83. return ! targetTree.hasProperty (targetProperty);
  84. }
  85. /** Resets the property to an empty var. */
  86. void resetToDefault() noexcept
  87. {
  88. targetTree.removeProperty (targetProperty, nullptr);
  89. }
  90. //==============================================================================
  91. /** Sets the property and returns the new ValueWithDefault. This will modify the property in the referenced ValueTree. */
  92. ValueWithDefault& operator= (const var& newValue)
  93. {
  94. setValue (newValue, undoManager);
  95. return *this;
  96. }
  97. /** Sets the property. This will actually modify the property in the referenced ValueTree. */
  98. void setValue (const var& newValue, UndoManager* undoManagerToUse)
  99. {
  100. targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
  101. }
  102. //==============================================================================
  103. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree. */
  104. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um)
  105. {
  106. referToWithDefault (tree, property, um, var());
  107. }
  108. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree,
  109. and specifies a default value to use.
  110. */
  111. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const var& defaultVal)
  112. {
  113. referToWithDefault (tree, property, um, defaultVal);
  114. }
  115. //==============================================================================
  116. /** Returns a reference to the ValueTree containing the referenced property. */
  117. ValueTree& getValueTree() noexcept { return targetTree; }
  118. /** Returns the property ID of the referenced property. */
  119. Identifier& getPropertyID() noexcept { return targetProperty; }
  120. private:
  121. //==============================================================================
  122. ValueTree targetTree;
  123. Identifier targetProperty;
  124. UndoManager* undoManager;
  125. var defaultValue;
  126. //==============================================================================
  127. void referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const var& defaultVal)
  128. {
  129. targetTree = v;
  130. targetProperty = i;
  131. undoManager = um;
  132. defaultValue = defaultVal;
  133. }
  134. };
  135. } // namespace juce