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.

1146 lines
36KB

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