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.

1128 lines
36KB

  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 == 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
  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()
  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()
  402. {
  403. if (isAddingNewProperty)
  404. target->removeProperty (name, nullptr);
  405. else
  406. target->setProperty (name, oldValue, nullptr);
  407. return true;
  408. }
  409. int getSizeInUnits()
  410. {
  411. return (int) sizeof (*this); //xxx should be more accurate
  412. }
  413. UndoableAction* createCoalescedAction (UndoableAction* nextAction)
  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()
  445. {
  446. if (isDeleting)
  447. target->removeChild (childIndex, nullptr);
  448. else
  449. target->addChild (child, childIndex, nullptr);
  450. return true;
  451. }
  452. bool undo()
  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()
  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()
  486. {
  487. parent->moveChild (startIndex, endIndex, nullptr);
  488. return true;
  489. }
  490. bool undo()
  491. {
  492. parent->moveChild (endIndex, startIndex, nullptr);
  493. return true;
  494. }
  495. int getSizeInUnits()
  496. {
  497. return (int) sizeof (*this); //xxx should be more accurate
  498. }
  499. UndoableAction* createCoalescedAction (UndoableAction* nextAction)
  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 (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) : object (so)
  531. {
  532. }
  533. ValueTree::ValueTree (const ValueTree& other) : 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
  586. {
  587. return object != nullptr && object->type == typeName;
  588. }
  589. Identifier ValueTree::getType() const
  590. {
  591. return object != nullptr ? object->type : Identifier();
  592. }
  593. ValueTree ValueTree::getParent() const
  594. {
  595. return ValueTree (object != nullptr ? object->parent
  596. : static_cast<SharedObject*> (nullptr));
  597. }
  598. ValueTree ValueTree::getSibling (const int delta) const
  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
  606. {
  607. return object == nullptr ? var::null : object->properties[name];
  608. }
  609. const var& ValueTree::getProperty (const Identifier name) const
  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,
  619. UndoManager* const undoManager)
  620. {
  621. jassert (name.toString().isNotEmpty()); // Must have a valid property name!
  622. jassert (object != nullptr); // Trying to add a property to a null ValueTree will fail!
  623. if (object != nullptr)
  624. object->setProperty (name, newValue, undoManager);
  625. return *this;
  626. }
  627. bool ValueTree::hasProperty (const Identifier name) const
  628. {
  629. return object != nullptr && object->hasProperty (name);
  630. }
  631. void ValueTree::removeProperty (const Identifier name, UndoManager* const undoManager)
  632. {
  633. if (object != nullptr)
  634. object->removeProperty (name, undoManager);
  635. }
  636. void ValueTree::removeAllProperties (UndoManager* const undoManager)
  637. {
  638. if (object != nullptr)
  639. object->removeAllProperties (undoManager);
  640. }
  641. int ValueTree::getNumProperties() const
  642. {
  643. return object == nullptr ? 0 : object->properties.size();
  644. }
  645. Identifier ValueTree::getPropertyName (const int index) const
  646. {
  647. return object == nullptr ? Identifier()
  648. : object->properties.getName (index);
  649. }
  650. void ValueTree::copyPropertiesFrom (const ValueTree& source, UndoManager* const undoManager)
  651. {
  652. jassert (object != nullptr || source.object == nullptr); // Trying to add properties to a null ValueTree will fail!
  653. if (source.object == nullptr)
  654. removeAllProperties (undoManager);
  655. else if (object != nullptr)
  656. object->copyPropertiesFrom (*(source.object), undoManager);
  657. }
  658. int ValueTree::getReferenceCount() const noexcept
  659. {
  660. return object != nullptr ? object->getReferenceCount() : 0;
  661. }
  662. //==============================================================================
  663. class ValueTreePropertyValueSource : public Value::ValueSource,
  664. private ValueTree::Listener
  665. {
  666. public:
  667. ValueTreePropertyValueSource (const ValueTree& vt, const Identifier prop, UndoManager* um)
  668. : tree (vt), property (prop), undoManager (um)
  669. {
  670. tree.addListener (this);
  671. }
  672. ~ValueTreePropertyValueSource()
  673. {
  674. tree.removeListener (this);
  675. }
  676. var getValue() const { return tree [property]; }
  677. void setValue (const var& newValue) { tree.setProperty (property, newValue, undoManager); }
  678. private:
  679. ValueTree tree;
  680. const Identifier property;
  681. UndoManager* const undoManager;
  682. void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override
  683. {
  684. if (tree == changedTree && property == changedProperty)
  685. sendChangeMessage (false);
  686. }
  687. void valueTreeChildAdded (ValueTree&, ValueTree&) override {}
  688. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override {}
  689. void valueTreeChildOrderChanged (ValueTree&, int, int) override {}
  690. void valueTreeParentChanged (ValueTree&) override {}
  691. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource)
  692. };
  693. Value ValueTree::getPropertyAsValue (const Identifier name, UndoManager* const undoManager)
  694. {
  695. return Value (new ValueTreePropertyValueSource (*this, name, undoManager));
  696. }
  697. //==============================================================================
  698. int ValueTree::getNumChildren() const
  699. {
  700. return object == nullptr ? 0 : object->children.size();
  701. }
  702. ValueTree ValueTree::getChild (int index) const
  703. {
  704. return ValueTree (object != nullptr ? object->children.getObjectPointer (index)
  705. : static_cast<SharedObject*> (nullptr));
  706. }
  707. ValueTree ValueTree::getChildWithName (const Identifier type) const
  708. {
  709. return object != nullptr ? object->getChildWithName (type) : ValueTree();
  710. }
  711. ValueTree ValueTree::getOrCreateChildWithName (const Identifier type, UndoManager* undoManager)
  712. {
  713. return object != nullptr ? object->getOrCreateChildWithName (type, undoManager) : ValueTree();
  714. }
  715. ValueTree ValueTree::getChildWithProperty (const Identifier propertyName, const var& propertyValue) const
  716. {
  717. return object != nullptr ? object->getChildWithProperty (propertyName, propertyValue) : ValueTree();
  718. }
  719. bool ValueTree::isAChildOf (const ValueTree& possibleParent) const
  720. {
  721. return object != nullptr && object->isAChildOf (possibleParent.object);
  722. }
  723. int ValueTree::indexOf (const ValueTree& child) const
  724. {
  725. return object != nullptr ? object->indexOf (child) : -1;
  726. }
  727. void ValueTree::addChild (const ValueTree& child, int index, UndoManager* const undoManager)
  728. {
  729. jassert (object != nullptr); // Trying to add a child to a null ValueTree!
  730. if (object != nullptr)
  731. object->addChild (child.object, index, undoManager);
  732. }
  733. void ValueTree::removeChild (const int childIndex, UndoManager* const undoManager)
  734. {
  735. if (object != nullptr)
  736. object->removeChild (childIndex, undoManager);
  737. }
  738. void ValueTree::removeChild (const ValueTree& child, UndoManager* const undoManager)
  739. {
  740. if (object != nullptr)
  741. object->removeChild (object->children.indexOf (child.object), undoManager);
  742. }
  743. void ValueTree::removeAllChildren (UndoManager* const undoManager)
  744. {
  745. if (object != nullptr)
  746. object->removeAllChildren (undoManager);
  747. }
  748. void ValueTree::moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
  749. {
  750. if (object != nullptr)
  751. object->moveChild (currentIndex, newIndex, undoManager);
  752. }
  753. //==============================================================================
  754. void ValueTree::createListOfChildren (OwnedArray<ValueTree>& list) const
  755. {
  756. jassert (object != nullptr);
  757. for (int i = 0; i < object->children.size(); ++i)
  758. list.add (new ValueTree (object->children.getObjectPointerUnchecked(i)));
  759. }
  760. void ValueTree::reorderChildren (const OwnedArray<ValueTree>& newOrder, UndoManager* undoManager)
  761. {
  762. jassert (object != nullptr);
  763. object->reorderChildren (newOrder, undoManager);
  764. }
  765. //==============================================================================
  766. void ValueTree::addListener (Listener* listener)
  767. {
  768. if (listener != nullptr)
  769. {
  770. if (listeners.isEmpty() && object != nullptr)
  771. object->valueTreesWithListeners.add (this);
  772. listeners.add (listener);
  773. }
  774. }
  775. void ValueTree::removeListener (Listener* listener)
  776. {
  777. listeners.remove (listener);
  778. if (listeners.isEmpty() && object != nullptr)
  779. object->valueTreesWithListeners.removeValue (this);
  780. }
  781. void ValueTree::sendPropertyChangeMessage (const Identifier property)
  782. {
  783. if (object != nullptr)
  784. object->sendPropertyChangeMessage (property);
  785. }
  786. //==============================================================================
  787. XmlElement* ValueTree::createXml() const
  788. {
  789. return object != nullptr ? object->createXml() : nullptr;
  790. }
  791. ValueTree ValueTree::fromXml (const XmlElement& xml)
  792. {
  793. // ValueTrees don't have any equivalent to XML text elements!
  794. jassert (! xml.isTextElement());
  795. ValueTree v (xml.getTagName());
  796. v.object->properties.setFromXmlAttributes (xml);
  797. forEachXmlChildElement (xml, e)
  798. v.addChild (fromXml (*e), -1, nullptr);
  799. return v;
  800. }
  801. String ValueTree::toXmlString() const
  802. {
  803. const ScopedPointer<XmlElement> xml (createXml());
  804. return xml != nullptr ? xml->createDocument ("") : String();
  805. }
  806. //==============================================================================
  807. void ValueTree::writeToStream (OutputStream& output) const
  808. {
  809. SharedObject::writeObjectToStream (output, object);
  810. }
  811. ValueTree ValueTree::readFromStream (InputStream& input)
  812. {
  813. const String type (input.readString());
  814. if (type.isEmpty())
  815. return ValueTree();
  816. ValueTree v (type);
  817. const int numProps = input.readCompressedInt();
  818. if (numProps < 0)
  819. {
  820. jassertfalse; // trying to read corrupted data!
  821. return v;
  822. }
  823. for (int i = 0; i < numProps; ++i)
  824. {
  825. const String name (input.readString());
  826. jassert (name.isNotEmpty());
  827. const var value (var::readFromStream (input));
  828. v.object->properties.set (name, value);
  829. }
  830. const int numChildren = input.readCompressedInt();
  831. v.object->children.ensureStorageAllocated (numChildren);
  832. for (int i = 0; i < numChildren; ++i)
  833. {
  834. ValueTree child (readFromStream (input));
  835. v.object->children.add (child.object);
  836. child.object->parent = v.object;
  837. }
  838. return v;
  839. }
  840. ValueTree ValueTree::readFromData (const void* const data, const size_t numBytes)
  841. {
  842. MemoryInputStream in (data, numBytes, false);
  843. return readFromStream (in);
  844. }
  845. ValueTree ValueTree::readFromGZIPData (const void* const data, const size_t numBytes)
  846. {
  847. MemoryInputStream in (data, numBytes, false);
  848. GZIPDecompressorInputStream gzipStream (in);
  849. return readFromStream (gzipStream);
  850. }
  851. void ValueTree::Listener::valueTreeRedirected (ValueTree&) {}
  852. //==============================================================================
  853. #if JUCE_UNIT_TESTS
  854. class ValueTreeTests : public UnitTest
  855. {
  856. public:
  857. ValueTreeTests() : UnitTest ("ValueTrees") {}
  858. static String createRandomIdentifier (Random& r)
  859. {
  860. char buffer[50] = { 0 };
  861. const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:";
  862. for (int i = 1 + r.nextInt (numElementsInArray (buffer) - 2); --i >= 0;)
  863. buffer[i] = chars [r.nextInt (sizeof (chars) - 1)];
  864. return CharPointer_ASCII (buffer);
  865. }
  866. static String createRandomWideCharString (Random& r)
  867. {
  868. juce_wchar buffer[50] = { 0 };
  869. for (int i = r.nextInt (numElementsInArray (buffer) - 1); --i >= 0;)
  870. {
  871. if (r.nextBool())
  872. {
  873. do
  874. {
  875. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  876. }
  877. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  878. }
  879. else
  880. buffer[i] = (juce_wchar) (1 + r.nextInt (0x7e));
  881. }
  882. return CharPointer_UTF32 (buffer);
  883. }
  884. static ValueTree createRandomTree (UndoManager* undoManager, int depth, Random& r)
  885. {
  886. ValueTree v (createRandomIdentifier (r));
  887. for (int i = r.nextInt (10); --i >= 0;)
  888. {
  889. switch (r.nextInt (5))
  890. {
  891. case 0: v.setProperty (createRandomIdentifier (r), createRandomWideCharString (r), undoManager); break;
  892. case 1: v.setProperty (createRandomIdentifier (r), r.nextInt(), undoManager); break;
  893. case 2: if (depth < 5) v.addChild (createRandomTree (undoManager, depth + 1, r), r.nextInt (v.getNumChildren() + 1), undoManager); break;
  894. case 3: v.setProperty (createRandomIdentifier (r), r.nextBool(), undoManager); break;
  895. case 4: v.setProperty (createRandomIdentifier (r), r.nextDouble(), undoManager); break;
  896. default: break;
  897. }
  898. }
  899. return v;
  900. }
  901. void runTest()
  902. {
  903. beginTest ("ValueTree");
  904. Random r = getRandom();
  905. for (int i = 10; --i >= 0;)
  906. {
  907. MemoryOutputStream mo;
  908. ValueTree v1 (createRandomTree (nullptr, 0, r));
  909. v1.writeToStream (mo);
  910. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  911. ValueTree v2 = ValueTree::readFromStream (mi);
  912. expect (v1.isEquivalentTo (v2));
  913. ScopedPointer<XmlElement> xml1 (v1.createXml());
  914. ScopedPointer<XmlElement> xml2 (v2.createCopy().createXml());
  915. expect (xml1->isEquivalentTo (xml2, false));
  916. ValueTree v4 = v2.createCopy();
  917. expect (v1.isEquivalentTo (v4));
  918. }
  919. }
  920. };
  921. static ValueTreeTests valueTreeTests;
  922. #endif