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.

juce_ValueTree.cpp 35KB

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