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.

1187 lines
37KB

  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. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  526. const ValueTree ValueTree::invalid;
  527. #endif
  528. ValueTree::ValueTree (const Identifier& type) : object (new ValueTree::SharedObject (type))
  529. {
  530. jassert (type.toString().isNotEmpty()); // All objects must be given a sensible type name!
  531. }
  532. ValueTree::ValueTree (SharedObject* so) noexcept : object (so)
  533. {
  534. }
  535. ValueTree::ValueTree (const ValueTree& other) noexcept : object (other.object)
  536. {
  537. }
  538. ValueTree& ValueTree::operator= (const ValueTree& other)
  539. {
  540. if (object != other.object)
  541. {
  542. if (listeners.isEmpty())
  543. {
  544. object = other.object;
  545. }
  546. else
  547. {
  548. if (object != nullptr)
  549. object->valueTreesWithListeners.removeValue (this);
  550. if (other.object != nullptr)
  551. other.object->valueTreesWithListeners.add (this);
  552. object = other.object;
  553. listeners.call (&ValueTree::Listener::valueTreeRedirected, *this);
  554. }
  555. }
  556. return *this;
  557. }
  558. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  559. ValueTree::ValueTree (ValueTree&& other) noexcept
  560. : object (static_cast<SharedObject::Ptr&&> (other.object))
  561. {
  562. }
  563. #endif
  564. ValueTree::~ValueTree()
  565. {
  566. if (listeners.size() > 0 && object != nullptr)
  567. object->valueTreesWithListeners.removeValue (this);
  568. }
  569. bool ValueTree::operator== (const ValueTree& other) const noexcept
  570. {
  571. return object == other.object;
  572. }
  573. bool ValueTree::operator!= (const ValueTree& other) const noexcept
  574. {
  575. return object != other.object;
  576. }
  577. bool ValueTree::isEquivalentTo (const ValueTree& other) const
  578. {
  579. return object == other.object
  580. || (object != nullptr && other.object != nullptr
  581. && object->isEquivalentTo (*other.object));
  582. }
  583. ValueTree ValueTree::createCopy() const
  584. {
  585. return ValueTree (createCopyIfNotNull (object.get()));
  586. }
  587. bool ValueTree::hasType (const Identifier& typeName) const noexcept
  588. {
  589. return object != nullptr && object->type == typeName;
  590. }
  591. Identifier ValueTree::getType() const noexcept
  592. {
  593. return object != nullptr ? object->type : Identifier();
  594. }
  595. ValueTree ValueTree::getParent() const noexcept
  596. {
  597. return ValueTree (object != nullptr ? object->parent
  598. : static_cast<SharedObject*> (nullptr));
  599. }
  600. ValueTree ValueTree::getSibling (const int delta) const noexcept
  601. {
  602. if (object == nullptr || object->parent == nullptr)
  603. return ValueTree();
  604. const int index = object->parent->indexOf (*this) + delta;
  605. return ValueTree (object->parent->children.getObjectPointer (index));
  606. }
  607. static const var& getNullVarRef() noexcept
  608. {
  609. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  610. return var::null;
  611. #else
  612. static var nullVar;
  613. return nullVar;
  614. #endif
  615. }
  616. const var& ValueTree::operator[] (const Identifier& name) const noexcept
  617. {
  618. return object == nullptr ? getNullVarRef() : object->properties[name];
  619. }
  620. const var& ValueTree::getProperty (const Identifier& name) const noexcept
  621. {
  622. return object == nullptr ? getNullVarRef() : object->properties[name];
  623. }
  624. var ValueTree::getProperty (const Identifier& name, const var& defaultReturnValue) const
  625. {
  626. return object == nullptr ? defaultReturnValue
  627. : object->properties.getWithDefault (name, defaultReturnValue);
  628. }
  629. const var* ValueTree::getPropertyPointer (const Identifier& name) const noexcept
  630. {
  631. return object == nullptr ? nullptr
  632. : object->properties.getVarPointer (name);
  633. }
  634. ValueTree& ValueTree::setProperty (const Identifier& name, const var& newValue, UndoManager* undoManager)
  635. {
  636. jassert (name.toString().isNotEmpty()); // Must have a valid property name!
  637. jassert (object != nullptr); // Trying to add a property to a null ValueTree will fail!
  638. if (object != nullptr)
  639. object->setProperty (name, newValue, undoManager);
  640. return *this;
  641. }
  642. bool ValueTree::hasProperty (const Identifier& name) const noexcept
  643. {
  644. return object != nullptr && object->hasProperty (name);
  645. }
  646. void ValueTree::removeProperty (const Identifier& name, UndoManager* const undoManager)
  647. {
  648. if (object != nullptr)
  649. object->removeProperty (name, undoManager);
  650. }
  651. void ValueTree::removeAllProperties (UndoManager* const undoManager)
  652. {
  653. if (object != nullptr)
  654. object->removeAllProperties (undoManager);
  655. }
  656. int ValueTree::getNumProperties() const noexcept
  657. {
  658. return object == nullptr ? 0 : object->properties.size();
  659. }
  660. Identifier ValueTree::getPropertyName (const int index) const noexcept
  661. {
  662. return object == nullptr ? Identifier()
  663. : object->properties.getName (index);
  664. }
  665. void ValueTree::copyPropertiesFrom (const ValueTree& source, UndoManager* const undoManager)
  666. {
  667. jassert (object != nullptr || source.object == nullptr); // Trying to add properties to a null ValueTree will fail!
  668. if (source.object == nullptr)
  669. removeAllProperties (undoManager);
  670. else if (object != nullptr)
  671. object->copyPropertiesFrom (*(source.object), undoManager);
  672. }
  673. int ValueTree::getReferenceCount() const noexcept
  674. {
  675. return object != nullptr ? object->getReferenceCount() : 0;
  676. }
  677. //==============================================================================
  678. class ValueTreePropertyValueSource : public Value::ValueSource,
  679. private ValueTree::Listener
  680. {
  681. public:
  682. ValueTreePropertyValueSource (const ValueTree& vt, const Identifier& prop, UndoManager* um)
  683. : tree (vt), property (prop), undoManager (um)
  684. {
  685. tree.addListener (this);
  686. }
  687. ~ValueTreePropertyValueSource()
  688. {
  689. tree.removeListener (this);
  690. }
  691. var getValue() const override { return tree [property]; }
  692. void setValue (const var& newValue) override { tree.setProperty (property, newValue, undoManager); }
  693. private:
  694. ValueTree tree;
  695. const Identifier property;
  696. UndoManager* const undoManager;
  697. void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override
  698. {
  699. if (tree == changedTree && property == changedProperty)
  700. sendChangeMessage (false);
  701. }
  702. void valueTreeChildAdded (ValueTree&, ValueTree&) override {}
  703. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override {}
  704. void valueTreeChildOrderChanged (ValueTree&, int, int) override {}
  705. void valueTreeParentChanged (ValueTree&) override {}
  706. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource)
  707. };
  708. Value ValueTree::getPropertyAsValue (const Identifier& name, UndoManager* const undoManager)
  709. {
  710. return Value (new ValueTreePropertyValueSource (*this, name, undoManager));
  711. }
  712. //==============================================================================
  713. int ValueTree::getNumChildren() const noexcept
  714. {
  715. return object == nullptr ? 0 : object->children.size();
  716. }
  717. ValueTree ValueTree::getChild (int index) const
  718. {
  719. return ValueTree (object != nullptr ? object->children.getObjectPointer (index)
  720. : static_cast<SharedObject*> (nullptr));
  721. }
  722. ValueTree::Iterator::Iterator (const ValueTree& v, bool isEnd) noexcept
  723. : internal (v.object != nullptr ? (isEnd ? v.object->children.end() : v.object->children.begin()) : nullptr)
  724. {}
  725. ValueTree::Iterator& ValueTree::Iterator::operator++() noexcept
  726. {
  727. internal = static_cast<SharedObject**> (internal) + 1;
  728. return *this;
  729. }
  730. bool ValueTree::Iterator::operator!= (const Iterator& other) const noexcept
  731. {
  732. return internal != other.internal;
  733. }
  734. ValueTree ValueTree::Iterator::operator*() const
  735. {
  736. return ValueTree (*static_cast<SharedObject**> (internal));
  737. }
  738. ValueTree::Iterator ValueTree::begin() const noexcept { return Iterator (*this, false); }
  739. ValueTree::Iterator ValueTree::end() const noexcept { return Iterator (*this, true); }
  740. ValueTree ValueTree::getChildWithName (const Identifier& type) const
  741. {
  742. return object != nullptr ? object->getChildWithName (type) : ValueTree();
  743. }
  744. ValueTree ValueTree::getOrCreateChildWithName (const Identifier& type, UndoManager* undoManager)
  745. {
  746. return object != nullptr ? object->getOrCreateChildWithName (type, undoManager) : ValueTree();
  747. }
  748. ValueTree ValueTree::getChildWithProperty (const Identifier& propertyName, const var& propertyValue) const
  749. {
  750. return object != nullptr ? object->getChildWithProperty (propertyName, propertyValue) : ValueTree();
  751. }
  752. bool ValueTree::isAChildOf (const ValueTree& possibleParent) const noexcept
  753. {
  754. return object != nullptr && object->isAChildOf (possibleParent.object);
  755. }
  756. int ValueTree::indexOf (const ValueTree& child) const noexcept
  757. {
  758. return object != nullptr ? object->indexOf (child) : -1;
  759. }
  760. void ValueTree::addChild (const ValueTree& child, int index, UndoManager* const undoManager)
  761. {
  762. jassert (object != nullptr); // Trying to add a child to a null ValueTree!
  763. if (object != nullptr)
  764. object->addChild (child.object, index, undoManager);
  765. }
  766. void ValueTree::removeChild (const int childIndex, UndoManager* const undoManager)
  767. {
  768. if (object != nullptr)
  769. object->removeChild (childIndex, undoManager);
  770. }
  771. void ValueTree::removeChild (const ValueTree& child, UndoManager* const undoManager)
  772. {
  773. if (object != nullptr)
  774. object->removeChild (object->children.indexOf (child.object), undoManager);
  775. }
  776. void ValueTree::removeAllChildren (UndoManager* const undoManager)
  777. {
  778. if (object != nullptr)
  779. object->removeAllChildren (undoManager);
  780. }
  781. void ValueTree::moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
  782. {
  783. if (object != nullptr)
  784. object->moveChild (currentIndex, newIndex, undoManager);
  785. }
  786. //==============================================================================
  787. void ValueTree::createListOfChildren (OwnedArray<ValueTree>& list) const
  788. {
  789. jassert (object != nullptr);
  790. for (int i = 0; i < object->children.size(); ++i)
  791. list.add (new ValueTree (object->children.getObjectPointerUnchecked(i)));
  792. }
  793. void ValueTree::reorderChildren (const OwnedArray<ValueTree>& newOrder, UndoManager* undoManager)
  794. {
  795. jassert (object != nullptr);
  796. object->reorderChildren (newOrder, undoManager);
  797. }
  798. //==============================================================================
  799. void ValueTree::addListener (Listener* listener)
  800. {
  801. if (listener != nullptr)
  802. {
  803. if (listeners.isEmpty() && object != nullptr)
  804. object->valueTreesWithListeners.add (this);
  805. listeners.add (listener);
  806. }
  807. }
  808. void ValueTree::removeListener (Listener* listener)
  809. {
  810. listeners.remove (listener);
  811. if (listeners.isEmpty() && object != nullptr)
  812. object->valueTreesWithListeners.removeValue (this);
  813. }
  814. void ValueTree::sendPropertyChangeMessage (const Identifier& property)
  815. {
  816. if (object != nullptr)
  817. object->sendPropertyChangeMessage (property);
  818. }
  819. //==============================================================================
  820. XmlElement* ValueTree::createXml() const
  821. {
  822. return object != nullptr ? object->createXml() : nullptr;
  823. }
  824. ValueTree ValueTree::fromXml (const XmlElement& xml)
  825. {
  826. if (! xml.isTextElement())
  827. {
  828. ValueTree v (xml.getTagName());
  829. v.object->properties.setFromXmlAttributes (xml);
  830. forEachXmlChildElement (xml, e)
  831. v.addChild (fromXml (*e), -1, nullptr);
  832. return v;
  833. }
  834. // ValueTrees don't have any equivalent to XML text elements!
  835. jassertfalse;
  836. return ValueTree();
  837. }
  838. String ValueTree::toXmlString() const
  839. {
  840. const ScopedPointer<XmlElement> xml (createXml());
  841. return xml != nullptr ? xml->createDocument (StringRef()) : String();
  842. }
  843. //==============================================================================
  844. void ValueTree::writeToStream (OutputStream& output) const
  845. {
  846. SharedObject::writeObjectToStream (output, object);
  847. }
  848. ValueTree ValueTree::readFromStream (InputStream& input)
  849. {
  850. const String type (input.readString());
  851. if (type.isEmpty())
  852. return ValueTree();
  853. ValueTree v (type);
  854. const int numProps = input.readCompressedInt();
  855. if (numProps < 0)
  856. {
  857. jassertfalse; // trying to read corrupted data!
  858. return v;
  859. }
  860. for (int i = 0; i < numProps; ++i)
  861. {
  862. const String name (input.readString());
  863. if (name.isNotEmpty())
  864. {
  865. const var value (var::readFromStream (input));
  866. v.object->properties.set (name, value);
  867. }
  868. else
  869. {
  870. jassertfalse; // trying to read corrupted data!
  871. }
  872. }
  873. const int numChildren = input.readCompressedInt();
  874. v.object->children.ensureStorageAllocated (numChildren);
  875. for (int i = 0; i < numChildren; ++i)
  876. {
  877. ValueTree child (readFromStream (input));
  878. if (! child.isValid())
  879. return v;
  880. v.object->children.add (child.object);
  881. child.object->parent = v.object;
  882. }
  883. return v;
  884. }
  885. ValueTree ValueTree::readFromData (const void* const data, const size_t numBytes)
  886. {
  887. MemoryInputStream in (data, numBytes, false);
  888. return readFromStream (in);
  889. }
  890. ValueTree ValueTree::readFromGZIPData (const void* const data, const size_t numBytes)
  891. {
  892. MemoryInputStream in (data, numBytes, false);
  893. GZIPDecompressorInputStream gzipStream (in);
  894. return readFromStream (gzipStream);
  895. }
  896. void ValueTree::Listener::valueTreeRedirected (ValueTree&) {}
  897. //==============================================================================
  898. #if JUCE_UNIT_TESTS
  899. class ValueTreeTests : public UnitTest
  900. {
  901. public:
  902. ValueTreeTests() : UnitTest ("ValueTrees") {}
  903. static String createRandomIdentifier (Random& r)
  904. {
  905. char buffer[50] = { 0 };
  906. const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:";
  907. for (int i = 1 + r.nextInt (numElementsInArray (buffer) - 2); --i >= 0;)
  908. buffer[i] = chars [r.nextInt (sizeof (chars) - 1)];
  909. String result (buffer);
  910. if (! XmlElement::isValidXmlName (result))
  911. result = createRandomIdentifier (r);
  912. return result;
  913. }
  914. static String createRandomWideCharString (Random& r)
  915. {
  916. juce_wchar buffer[50] = { 0 };
  917. for (int i = r.nextInt (numElementsInArray (buffer) - 1); --i >= 0;)
  918. {
  919. if (r.nextBool())
  920. {
  921. do
  922. {
  923. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  924. }
  925. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  926. }
  927. else
  928. buffer[i] = (juce_wchar) (1 + r.nextInt (0x7e));
  929. }
  930. return CharPointer_UTF32 (buffer);
  931. }
  932. static ValueTree createRandomTree (UndoManager* undoManager, int depth, Random& r)
  933. {
  934. ValueTree v (createRandomIdentifier (r));
  935. for (int i = r.nextInt (10); --i >= 0;)
  936. {
  937. switch (r.nextInt (5))
  938. {
  939. case 0: v.setProperty (createRandomIdentifier (r), createRandomWideCharString (r), undoManager); break;
  940. case 1: v.setProperty (createRandomIdentifier (r), r.nextInt(), undoManager); break;
  941. case 2: if (depth < 5) v.addChild (createRandomTree (undoManager, depth + 1, r), r.nextInt (v.getNumChildren() + 1), undoManager); break;
  942. case 3: v.setProperty (createRandomIdentifier (r), r.nextBool(), undoManager); break;
  943. case 4: v.setProperty (createRandomIdentifier (r), r.nextDouble(), undoManager); break;
  944. default: break;
  945. }
  946. }
  947. return v;
  948. }
  949. void runTest() override
  950. {
  951. beginTest ("ValueTree");
  952. Random r = getRandom();
  953. for (int i = 10; --i >= 0;)
  954. {
  955. MemoryOutputStream mo;
  956. ValueTree v1 (createRandomTree (nullptr, 0, r));
  957. v1.writeToStream (mo);
  958. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  959. ValueTree v2 = ValueTree::readFromStream (mi);
  960. expect (v1.isEquivalentTo (v2));
  961. ScopedPointer<XmlElement> xml1 (v1.createXml());
  962. ScopedPointer<XmlElement> xml2 (v2.createCopy().createXml());
  963. expect (xml1->isEquivalentTo (xml2, false));
  964. ValueTree v4 = v2.createCopy();
  965. expect (v1.isEquivalentTo (v4));
  966. }
  967. }
  968. };
  969. static ValueTreeTests valueTreeTests;
  970. #endif