Audio plugin host https://kx.studio/carla
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.

1116 lines
35KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ValueTree::SharedObject : public ReferenceCountedObject
  18. {
  19. public:
  20. typedef ReferenceCountedObjectPtr<SharedObject> Ptr;
  21. explicit SharedObject (Identifier t) noexcept
  22. : type (t), parent (nullptr)
  23. {
  24. }
  25. SharedObject (const SharedObject& other)
  26. : ReferenceCountedObject(),
  27. type (other.type), properties (other.properties), parent (nullptr)
  28. {
  29. for (int i = 0; i < other.children.size(); ++i)
  30. {
  31. SharedObject* const child = new SharedObject (*other.children.getObjectPointerUnchecked(i));
  32. child->parent = this;
  33. children.add (child);
  34. }
  35. }
  36. ~SharedObject()
  37. {
  38. jassert (parent == nullptr); // this should never happen unless something isn't obeying the ref-counting!
  39. for (int i = children.size(); --i >= 0;)
  40. {
  41. const Ptr c (children.getObjectPointerUnchecked(i));
  42. c->parent = nullptr;
  43. children.remove (i);
  44. c->sendParentChangeMessage();
  45. }
  46. }
  47. template <typename Method>
  48. void callListeners (Method method, ValueTree& tree) const
  49. {
  50. const int numListeners = valueTreesWithListeners.size();
  51. if (numListeners > 0)
  52. {
  53. if (numListeners == 1)
  54. {
  55. valueTreesWithListeners.getUnchecked(0)->listeners.call (method, tree);
  56. }
  57. else
  58. {
  59. const SortedSet<ValueTree*> listenersCopy (valueTreesWithListeners);
  60. for (int i = 0; i < numListeners; ++i)
  61. {
  62. ValueTree* const v = listenersCopy.getUnchecked(i);
  63. if (i == 0 || valueTreesWithListeners.contains (v))
  64. v->listeners.call (method, tree);
  65. }
  66. }
  67. }
  68. }
  69. template <typename Method, typename ParamType>
  70. void callListeners (Method method, ValueTree& tree, ParamType& param2) const
  71. {
  72. const int numListeners = valueTreesWithListeners.size();
  73. if (numListeners > 0)
  74. {
  75. if (numListeners == 1)
  76. {
  77. valueTreesWithListeners.getUnchecked(0)->listeners.call (method, tree, param2);
  78. }
  79. else
  80. {
  81. const SortedSet<ValueTree*> listenersCopy (valueTreesWithListeners);
  82. for (int i = 0; i < numListeners; ++i)
  83. {
  84. ValueTree* const v = listenersCopy.getUnchecked(i);
  85. if (i == 0 || valueTreesWithListeners.contains (v))
  86. v->listeners.call (method, tree, param2);
  87. }
  88. }
  89. }
  90. }
  91. void sendPropertyChangeMessage (const Identifier property)
  92. {
  93. ValueTree tree (this);
  94. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  95. t->callListeners (&ValueTree::Listener::valueTreePropertyChanged, tree, property);
  96. }
  97. void sendChildAddedMessage (ValueTree child)
  98. {
  99. ValueTree tree (this);
  100. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  101. t->callListeners (&ValueTree::Listener::valueTreeChildAdded, tree, child);
  102. }
  103. void sendChildRemovedMessage (ValueTree child)
  104. {
  105. ValueTree tree (this);
  106. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  107. t->callListeners (&ValueTree::Listener::valueTreeChildRemoved, tree, child);
  108. }
  109. void sendChildOrderChangedMessage()
  110. {
  111. ValueTree tree (this);
  112. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  113. t->callListeners (&ValueTree::Listener::valueTreeChildOrderChanged, tree);
  114. }
  115. void sendParentChangeMessage()
  116. {
  117. ValueTree tree (this);
  118. for (int j = children.size(); --j >= 0;)
  119. if (SharedObject* const child = children.getObjectPointer (j))
  120. child->sendParentChangeMessage();
  121. callListeners (&ValueTree::Listener::valueTreeParentChanged, tree);
  122. }
  123. void setProperty (const Identifier name, const var& newValue, UndoManager* const undoManager)
  124. {
  125. if (undoManager == nullptr)
  126. {
  127. if (properties.set (name, newValue))
  128. sendPropertyChangeMessage (name);
  129. }
  130. else
  131. {
  132. if (const var* const existingValue = properties.getVarPointer (name))
  133. {
  134. if (*existingValue != newValue)
  135. undoManager->perform (new SetPropertyAction (this, name, newValue, *existingValue, false, false));
  136. }
  137. else
  138. {
  139. undoManager->perform (new SetPropertyAction (this, name, newValue, var(), true, false));
  140. }
  141. }
  142. }
  143. bool hasProperty (const Identifier name) const noexcept
  144. {
  145. return properties.contains (name);
  146. }
  147. void removeProperty (const Identifier name, UndoManager* const undoManager)
  148. {
  149. if (undoManager == nullptr)
  150. {
  151. if (properties.remove (name))
  152. sendPropertyChangeMessage (name);
  153. }
  154. else
  155. {
  156. if (properties.contains (name))
  157. undoManager->perform (new SetPropertyAction (this, name, var(), properties [name], false, true));
  158. }
  159. }
  160. void removeAllProperties (UndoManager* const undoManager)
  161. {
  162. if (undoManager == nullptr)
  163. {
  164. while (properties.size() > 0)
  165. {
  166. const Identifier name (properties.getName (properties.size() - 1));
  167. properties.remove (name);
  168. sendPropertyChangeMessage (name);
  169. }
  170. }
  171. else
  172. {
  173. for (int i = properties.size(); --i >= 0;)
  174. undoManager->perform (new SetPropertyAction (this, properties.getName(i), var(),
  175. properties.getValueAt(i), false, true));
  176. }
  177. }
  178. void copyPropertiesFrom (const SharedObject& source, UndoManager* const undoManager)
  179. {
  180. for (int i = properties.size(); --i >= 0;)
  181. if (! source.properties.contains (properties.getName (i)))
  182. removeProperty (properties.getName (i), undoManager);
  183. for (int i = 0; i < source.properties.size(); ++i)
  184. setProperty (source.properties.getName(i), source.properties.getValueAt(i), undoManager);
  185. }
  186. ValueTree getChildWithName (const Identifier typeToMatch) const
  187. {
  188. for (int i = 0; i < children.size(); ++i)
  189. {
  190. SharedObject* const s = children.getObjectPointerUnchecked (i);
  191. if (s->type == typeToMatch)
  192. return ValueTree (s);
  193. }
  194. return ValueTree();
  195. }
  196. ValueTree getOrCreateChildWithName (const Identifier typeToMatch, UndoManager* undoManager)
  197. {
  198. for (int i = 0; i < children.size(); ++i)
  199. {
  200. SharedObject* const s = children.getObjectPointerUnchecked (i);
  201. if (s->type == typeToMatch)
  202. return ValueTree (s);
  203. }
  204. SharedObject* const newObject = new SharedObject (typeToMatch);
  205. addChild (newObject, -1, undoManager);
  206. return ValueTree (newObject);
  207. }
  208. ValueTree getChildWithProperty (const Identifier propertyName, const var& propertyValue) const
  209. {
  210. for (int i = 0; i < children.size(); ++i)
  211. {
  212. SharedObject* const s = children.getObjectPointerUnchecked (i);
  213. if (s->properties[propertyName] == propertyValue)
  214. return ValueTree (s);
  215. }
  216. return ValueTree();
  217. }
  218. bool isAChildOf (const SharedObject* const possibleParent) const noexcept
  219. {
  220. for (const SharedObject* p = parent; p != nullptr; p = p->parent)
  221. if (p == possibleParent)
  222. return true;
  223. return false;
  224. }
  225. int indexOf (const ValueTree& child) const noexcept
  226. {
  227. return children.indexOf (child.object);
  228. }
  229. void addChild (SharedObject* child, int index, UndoManager* const undoManager)
  230. {
  231. if (child != nullptr && child->parent != this)
  232. {
  233. if (child != this && ! isAChildOf (child))
  234. {
  235. // You should always make sure that a child is removed from its previous parent before
  236. // adding it somewhere else - otherwise, it's ambiguous as to whether a different
  237. // undomanager should be used when removing it from its current parent..
  238. jassert (child->parent == nullptr);
  239. if (child->parent != nullptr)
  240. {
  241. jassert (child->parent->children.indexOf (child) >= 0);
  242. child->parent->removeChild (child->parent->children.indexOf (child), undoManager);
  243. }
  244. if (undoManager == nullptr)
  245. {
  246. children.insert (index, child);
  247. child->parent = this;
  248. sendChildAddedMessage (ValueTree (child));
  249. child->sendParentChangeMessage();
  250. }
  251. else
  252. {
  253. if (! isPositiveAndBelow (index, children.size()))
  254. index = children.size();
  255. undoManager->perform (new AddOrRemoveChildAction (this, index, child));
  256. }
  257. }
  258. else
  259. {
  260. // You're attempting to create a recursive loop! A node
  261. // can't be a child of one of its own children!
  262. jassertfalse;
  263. }
  264. }
  265. }
  266. void removeChild (const int childIndex, UndoManager* const undoManager)
  267. {
  268. if (const Ptr child = children.getObjectPointer (childIndex))
  269. {
  270. if (undoManager == nullptr)
  271. {
  272. children.remove (childIndex);
  273. child->parent = nullptr;
  274. sendChildRemovedMessage (ValueTree (child));
  275. child->sendParentChangeMessage();
  276. }
  277. else
  278. {
  279. undoManager->perform (new AddOrRemoveChildAction (this, childIndex, nullptr));
  280. }
  281. }
  282. }
  283. void removeAllChildren (UndoManager* const undoManager)
  284. {
  285. while (children.size() > 0)
  286. removeChild (children.size() - 1, undoManager);
  287. }
  288. void moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
  289. {
  290. // The source index must be a valid index!
  291. jassert (isPositiveAndBelow (currentIndex, children.size()));
  292. if (currentIndex != newIndex
  293. && isPositiveAndBelow (currentIndex, children.size()))
  294. {
  295. if (undoManager == nullptr)
  296. {
  297. children.move (currentIndex, newIndex);
  298. sendChildOrderChangedMessage();
  299. }
  300. else
  301. {
  302. if (! isPositiveAndBelow (newIndex, children.size()))
  303. newIndex = children.size() - 1;
  304. undoManager->perform (new MoveChildAction (this, currentIndex, newIndex));
  305. }
  306. }
  307. }
  308. void reorderChildren (const OwnedArray<ValueTree>& newOrder, UndoManager* undoManager)
  309. {
  310. jassert (newOrder.size() == children.size());
  311. if (undoManager == nullptr)
  312. {
  313. children.clear();
  314. children.ensureStorageAllocated (newOrder.size());
  315. for (int i = 0; i < newOrder.size(); ++i)
  316. children.add (newOrder.getUnchecked(i)->object);
  317. sendChildOrderChangedMessage();
  318. }
  319. else
  320. {
  321. for (int i = 0; i < children.size(); ++i)
  322. {
  323. SharedObject* const child = newOrder.getUnchecked(i)->object;
  324. if (children.getObjectPointerUnchecked (i) != child)
  325. {
  326. const int oldIndex = children.indexOf (child);
  327. jassert (oldIndex >= 0);
  328. moveChild (oldIndex, i, undoManager);
  329. }
  330. }
  331. }
  332. }
  333. bool isEquivalentTo (const SharedObject& other) const
  334. {
  335. if (type != other.type
  336. || properties.size() != other.properties.size()
  337. || children.size() != other.children.size()
  338. || properties != other.properties)
  339. return false;
  340. for (int i = 0; i < children.size(); ++i)
  341. if (! children.getObjectPointerUnchecked(i)->isEquivalentTo (*other.children.getObjectPointerUnchecked(i)))
  342. return false;
  343. return true;
  344. }
  345. XmlElement* createXml() const
  346. {
  347. XmlElement* const xml = new XmlElement (type.toString());
  348. properties.copyToXmlAttributes (*xml);
  349. // (NB: it's faster to add nodes to XML elements in reverse order)
  350. for (int i = children.size(); --i >= 0;)
  351. xml->prependChildElement (children.getObjectPointerUnchecked(i)->createXml());
  352. return xml;
  353. }
  354. void writeToStream (OutputStream& output) const
  355. {
  356. output.writeString (type.toString());
  357. output.writeCompressedInt (properties.size());
  358. for (int j = 0; j < properties.size(); ++j)
  359. {
  360. output.writeString (properties.getName (j).toString());
  361. properties.getValueAt(j).writeToStream (output);
  362. }
  363. output.writeCompressedInt (children.size());
  364. for (int i = 0; i < children.size(); ++i)
  365. writeObjectToStream (output, children.getObjectPointerUnchecked(i));
  366. }
  367. static void writeObjectToStream (OutputStream& output, const SharedObject* const object)
  368. {
  369. if (object != nullptr)
  370. {
  371. object->writeToStream (output);
  372. }
  373. else
  374. {
  375. output.writeString (String());
  376. output.writeCompressedInt (0);
  377. output.writeCompressedInt (0);
  378. }
  379. }
  380. //==============================================================================
  381. class SetPropertyAction : public UndoableAction
  382. {
  383. public:
  384. SetPropertyAction (SharedObject* const so, const Identifier propertyName,
  385. const var& newVal, const var& oldVal, bool isAdding, bool isDeleting)
  386. : target (so), name (propertyName), newValue (newVal), oldValue (oldVal),
  387. isAddingNewProperty (isAdding), isDeletingProperty (isDeleting)
  388. {
  389. }
  390. bool perform()
  391. {
  392. jassert (! (isAddingNewProperty && target->hasProperty (name)));
  393. if (isDeletingProperty)
  394. target->removeProperty (name, nullptr);
  395. else
  396. target->setProperty (name, newValue, nullptr);
  397. return true;
  398. }
  399. bool undo()
  400. {
  401. if (isAddingNewProperty)
  402. target->removeProperty (name, nullptr);
  403. else
  404. target->setProperty (name, oldValue, nullptr);
  405. return true;
  406. }
  407. int getSizeInUnits()
  408. {
  409. return (int) sizeof (*this); //xxx should be more accurate
  410. }
  411. UndoableAction* createCoalescedAction (UndoableAction* nextAction)
  412. {
  413. if (! (isAddingNewProperty || isDeletingProperty))
  414. {
  415. if (SetPropertyAction* const next = dynamic_cast <SetPropertyAction*> (nextAction))
  416. if (next->target == target && next->name == name
  417. && ! (next->isAddingNewProperty || next->isDeletingProperty))
  418. return new SetPropertyAction (target, name, next->newValue, oldValue, false, false);
  419. }
  420. return nullptr;
  421. }
  422. private:
  423. const Ptr target;
  424. const Identifier name;
  425. const var newValue;
  426. var oldValue;
  427. const bool isAddingNewProperty : 1, isDeletingProperty : 1;
  428. JUCE_DECLARE_NON_COPYABLE (SetPropertyAction)
  429. };
  430. //==============================================================================
  431. class AddOrRemoveChildAction : public UndoableAction
  432. {
  433. public:
  434. AddOrRemoveChildAction (SharedObject* parentObject, int index, SharedObject* newChild)
  435. : target (parentObject),
  436. child (newChild != nullptr ? newChild : parentObject->children.getObjectPointer (index)),
  437. childIndex (index),
  438. isDeleting (newChild == nullptr)
  439. {
  440. jassert (child != nullptr);
  441. }
  442. bool perform()
  443. {
  444. if (isDeleting)
  445. target->removeChild (childIndex, nullptr);
  446. else
  447. target->addChild (child, childIndex, nullptr);
  448. return true;
  449. }
  450. bool undo()
  451. {
  452. if (isDeleting)
  453. {
  454. target->addChild (child, childIndex, nullptr);
  455. }
  456. else
  457. {
  458. // If you hit this, it seems that your object's state is getting confused - probably
  459. // because you've interleaved some undoable and non-undoable operations?
  460. jassert (childIndex < target->children.size());
  461. target->removeChild (childIndex, nullptr);
  462. }
  463. return true;
  464. }
  465. int getSizeInUnits()
  466. {
  467. return (int) sizeof (*this); //xxx should be more accurate
  468. }
  469. private:
  470. const Ptr target, child;
  471. const int childIndex;
  472. const bool isDeleting;
  473. JUCE_DECLARE_NON_COPYABLE (AddOrRemoveChildAction)
  474. };
  475. //==============================================================================
  476. class MoveChildAction : public UndoableAction
  477. {
  478. public:
  479. MoveChildAction (SharedObject* parentObject, int fromIndex, int toIndex) noexcept
  480. : parent (parentObject), startIndex (fromIndex), endIndex (toIndex)
  481. {
  482. }
  483. bool perform()
  484. {
  485. parent->moveChild (startIndex, endIndex, nullptr);
  486. return true;
  487. }
  488. bool undo()
  489. {
  490. parent->moveChild (endIndex, startIndex, nullptr);
  491. return true;
  492. }
  493. int getSizeInUnits()
  494. {
  495. return (int) sizeof (*this); //xxx should be more accurate
  496. }
  497. UndoableAction* createCoalescedAction (UndoableAction* nextAction)
  498. {
  499. if (MoveChildAction* next = dynamic_cast <MoveChildAction*> (nextAction))
  500. if (next->parent == parent && next->startIndex == endIndex)
  501. return new MoveChildAction (parent, startIndex, next->endIndex);
  502. return nullptr;
  503. }
  504. private:
  505. const Ptr parent;
  506. const int startIndex, endIndex;
  507. JUCE_DECLARE_NON_COPYABLE (MoveChildAction)
  508. };
  509. //==============================================================================
  510. const Identifier type;
  511. NamedValueSet properties;
  512. ReferenceCountedArray<SharedObject> children;
  513. SortedSet<ValueTree*> valueTreesWithListeners;
  514. SharedObject* parent;
  515. private:
  516. SharedObject& operator= (const SharedObject&);
  517. JUCE_LEAK_DETECTOR (SharedObject)
  518. };
  519. //==============================================================================
  520. ValueTree::ValueTree() noexcept
  521. {
  522. }
  523. const ValueTree ValueTree::invalid;
  524. ValueTree::ValueTree (Identifier type) : object (new ValueTree::SharedObject (type))
  525. {
  526. jassert (type.toString().isNotEmpty()); // All objects must be given a sensible type name!
  527. }
  528. ValueTree::ValueTree (SharedObject* so) : object (so)
  529. {
  530. }
  531. ValueTree::ValueTree (const ValueTree& other) : object (other.object)
  532. {
  533. }
  534. ValueTree& ValueTree::operator= (const ValueTree& other)
  535. {
  536. if (object != other.object)
  537. {
  538. if (listeners.isEmpty())
  539. {
  540. object = other.object;
  541. }
  542. else
  543. {
  544. if (object != nullptr)
  545. object->valueTreesWithListeners.removeValue (this);
  546. if (other.object != nullptr)
  547. other.object->valueTreesWithListeners.add (this);
  548. object = other.object;
  549. listeners.call (&ValueTree::Listener::valueTreeRedirected, *this);
  550. }
  551. }
  552. return *this;
  553. }
  554. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  555. ValueTree::ValueTree (ValueTree&& other) noexcept
  556. : object (static_cast<SharedObject::Ptr&&> (other.object))
  557. {
  558. }
  559. #endif
  560. ValueTree::~ValueTree()
  561. {
  562. if (listeners.size() > 0 && object != nullptr)
  563. object->valueTreesWithListeners.removeValue (this);
  564. }
  565. bool ValueTree::operator== (const ValueTree& other) const noexcept
  566. {
  567. return object == other.object;
  568. }
  569. bool ValueTree::operator!= (const ValueTree& other) const noexcept
  570. {
  571. return object != other.object;
  572. }
  573. bool ValueTree::isEquivalentTo (const ValueTree& other) const
  574. {
  575. return object == other.object
  576. || (object != nullptr && other.object != nullptr
  577. && object->isEquivalentTo (*other.object));
  578. }
  579. ValueTree ValueTree::createCopy() const
  580. {
  581. return ValueTree (createCopyIfNotNull (object.get()));
  582. }
  583. bool ValueTree::hasType (const Identifier typeName) const
  584. {
  585. return object != nullptr && object->type == typeName;
  586. }
  587. Identifier ValueTree::getType() const
  588. {
  589. return object != nullptr ? object->type : Identifier();
  590. }
  591. ValueTree ValueTree::getParent() const
  592. {
  593. return ValueTree (object != nullptr ? object->parent
  594. : static_cast <SharedObject*> (nullptr));
  595. }
  596. ValueTree ValueTree::getSibling (const int delta) const
  597. {
  598. if (object == nullptr || object->parent == nullptr)
  599. return invalid;
  600. const int index = object->parent->indexOf (*this) + delta;
  601. return ValueTree (object->parent->children.getObjectPointer (index));
  602. }
  603. const var& ValueTree::operator[] (const Identifier name) const
  604. {
  605. return object == nullptr ? var::null : object->properties[name];
  606. }
  607. const var& ValueTree::getProperty (const Identifier name) const
  608. {
  609. return object == nullptr ? var::null : object->properties[name];
  610. }
  611. var ValueTree::getProperty (const Identifier name, const var& defaultReturnValue) const
  612. {
  613. return object == nullptr ? defaultReturnValue
  614. : object->properties.getWithDefault (name, defaultReturnValue);
  615. }
  616. ValueTree& ValueTree::setProperty (const Identifier name, const var& newValue,
  617. UndoManager* const undoManager)
  618. {
  619. jassert (name.toString().isNotEmpty()); // Must have a valid property name!
  620. jassert (object != nullptr); // Trying to add a property to a null ValueTree will fail!
  621. if (object != nullptr)
  622. object->setProperty (name, newValue, undoManager);
  623. return *this;
  624. }
  625. bool ValueTree::hasProperty (const Identifier name) const
  626. {
  627. return object != nullptr && object->hasProperty (name);
  628. }
  629. void ValueTree::removeProperty (const Identifier name, UndoManager* const undoManager)
  630. {
  631. if (object != nullptr)
  632. object->removeProperty (name, undoManager);
  633. }
  634. void ValueTree::removeAllProperties (UndoManager* const undoManager)
  635. {
  636. if (object != nullptr)
  637. object->removeAllProperties (undoManager);
  638. }
  639. int ValueTree::getNumProperties() const
  640. {
  641. return object == nullptr ? 0 : object->properties.size();
  642. }
  643. Identifier ValueTree::getPropertyName (const int index) const
  644. {
  645. return object == nullptr ? Identifier()
  646. : object->properties.getName (index);
  647. }
  648. void ValueTree::copyPropertiesFrom (const ValueTree& source, UndoManager* const undoManager)
  649. {
  650. jassert (object != nullptr || source.object == nullptr); // Trying to add properties to a null ValueTree will fail!
  651. if (source.object == nullptr)
  652. removeAllProperties (undoManager);
  653. else if (object != nullptr)
  654. object->copyPropertiesFrom (*(source.object), undoManager);
  655. }
  656. //==============================================================================
  657. class ValueTreePropertyValueSource : public Value::ValueSource,
  658. private ValueTree::Listener
  659. {
  660. public:
  661. ValueTreePropertyValueSource (const ValueTree& vt, const Identifier prop, UndoManager* um)
  662. : tree (vt), property (prop), undoManager (um)
  663. {
  664. tree.addListener (this);
  665. }
  666. ~ValueTreePropertyValueSource()
  667. {
  668. tree.removeListener (this);
  669. }
  670. var getValue() const { return tree [property]; }
  671. void setValue (const var& newValue) { tree.setProperty (property, newValue, undoManager); }
  672. private:
  673. ValueTree tree;
  674. const Identifier property;
  675. UndoManager* const undoManager;
  676. void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override
  677. {
  678. if (tree == changedTree && property == changedProperty)
  679. sendChangeMessage (false);
  680. }
  681. void valueTreeChildAdded (ValueTree&, ValueTree&) override {}
  682. void valueTreeChildRemoved (ValueTree&, ValueTree&) override {}
  683. void valueTreeChildOrderChanged (ValueTree&) override {}
  684. void valueTreeParentChanged (ValueTree&) override {}
  685. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource)
  686. };
  687. Value ValueTree::getPropertyAsValue (const Identifier name, UndoManager* const undoManager)
  688. {
  689. return Value (new ValueTreePropertyValueSource (*this, name, undoManager));
  690. }
  691. //==============================================================================
  692. int ValueTree::getNumChildren() const
  693. {
  694. return object == nullptr ? 0 : object->children.size();
  695. }
  696. ValueTree ValueTree::getChild (int index) const
  697. {
  698. return ValueTree (object != nullptr ? object->children.getObjectPointer (index)
  699. : static_cast <SharedObject*> (nullptr));
  700. }
  701. ValueTree ValueTree::getChildWithName (const Identifier type) const
  702. {
  703. return object != nullptr ? object->getChildWithName (type) : ValueTree();
  704. }
  705. ValueTree ValueTree::getOrCreateChildWithName (const Identifier type, UndoManager* undoManager)
  706. {
  707. return object != nullptr ? object->getOrCreateChildWithName (type, undoManager) : ValueTree();
  708. }
  709. ValueTree ValueTree::getChildWithProperty (const Identifier propertyName, const var& propertyValue) const
  710. {
  711. return object != nullptr ? object->getChildWithProperty (propertyName, propertyValue) : ValueTree();
  712. }
  713. bool ValueTree::isAChildOf (const ValueTree& possibleParent) const
  714. {
  715. return object != nullptr && object->isAChildOf (possibleParent.object);
  716. }
  717. int ValueTree::indexOf (const ValueTree& child) const
  718. {
  719. return object != nullptr ? object->indexOf (child) : -1;
  720. }
  721. void ValueTree::addChild (const ValueTree& child, int index, UndoManager* const undoManager)
  722. {
  723. jassert (object != nullptr); // Trying to add a child to a null ValueTree!
  724. if (object != nullptr)
  725. object->addChild (child.object, index, undoManager);
  726. }
  727. void ValueTree::removeChild (const int childIndex, UndoManager* const undoManager)
  728. {
  729. if (object != nullptr)
  730. object->removeChild (childIndex, undoManager);
  731. }
  732. void ValueTree::removeChild (const ValueTree& child, UndoManager* const undoManager)
  733. {
  734. if (object != nullptr)
  735. object->removeChild (object->children.indexOf (child.object), undoManager);
  736. }
  737. void ValueTree::removeAllChildren (UndoManager* const undoManager)
  738. {
  739. if (object != nullptr)
  740. object->removeAllChildren (undoManager);
  741. }
  742. void ValueTree::moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
  743. {
  744. if (object != nullptr)
  745. object->moveChild (currentIndex, newIndex, undoManager);
  746. }
  747. //==============================================================================
  748. void ValueTree::createListOfChildren (OwnedArray<ValueTree>& list) const
  749. {
  750. jassert (object != nullptr);
  751. for (int i = 0; i < object->children.size(); ++i)
  752. list.add (new ValueTree (object->children.getObjectPointerUnchecked(i)));
  753. }
  754. void ValueTree::reorderChildren (const OwnedArray<ValueTree>& newOrder, UndoManager* undoManager)
  755. {
  756. jassert (object != nullptr);
  757. object->reorderChildren (newOrder, undoManager);
  758. }
  759. //==============================================================================
  760. void ValueTree::addListener (Listener* listener)
  761. {
  762. if (listener != nullptr)
  763. {
  764. if (listeners.isEmpty() && object != nullptr)
  765. object->valueTreesWithListeners.add (this);
  766. listeners.add (listener);
  767. }
  768. }
  769. void ValueTree::removeListener (Listener* listener)
  770. {
  771. listeners.remove (listener);
  772. if (listeners.isEmpty() && object != nullptr)
  773. object->valueTreesWithListeners.removeValue (this);
  774. }
  775. void ValueTree::sendPropertyChangeMessage (const Identifier property)
  776. {
  777. if (object != nullptr)
  778. object->sendPropertyChangeMessage (property);
  779. }
  780. //==============================================================================
  781. XmlElement* ValueTree::createXml() const
  782. {
  783. return object != nullptr ? object->createXml() : nullptr;
  784. }
  785. ValueTree ValueTree::fromXml (const XmlElement& xml)
  786. {
  787. ValueTree v (xml.getTagName());
  788. v.object->properties.setFromXmlAttributes (xml);
  789. forEachXmlChildElement (xml, e)
  790. v.addChild (fromXml (*e), -1, nullptr);
  791. return v;
  792. }
  793. String ValueTree::toXmlString() const
  794. {
  795. const ScopedPointer<XmlElement> xml (createXml());
  796. return xml != nullptr ? xml->createDocument ("") : String();
  797. }
  798. //==============================================================================
  799. void ValueTree::writeToStream (OutputStream& output) const
  800. {
  801. SharedObject::writeObjectToStream (output, object);
  802. }
  803. ValueTree ValueTree::readFromStream (InputStream& input)
  804. {
  805. const String type (input.readString());
  806. if (type.isEmpty())
  807. return ValueTree();
  808. ValueTree v (type);
  809. const int numProps = input.readCompressedInt();
  810. if (numProps < 0)
  811. {
  812. jassertfalse; // trying to read corrupted data!
  813. return v;
  814. }
  815. for (int i = 0; i < numProps; ++i)
  816. {
  817. const String name (input.readString());
  818. jassert (name.isNotEmpty());
  819. const var value (var::readFromStream (input));
  820. v.object->properties.set (name, value);
  821. }
  822. const int numChildren = input.readCompressedInt();
  823. v.object->children.ensureStorageAllocated (numChildren);
  824. for (int i = 0; i < numChildren; ++i)
  825. {
  826. ValueTree child (readFromStream (input));
  827. v.object->children.add (child.object);
  828. child.object->parent = v.object;
  829. }
  830. return v;
  831. }
  832. ValueTree ValueTree::readFromData (const void* const data, const size_t numBytes)
  833. {
  834. MemoryInputStream in (data, numBytes, false);
  835. return readFromStream (in);
  836. }
  837. ValueTree ValueTree::readFromGZIPData (const void* const data, const size_t numBytes)
  838. {
  839. MemoryInputStream in (data, numBytes, false);
  840. GZIPDecompressorInputStream gzipStream (in);
  841. return readFromStream (gzipStream);
  842. }
  843. void ValueTree::Listener::valueTreeRedirected (ValueTree&) {}
  844. //==============================================================================
  845. #if JUCE_UNIT_TESTS
  846. class ValueTreeTests : public UnitTest
  847. {
  848. public:
  849. ValueTreeTests() : UnitTest ("ValueTrees") {}
  850. static String createRandomIdentifier (Random& r)
  851. {
  852. char buffer[50] = { 0 };
  853. const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:";
  854. for (int i = 1 + r.nextInt (numElementsInArray (buffer) - 2); --i >= 0;)
  855. buffer[i] = chars [r.nextInt (sizeof (chars) - 1)];
  856. return CharPointer_ASCII (buffer);
  857. }
  858. static String createRandomWideCharString (Random& r)
  859. {
  860. juce_wchar buffer[50] = { 0 };
  861. for (int i = r.nextInt (numElementsInArray (buffer) - 1); --i >= 0;)
  862. {
  863. if (r.nextBool())
  864. {
  865. do
  866. {
  867. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  868. }
  869. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  870. }
  871. else
  872. buffer[i] = (juce_wchar) (1 + r.nextInt (0x7e));
  873. }
  874. return CharPointer_UTF32 (buffer);
  875. }
  876. static ValueTree createRandomTree (UndoManager* undoManager, int depth, Random& r)
  877. {
  878. ValueTree v (createRandomIdentifier (r));
  879. for (int i = r.nextInt (10); --i >= 0;)
  880. {
  881. switch (r.nextInt (5))
  882. {
  883. case 0: v.setProperty (createRandomIdentifier (r), createRandomWideCharString (r), undoManager); break;
  884. case 1: v.setProperty (createRandomIdentifier (r), r.nextInt(), undoManager); break;
  885. case 2: if (depth < 5) v.addChild (createRandomTree (undoManager, depth + 1, r), r.nextInt (v.getNumChildren() + 1), undoManager); break;
  886. case 3: v.setProperty (createRandomIdentifier (r), r.nextBool(), undoManager); break;
  887. case 4: v.setProperty (createRandomIdentifier (r), r.nextDouble(), undoManager); break;
  888. default: break;
  889. }
  890. }
  891. return v;
  892. }
  893. void runTest()
  894. {
  895. beginTest ("ValueTree");
  896. Random r = getRandom();
  897. for (int i = 10; --i >= 0;)
  898. {
  899. MemoryOutputStream mo;
  900. ValueTree v1 (createRandomTree (nullptr, 0, r));
  901. v1.writeToStream (mo);
  902. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  903. ValueTree v2 = ValueTree::readFromStream (mi);
  904. expect (v1.isEquivalentTo (v2));
  905. ScopedPointer <XmlElement> xml1 (v1.createXml());
  906. ScopedPointer <XmlElement> xml2 (v2.createCopy().createXml());
  907. expect (xml1->isEquivalentTo (xml2, false));
  908. ValueTree v4 = v2.createCopy();
  909. expect (v1.isEquivalentTo (v4));
  910. }
  911. }
  912. };
  913. static ValueTreeTests valueTreeTests;
  914. #endif