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.

811 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /** An array designed for holding objects.
  21. This holds a list of pointers to objects, and will automatically
  22. delete the objects when they are removed from the array, or when the
  23. array is itself deleted.
  24. Declare it in the form: OwnedArray<MyObjectClass>
  25. ..and then add new objects, e.g. myOwnedArray.add (new MyObjectClass());
  26. After adding objects, they are 'owned' by the array and will be deleted when
  27. removed or replaced.
  28. To make all the array's methods thread-safe, pass in "CriticalSection" as the templated
  29. TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection.
  30. @see Array, ReferenceCountedArray, StringArray, CriticalSection
  31. @tags{Core}
  32. */
  33. template <class ObjectClass,
  34. class TypeOfCriticalSectionToUse = DummyCriticalSection>
  35. class OwnedArray
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates an empty array. */
  40. OwnedArray() = default;
  41. /** Deletes the array and also deletes any objects inside it.
  42. To get rid of the array without deleting its objects, use its
  43. clear (false) method before deleting it.
  44. */
  45. ~OwnedArray()
  46. {
  47. deleteAllObjects();
  48. }
  49. /** Move constructor. */
  50. OwnedArray (OwnedArray&& other) noexcept
  51. : values (std::move (other.values))
  52. {
  53. }
  54. /** Creates an array from a list of objects. */
  55. OwnedArray (const std::initializer_list<ObjectClass*>& items)
  56. {
  57. addArray (items);
  58. }
  59. /** Move assignment operator. */
  60. OwnedArray& operator= (OwnedArray&& other) noexcept
  61. {
  62. const ScopedLockType lock (getLock());
  63. deleteAllObjects();
  64. values = std::move (other.values);
  65. return *this;
  66. }
  67. /** Converting move constructor. */
  68. template <class OtherObjectClass, class OtherCriticalSection>
  69. OwnedArray (OwnedArray<OtherObjectClass, OtherCriticalSection>&& other) noexcept
  70. : values (std::move (other.values))
  71. {
  72. }
  73. /** Converting move assignment operator. */
  74. template <class OtherObjectClass, class OtherCriticalSection>
  75. OwnedArray& operator= (OwnedArray<OtherObjectClass, OtherCriticalSection>&& other) noexcept
  76. {
  77. const ScopedLockType lock (getLock());
  78. deleteAllObjects();
  79. values = std::move (other.values);
  80. return *this;
  81. }
  82. //==============================================================================
  83. /** Clears the array, optionally deleting the objects inside it first. */
  84. void clear (bool deleteObjects = true)
  85. {
  86. const ScopedLockType lock (getLock());
  87. clearQuick (deleteObjects);
  88. values.setAllocatedSize (0);
  89. }
  90. //==============================================================================
  91. /** Clears the array, optionally deleting the objects inside it first. */
  92. void clearQuick (bool deleteObjects)
  93. {
  94. const ScopedLockType lock (getLock());
  95. if (deleteObjects)
  96. deleteAllObjects();
  97. else
  98. values.clear();
  99. }
  100. //==============================================================================
  101. /** Returns the number of items currently in the array.
  102. @see operator[]
  103. */
  104. inline int size() const noexcept
  105. {
  106. return values.size();
  107. }
  108. /** Returns true if the array is empty, false otherwise. */
  109. inline bool isEmpty() const noexcept
  110. {
  111. return size() == 0;
  112. }
  113. /** Returns a pointer to the object at this index in the array.
  114. If the index is out-of-range, this will return a null pointer, (and
  115. it could be null anyway, because it's ok for the array to hold null
  116. pointers as well as objects).
  117. @see getUnchecked
  118. */
  119. inline ObjectClass* operator[] (const int index) const noexcept
  120. {
  121. const ScopedLockType lock (getLock());
  122. return values.getValueWithDefault (index);
  123. }
  124. /** Returns a pointer to the object at this index in the array, without checking whether the index is in-range.
  125. This is a faster and less safe version of operator[] which doesn't check the index passed in, so
  126. it can be used when you're sure the index is always going to be legal.
  127. */
  128. inline ObjectClass* getUnchecked (const int index) const noexcept
  129. {
  130. const ScopedLockType lock (getLock());
  131. return values[index];
  132. }
  133. /** Returns a pointer to the first object in the array.
  134. This will return a null pointer if the array's empty.
  135. @see getLast
  136. */
  137. inline ObjectClass* getFirst() const noexcept
  138. {
  139. const ScopedLockType lock (getLock());
  140. return values.getFirst();
  141. }
  142. /** Returns a pointer to the last object in the array.
  143. This will return a null pointer if the array's empty.
  144. @see getFirst
  145. */
  146. inline ObjectClass* getLast() const noexcept
  147. {
  148. const ScopedLockType lock (getLock());
  149. return values.getLast();
  150. }
  151. /** Returns a pointer to the actual array data.
  152. This pointer will only be valid until the next time a non-const method
  153. is called on the array.
  154. */
  155. inline ObjectClass** getRawDataPointer() noexcept
  156. {
  157. return values.begin();
  158. }
  159. //==============================================================================
  160. /** Returns a pointer to the first element in the array.
  161. This method is provided for compatibility with standard C++ iteration mechanisms.
  162. */
  163. inline ObjectClass** begin() const noexcept
  164. {
  165. return values.begin();
  166. }
  167. /** Returns a pointer to the element which follows the last element in the array.
  168. This method is provided for compatibility with standard C++ iteration mechanisms.
  169. */
  170. inline ObjectClass** end() const noexcept
  171. {
  172. return values.end();
  173. }
  174. /** Returns a pointer to the first element in the array.
  175. This method is provided for compatibility with the standard C++ containers.
  176. */
  177. inline ObjectClass** data() const noexcept
  178. {
  179. return begin();
  180. }
  181. //==============================================================================
  182. /** Finds the index of an object which might be in the array.
  183. @param objectToLookFor the object to look for
  184. @returns the index at which the object was found, or -1 if it's not found
  185. */
  186. int indexOf (const ObjectClass* objectToLookFor) const noexcept
  187. {
  188. const ScopedLockType lock (getLock());
  189. auto** e = values.begin();
  190. for (; e != values.end(); ++e)
  191. if (objectToLookFor == *e)
  192. return static_cast<int> (e - values.begin());
  193. return -1;
  194. }
  195. /** Returns true if the array contains a specified object.
  196. @param objectToLookFor the object to look for
  197. @returns true if the object is in the array
  198. */
  199. bool contains (const ObjectClass* objectToLookFor) const noexcept
  200. {
  201. const ScopedLockType lock (getLock());
  202. auto** e = values.begin();
  203. for (; e != values.end(); ++e)
  204. if (objectToLookFor == *e)
  205. return true;
  206. return false;
  207. }
  208. //==============================================================================
  209. /** Appends a new object to the end of the array.
  210. Note that the this object will be deleted by the OwnedArray when it
  211. is removed, so be careful not to delete it somewhere else.
  212. Also be careful not to add the same object to the array more than once,
  213. as this will obviously cause deletion of dangling pointers.
  214. @param newObject the new object to add to the array
  215. @returns the new object that was added
  216. @see set, insert, addIfNotAlreadyThere, addSorted
  217. */
  218. ObjectClass* add (ObjectClass* newObject) noexcept
  219. {
  220. const ScopedLockType lock (getLock());
  221. values.add (newObject);
  222. return newObject;
  223. }
  224. /** Inserts a new object into the array at the given index.
  225. Note that the this object will be deleted by the OwnedArray when it
  226. is removed, so be careful not to delete it somewhere else.
  227. If the index is less than 0 or greater than the size of the array, the
  228. element will be added to the end of the array.
  229. Otherwise, it will be inserted into the array, moving all the later elements
  230. along to make room.
  231. Be careful not to add the same object to the array more than once,
  232. as this will obviously cause deletion of dangling pointers.
  233. @param indexToInsertAt the index at which the new element should be inserted
  234. @param newObject the new object to add to the array
  235. @returns the new object that was added
  236. @see add, addSorted, addIfNotAlreadyThere, set
  237. */
  238. ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject) noexcept
  239. {
  240. const ScopedLockType lock (getLock());
  241. values.insert (indexToInsertAt, newObject, 1);
  242. return newObject;
  243. }
  244. /** Inserts an array of values into this array at a given position.
  245. If the index is less than 0 or greater than the size of the array, the
  246. new elements will be added to the end of the array.
  247. Otherwise, they will be inserted into the array, moving all the later elements
  248. along to make room.
  249. @param indexToInsertAt the index at which the first new element should be inserted
  250. @param newObjects the new values to add to the array
  251. @param numberOfElements how many items are in the array
  252. @see insert, add, addSorted, set
  253. */
  254. void insertArray (int indexToInsertAt,
  255. ObjectClass* const* newObjects,
  256. int numberOfElements)
  257. {
  258. if (numberOfElements > 0)
  259. {
  260. const ScopedLockType lock (getLock());
  261. values.insertArray (indexToInsertAt, newObjects, numberOfElements);
  262. }
  263. }
  264. /** Appends a new object at the end of the array as long as the array doesn't
  265. already contain it.
  266. If the array already contains a matching object, nothing will be done.
  267. @param newObject the new object to add to the array
  268. @returns true if the new object was added, false otherwise
  269. */
  270. bool addIfNotAlreadyThere (ObjectClass* newObject) noexcept
  271. {
  272. const ScopedLockType lock (getLock());
  273. if (contains (newObject))
  274. return false;
  275. add (newObject);
  276. return true;
  277. }
  278. /** Replaces an object in the array with a different one.
  279. If the index is less than zero, this method does nothing.
  280. If the index is beyond the end of the array, the new object is added to the end of the array.
  281. Be careful not to add the same object to the array more than once,
  282. as this will obviously cause deletion of dangling pointers.
  283. @param indexToChange the index whose value you want to change
  284. @param newObject the new value to set for this index.
  285. @param deleteOldElement whether to delete the object that's being replaced with the new one
  286. @see add, insert, remove
  287. */
  288. ObjectClass* set (int indexToChange, ObjectClass* newObject, bool deleteOldElement = true)
  289. {
  290. if (indexToChange >= 0)
  291. {
  292. std::unique_ptr<ObjectClass> toDelete;
  293. {
  294. const ScopedLockType lock (getLock());
  295. if (indexToChange < values.size())
  296. {
  297. if (deleteOldElement)
  298. {
  299. toDelete.reset (values[indexToChange]);
  300. if (toDelete.get() == newObject)
  301. toDelete.release();
  302. }
  303. values[indexToChange] = newObject;
  304. }
  305. else
  306. {
  307. values.add (newObject);
  308. }
  309. }
  310. }
  311. else
  312. {
  313. jassertfalse; // you're trying to set an object at a negative index, which doesn't have
  314. // any effect - but since the object is not being added, it may be leaking..
  315. }
  316. return newObject;
  317. }
  318. /** Adds elements from another array to the end of this array.
  319. @param arrayToAddFrom the array from which to copy the elements
  320. @param startIndex the first element of the other array to start copying from
  321. @param numElementsToAdd how many elements to add from the other array. If this
  322. value is negative or greater than the number of available elements,
  323. all available elements will be copied.
  324. @see add
  325. */
  326. template <class OtherArrayType>
  327. void addArray (const OtherArrayType& arrayToAddFrom,
  328. int startIndex = 0,
  329. int numElementsToAdd = -1)
  330. {
  331. const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
  332. const ScopedLockType lock2 (getLock());
  333. values.addArray (arrayToAddFrom, startIndex, numElementsToAdd);
  334. }
  335. /** Adds elements from another array to the end of this array. */
  336. template <typename OtherArrayType>
  337. void addArray (const std::initializer_list<OtherArrayType>& items)
  338. {
  339. const ScopedLockType lock (getLock());
  340. values.addArray (items);
  341. }
  342. /** Adds copies of the elements in another array to the end of this array.
  343. The other array must be either an OwnedArray of a compatible type of object, or an Array
  344. containing pointers to the same kind of object. The objects involved must provide
  345. a copy constructor, and this will be used to create new copies of each element, and
  346. add them to this array.
  347. @param arrayToAddFrom the array from which to copy the elements
  348. @param startIndex the first element of the other array to start copying from
  349. @param numElementsToAdd how many elements to add from the other array. If this
  350. value is negative or greater than the number of available elements,
  351. all available elements will be copied.
  352. @see add
  353. */
  354. template <class OtherArrayType>
  355. void addCopiesOf (const OtherArrayType& arrayToAddFrom,
  356. int startIndex = 0,
  357. int numElementsToAdd = -1)
  358. {
  359. const typename OtherArrayType::ScopedLockType lock1 (arrayToAddFrom.getLock());
  360. const ScopedLockType lock2 (getLock());
  361. if (startIndex < 0)
  362. {
  363. jassertfalse;
  364. startIndex = 0;
  365. }
  366. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
  367. numElementsToAdd = arrayToAddFrom.size() - startIndex;
  368. jassert (numElementsToAdd >= 0);
  369. values.ensureAllocatedSize (values.size() + numElementsToAdd);
  370. while (--numElementsToAdd >= 0)
  371. values.add (createCopyIfNotNull (arrayToAddFrom.getUnchecked (startIndex++)));
  372. }
  373. /** Inserts a new object into the array assuming that the array is sorted.
  374. This will use a comparator to find the position at which the new object
  375. should go. If the array isn't sorted, the behaviour of this
  376. method will be unpredictable.
  377. @param comparator the comparator to use to compare the elements - see the sort method
  378. for details about this object's structure
  379. @param newObject the new object to insert to the array
  380. @returns the index at which the new object was added
  381. @see add, sort, indexOfSorted
  382. */
  383. template <class ElementComparator>
  384. int addSorted (ElementComparator& comparator, ObjectClass* const newObject) noexcept
  385. {
  386. // If you pass in an object with a static compareElements() method, this
  387. // avoids getting warning messages about the parameter being unused
  388. ignoreUnused (comparator);
  389. const ScopedLockType lock (getLock());
  390. const int index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
  391. insert (index, newObject);
  392. return index;
  393. }
  394. /** Finds the index of an object in the array, assuming that the array is sorted.
  395. This will use a comparator to do a binary-chop to find the index of the given
  396. element, if it exists. If the array isn't sorted, the behaviour of this
  397. method will be unpredictable.
  398. @param comparator the comparator to use to compare the elements - see the sort()
  399. method for details about the form this object should take
  400. @param objectToLookFor the object to search for
  401. @returns the index of the element, or -1 if it's not found
  402. @see addSorted, sort
  403. */
  404. template <typename ElementComparator>
  405. int indexOfSorted (ElementComparator& comparator, const ObjectClass* const objectToLookFor) const noexcept
  406. {
  407. // If you pass in an object with a static compareElements() method, this
  408. // avoids getting warning messages about the parameter being unused
  409. ignoreUnused (comparator);
  410. const ScopedLockType lock (getLock());
  411. int s = 0, e = values.size();
  412. while (s < e)
  413. {
  414. if (comparator.compareElements (objectToLookFor, values[s]) == 0)
  415. return s;
  416. auto halfway = (s + e) / 2;
  417. if (halfway == s)
  418. break;
  419. if (comparator.compareElements (objectToLookFor, values[halfway]) >= 0)
  420. s = halfway;
  421. else
  422. e = halfway;
  423. }
  424. return -1;
  425. }
  426. //==============================================================================
  427. /** Removes an object from the array.
  428. This will remove the object at a given index (optionally also
  429. deleting it) and move back all the subsequent objects to close the gap.
  430. If the index passed in is out-of-range, nothing will happen.
  431. @param indexToRemove the index of the element to remove
  432. @param deleteObject whether to delete the object that is removed
  433. @see removeObject, removeRange
  434. */
  435. void remove (int indexToRemove, bool deleteObject = true)
  436. {
  437. std::unique_ptr<ObjectClass> toDelete;
  438. {
  439. const ScopedLockType lock (getLock());
  440. if (isPositiveAndBelow (indexToRemove, values.size()))
  441. {
  442. auto** e = values.begin() + indexToRemove;
  443. if (deleteObject)
  444. toDelete.reset (*e);
  445. values.removeElements (indexToRemove, 1);
  446. }
  447. }
  448. if ((values.size() << 1) < values.capacity())
  449. minimiseStorageOverheads();
  450. }
  451. /** Removes and returns an object from the array without deleting it.
  452. This will remove the object at a given index and return it, moving back all
  453. the subsequent objects to close the gap. If the index passed in is out-of-range,
  454. nothing will happen.
  455. @param indexToRemove the index of the element to remove
  456. @see remove, removeObject, removeRange
  457. */
  458. ObjectClass* removeAndReturn (int indexToRemove)
  459. {
  460. ObjectClass* removedItem = nullptr;
  461. const ScopedLockType lock (getLock());
  462. if (isPositiveAndBelow (indexToRemove, values.size()))
  463. {
  464. removedItem = values[indexToRemove];
  465. values.removeElements (indexToRemove, 1);
  466. if ((values.size() << 1) < values.capacity())
  467. minimiseStorageOverheads();
  468. }
  469. return removedItem;
  470. }
  471. /** Removes a specified object from the array.
  472. If the item isn't found, no action is taken.
  473. @param objectToRemove the object to try to remove
  474. @param deleteObject whether to delete the object (if it's found)
  475. @see remove, removeRange
  476. */
  477. void removeObject (const ObjectClass* objectToRemove, bool deleteObject = true)
  478. {
  479. const ScopedLockType lock (getLock());
  480. for (int i = 0; i < values.size(); ++i)
  481. {
  482. if (objectToRemove == values[i])
  483. {
  484. remove (i, deleteObject);
  485. break;
  486. }
  487. }
  488. }
  489. /** Removes a range of objects from the array.
  490. This will remove a set of objects, starting from the given index,
  491. and move any subsequent elements down to close the gap.
  492. If the range extends beyond the bounds of the array, it will
  493. be safely clipped to the size of the array.
  494. @param startIndex the index of the first object to remove
  495. @param numberToRemove how many objects should be removed
  496. @param deleteObjects whether to delete the objects that get removed
  497. @see remove, removeObject
  498. */
  499. void removeRange (int startIndex, int numberToRemove, bool deleteObjects = true)
  500. {
  501. const ScopedLockType lock (getLock());
  502. auto endIndex = jlimit (0, values.size(), startIndex + numberToRemove);
  503. startIndex = jlimit (0, values.size(), startIndex);
  504. numberToRemove = endIndex - startIndex;
  505. if (numberToRemove > 0)
  506. {
  507. Array<ObjectClass*> objectsToDelete;
  508. if (deleteObjects)
  509. objectsToDelete.addArray (values.begin() + startIndex, numberToRemove);
  510. values.removeElements (startIndex, numberToRemove);
  511. for (auto& o : objectsToDelete)
  512. ContainerDeletePolicy<ObjectClass>::destroy (o);
  513. if ((values.size() << 1) < values.capacity())
  514. minimiseStorageOverheads();
  515. }
  516. }
  517. /** Removes the last n objects from the array.
  518. @param howManyToRemove how many objects to remove from the end of the array
  519. @param deleteObjects whether to also delete the objects that are removed
  520. @see remove, removeObject, removeRange
  521. */
  522. void removeLast (int howManyToRemove = 1,
  523. bool deleteObjects = true)
  524. {
  525. const ScopedLockType lock (getLock());
  526. if (howManyToRemove >= values.size())
  527. clear (deleteObjects);
  528. else
  529. removeRange (values.size() - howManyToRemove, howManyToRemove, deleteObjects);
  530. }
  531. /** Swaps a pair of objects in the array.
  532. If either of the indexes passed in is out-of-range, nothing will happen,
  533. otherwise the two objects at these positions will be exchanged.
  534. */
  535. void swap (int index1, int index2) noexcept
  536. {
  537. const ScopedLockType lock (getLock());
  538. values.swap (index1, index2);
  539. }
  540. /** Moves one of the objects to a different position.
  541. This will move the object to a specified index, shuffling along
  542. any intervening elements as required.
  543. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  544. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  545. @param currentIndex the index of the object to be moved. If this isn't a
  546. valid index, then nothing will be done
  547. @param newIndex the index at which you'd like this object to end up. If this
  548. is less than zero, it will be moved to the end of the array
  549. */
  550. void move (int currentIndex, int newIndex) noexcept
  551. {
  552. if (currentIndex != newIndex)
  553. {
  554. const ScopedLockType lock (getLock());
  555. values.move (currentIndex, newIndex);
  556. }
  557. }
  558. /** This swaps the contents of this array with those of another array.
  559. If you need to exchange two arrays, this is vastly quicker than using copy-by-value
  560. because it just swaps their internal pointers.
  561. */
  562. template <class OtherArrayType>
  563. void swapWith (OtherArrayType& otherArray) noexcept
  564. {
  565. const ScopedLockType lock1 (getLock());
  566. const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock());
  567. values.swapWith (otherArray.values);
  568. }
  569. //==============================================================================
  570. /** Reduces the amount of storage being used by the array.
  571. Arrays typically allocate slightly more storage than they need, and after
  572. removing elements, they may have quite a lot of unused space allocated.
  573. This method will reduce the amount of allocated storage to a minimum.
  574. */
  575. void minimiseStorageOverheads() noexcept
  576. {
  577. const ScopedLockType lock (getLock());
  578. values.shrinkToNoMoreThan (values.size());
  579. }
  580. /** Increases the array's internal storage to hold a minimum number of elements.
  581. Calling this before adding a large known number of elements means that
  582. the array won't have to keep dynamically resizing itself as the elements
  583. are added, and it'll therefore be more efficient.
  584. */
  585. void ensureStorageAllocated (const int minNumElements) noexcept
  586. {
  587. const ScopedLockType lock (getLock());
  588. values.ensureAllocatedSize (minNumElements);
  589. }
  590. //==============================================================================
  591. /** Sorts the elements in the array.
  592. This will use a comparator object to sort the elements into order. The object
  593. passed must have a method of the form:
  594. @code
  595. int compareElements (ElementType* first, ElementType* second);
  596. @endcode
  597. ..and this method must return:
  598. - a value of < 0 if the first comes before the second
  599. - a value of 0 if the two objects are equivalent
  600. - a value of > 0 if the second comes before the first
  601. To improve performance, the compareElements() method can be declared as static or const.
  602. @param comparator the comparator to use for comparing elements.
  603. @param retainOrderOfEquivalentItems if this is true, then items
  604. which the comparator says are equivalent will be
  605. kept in the order in which they currently appear
  606. in the array. This is slower to perform, but may
  607. be important in some cases. If it's false, a faster
  608. algorithm is used, but equivalent elements may be
  609. rearranged.
  610. @see sortArray, indexOfSorted
  611. */
  612. template <class ElementComparator>
  613. void sort (ElementComparator& comparator,
  614. bool retainOrderOfEquivalentItems = false) const noexcept
  615. {
  616. // If you pass in an object with a static compareElements() method, this
  617. // avoids getting warning messages about the parameter being unused
  618. ignoreUnused (comparator);
  619. const ScopedLockType lock (getLock());
  620. if (size() > 1)
  621. sortArray (comparator, values.begin(), 0, size() - 1, retainOrderOfEquivalentItems);
  622. }
  623. //==============================================================================
  624. /** Returns the CriticalSection that locks this array.
  625. To lock, you can call getLock().enter() and getLock().exit(), or preferably use
  626. an object of ScopedLockType as an RAII lock for it.
  627. */
  628. inline const TypeOfCriticalSectionToUse& getLock() const noexcept { return values; }
  629. /** Returns the type of scoped lock to use for locking this array */
  630. using ScopedLockType = typename TypeOfCriticalSectionToUse::ScopedLockType;
  631. //==============================================================================
  632. #ifndef DOXYGEN
  633. // Note that the swapWithArray method has been replaced by a more flexible templated version,
  634. // and renamed "swapWith" to be more consistent with the names used in other classes.
  635. JUCE_DEPRECATED_WITH_BODY (void swapWithArray (OwnedArray& other) noexcept, { swapWith (other); })
  636. #endif
  637. private:
  638. //==============================================================================
  639. ArrayBase <ObjectClass*, TypeOfCriticalSectionToUse> values;
  640. void deleteAllObjects()
  641. {
  642. auto i = values.size();
  643. while (--i >= 0)
  644. {
  645. auto* e = values[i];
  646. values.removeElements (i, 1);
  647. ContainerDeletePolicy<ObjectClass>::destroy (e);
  648. }
  649. }
  650. template <class OtherObjectClass, class OtherCriticalSection>
  651. friend class OwnedArray;
  652. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray)
  653. };
  654. } // namespace juce