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.

798 lines
29KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_REFERENCECOUNTEDARRAY_JUCEHEADER__
  19. #define __JUCE_REFERENCECOUNTEDARRAY_JUCEHEADER__
  20. #include "../memory/juce_ReferenceCountedObject.h"
  21. #include "juce_ArrayAllocationBase.h"
  22. #include "juce_ElementComparator.h"
  23. #include "../threads/juce_CriticalSection.h"
  24. //==============================================================================
  25. /**
  26. Holds a list of objects derived from ReferenceCountedObject.
  27. A ReferenceCountedArray holds objects derived from ReferenceCountedObject,
  28. and takes care of incrementing and decrementing their ref counts when they
  29. are added and removed from the array.
  30. To make all the array's methods thread-safe, pass in "CriticalSection" as the templated
  31. TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection.
  32. @see Array, OwnedArray, StringArray
  33. */
  34. template <class ObjectClass, class TypeOfCriticalSectionToUse = DummyCriticalSection>
  35. class ReferenceCountedArray
  36. {
  37. public:
  38. typedef ReferenceCountedObjectPtr<ObjectClass> ObjectClassPtr;
  39. //==============================================================================
  40. /** Creates an empty array.
  41. @see ReferenceCountedObject, Array, OwnedArray
  42. */
  43. ReferenceCountedArray() noexcept
  44. : numUsed (0)
  45. {
  46. }
  47. /** Creates a copy of another array */
  48. template <class OtherObjectClass>
  49. ReferenceCountedArray (const ReferenceCountedArray<OtherObjectClass, TypeOfCriticalSectionToUse>& other) noexcept
  50. {
  51. const ScopedLockType lock (other.getLock());
  52. numUsed = other.size();
  53. data.setAllocatedSize (numUsed);
  54. memcpy (data.elements, other.getRawDataPointer(), numUsed * sizeof (ObjectClass*));
  55. for (int i = numUsed; --i >= 0;)
  56. if (data.elements[i] != nullptr)
  57. data.elements[i]->incReferenceCount();
  58. }
  59. /** Copies another array into this one.
  60. Any existing objects in this array will first be released.
  61. */
  62. template <class OtherObjectClass>
  63. ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse>& operator= (const ReferenceCountedArray<OtherObjectClass, TypeOfCriticalSectionToUse>& other) noexcept
  64. {
  65. ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse> otherCopy (other);
  66. swapWithArray (otherCopy);
  67. return *this;
  68. }
  69. /** Destructor.
  70. Any objects in the array will be released, and may be deleted if not referenced from elsewhere.
  71. */
  72. ~ReferenceCountedArray()
  73. {
  74. clear();
  75. }
  76. //==============================================================================
  77. /** Removes all objects from the array.
  78. Any objects in the array that are not referenced from elsewhere will be deleted.
  79. */
  80. void clear()
  81. {
  82. const ScopedLockType lock (getLock());
  83. while (numUsed > 0)
  84. if (data.elements [--numUsed] != nullptr)
  85. data.elements [numUsed]->decReferenceCount();
  86. jassert (numUsed == 0);
  87. data.setAllocatedSize (0);
  88. }
  89. /** Returns the current number of objects in the array. */
  90. inline int size() const noexcept
  91. {
  92. return numUsed;
  93. }
  94. /** Returns a pointer to the object at this index in the array.
  95. If the index is out-of-range, this will return a null pointer, (and
  96. it could be null anyway, because it's ok for the array to hold null
  97. pointers as well as objects).
  98. @see getUnchecked
  99. */
  100. inline ObjectClassPtr operator[] (const int index) const noexcept
  101. {
  102. return getObjectPointer (index);
  103. }
  104. /** Returns a pointer to the object at this index in the array, without checking
  105. whether the index is in-range.
  106. This is a faster and less safe version of operator[] which doesn't check the index passed in, so
  107. it can be used when you're sure the index if always going to be legal.
  108. */
  109. inline ObjectClassPtr getUnchecked (const int index) const noexcept
  110. {
  111. return getObjectPointerUnchecked (index);
  112. }
  113. /** Returns a raw 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* getObjectPointer (const int index) const noexcept
  120. {
  121. const ScopedLockType lock (getLock());
  122. return isPositiveAndBelow (index, numUsed) ? data.elements [index]
  123. : nullptr;
  124. }
  125. /** Returns a raw pointer to the object at this index in the array, without checking
  126. whether the index is in-range.
  127. */
  128. inline ObjectClass* getObjectPointerUnchecked (const int index) const noexcept
  129. {
  130. const ScopedLockType lock (getLock());
  131. jassert (isPositiveAndBelow (index, numUsed));
  132. return data.elements [index];
  133. }
  134. /** Returns a pointer to the first object in the array.
  135. This will return a null pointer if the array's empty.
  136. @see getLast
  137. */
  138. inline ObjectClassPtr getFirst() const noexcept
  139. {
  140. const ScopedLockType lock (getLock());
  141. return numUsed > 0 ? data.elements [0]
  142. : static_cast <ObjectClass*> (nullptr);
  143. }
  144. /** Returns a pointer to the last object in the array.
  145. This will return a null pointer if the array's empty.
  146. @see getFirst
  147. */
  148. inline ObjectClassPtr getLast() const noexcept
  149. {
  150. const ScopedLockType lock (getLock());
  151. return numUsed > 0 ? data.elements [numUsed - 1]
  152. : static_cast <ObjectClass*> (nullptr);
  153. }
  154. /** Returns a pointer to the actual array data.
  155. This pointer will only be valid until the next time a non-const method
  156. is called on the array.
  157. */
  158. inline ObjectClass** getRawDataPointer() const noexcept
  159. {
  160. return data.elements;
  161. }
  162. //==============================================================================
  163. /** Returns a pointer to the first element in the array.
  164. This method is provided for compatibility with standard C++ iteration mechanisms.
  165. */
  166. inline ObjectClass** begin() const noexcept
  167. {
  168. return data.elements;
  169. }
  170. /** Returns a pointer to the element which follows the last element in the array.
  171. This method is provided for compatibility with standard C++ iteration mechanisms.
  172. */
  173. inline ObjectClass** end() const noexcept
  174. {
  175. return data.elements + numUsed;
  176. }
  177. //==============================================================================
  178. /** Finds the index of the first occurrence of an object in the array.
  179. @param objectToLookFor the object to look for
  180. @returns the index at which the object was found, or -1 if it's not found
  181. */
  182. int indexOf (const ObjectClass* const objectToLookFor) const noexcept
  183. {
  184. const ScopedLockType lock (getLock());
  185. ObjectClass** e = data.elements.getData();
  186. ObjectClass** const end_ = e + numUsed;
  187. while (e != end_)
  188. {
  189. if (objectToLookFor == *e)
  190. return static_cast <int> (e - data.elements.getData());
  191. ++e;
  192. }
  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* const objectToLookFor) const noexcept
  200. {
  201. const ScopedLockType lock (getLock());
  202. ObjectClass** e = data.elements.getData();
  203. ObjectClass** const end_ = e + numUsed;
  204. while (e != end_)
  205. {
  206. if (objectToLookFor == *e)
  207. return true;
  208. ++e;
  209. }
  210. return false;
  211. }
  212. /** Appends a new object to the end of the array.
  213. This will increase the new object's reference count.
  214. @param newObject the new object to add to the array
  215. @see set, insert, addIfNotAlreadyThere, addSorted, addArray
  216. */
  217. void add (ObjectClass* const newObject) noexcept
  218. {
  219. const ScopedLockType lock (getLock());
  220. data.ensureAllocatedSize (numUsed + 1);
  221. data.elements [numUsed++] = newObject;
  222. if (newObject != nullptr)
  223. newObject->incReferenceCount();
  224. }
  225. /** Inserts a new object into the array at the given index.
  226. If the index is less than 0 or greater than the size of the array, the
  227. element will be added to the end of the array.
  228. Otherwise, it will be inserted into the array, moving all the later elements
  229. along to make room.
  230. This will increase the new object's reference count.
  231. @param indexToInsertAt the index at which the new element should be inserted
  232. @param newObject the new object to add to the array
  233. @see add, addSorted, addIfNotAlreadyThere, set
  234. */
  235. void insert (int indexToInsertAt,
  236. ObjectClass* const newObject) noexcept
  237. {
  238. if (indexToInsertAt >= 0)
  239. {
  240. const ScopedLockType lock (getLock());
  241. if (indexToInsertAt > numUsed)
  242. indexToInsertAt = numUsed;
  243. data.ensureAllocatedSize (numUsed + 1);
  244. ObjectClass** const e = data.elements + indexToInsertAt;
  245. const int numToMove = numUsed - indexToInsertAt;
  246. if (numToMove > 0)
  247. memmove (e + 1, e, numToMove * sizeof (ObjectClass*));
  248. *e = newObject;
  249. if (newObject != nullptr)
  250. newObject->incReferenceCount();
  251. ++numUsed;
  252. }
  253. else
  254. {
  255. add (newObject);
  256. }
  257. }
  258. /** Appends a new object at the end of the array as long as the array doesn't
  259. already contain it.
  260. If the array already contains a matching object, nothing will be done.
  261. @param newObject the new object to add to the array
  262. */
  263. void addIfNotAlreadyThere (ObjectClass* const newObject) noexcept
  264. {
  265. const ScopedLockType lock (getLock());
  266. if (! contains (newObject))
  267. add (newObject);
  268. }
  269. /** Replaces an object in the array with a different one.
  270. If the index is less than zero, this method does nothing.
  271. If the index is beyond the end of the array, the new object is added to the end of the array.
  272. The object being added has its reference count increased, and if it's replacing
  273. another object, then that one has its reference count decreased, and may be deleted.
  274. @param indexToChange the index whose value you want to change
  275. @param newObject the new value to set for this index.
  276. @see add, insert, remove
  277. */
  278. void set (const int indexToChange,
  279. ObjectClass* const newObject)
  280. {
  281. if (indexToChange >= 0)
  282. {
  283. const ScopedLockType lock (getLock());
  284. if (newObject != nullptr)
  285. newObject->incReferenceCount();
  286. if (indexToChange < numUsed)
  287. {
  288. if (data.elements [indexToChange] != nullptr)
  289. data.elements [indexToChange]->decReferenceCount();
  290. data.elements [indexToChange] = newObject;
  291. }
  292. else
  293. {
  294. data.ensureAllocatedSize (numUsed + 1);
  295. data.elements [numUsed++] = newObject;
  296. }
  297. }
  298. }
  299. /** Adds elements from another array to the end of this array.
  300. @param arrayToAddFrom the array from which to copy the elements
  301. @param startIndex the first element of the other array to start copying from
  302. @param numElementsToAdd how many elements to add from the other array. If this
  303. value is negative or greater than the number of available elements,
  304. all available elements will be copied.
  305. @see add
  306. */
  307. void addArray (const ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse>& arrayToAddFrom,
  308. int startIndex = 0,
  309. int numElementsToAdd = -1) noexcept
  310. {
  311. const ScopedLockType lock1 (arrayToAddFrom.getLock());
  312. {
  313. const ScopedLockType lock2 (getLock());
  314. if (startIndex < 0)
  315. {
  316. jassertfalse;
  317. startIndex = 0;
  318. }
  319. if (numElementsToAdd < 0 || startIndex + numElementsToAdd > arrayToAddFrom.size())
  320. numElementsToAdd = arrayToAddFrom.size() - startIndex;
  321. if (numElementsToAdd > 0)
  322. {
  323. data.ensureAllocatedSize (numUsed + numElementsToAdd);
  324. while (--numElementsToAdd >= 0)
  325. add (arrayToAddFrom.getUnchecked (startIndex++));
  326. }
  327. }
  328. }
  329. /** Inserts a new object into the array assuming that the array is sorted.
  330. This will use a comparator to find the position at which the new object
  331. should go. If the array isn't sorted, the behaviour of this
  332. method will be unpredictable.
  333. @param comparator the comparator object to use to compare the elements - see the
  334. sort() method for details about this object's form
  335. @param newObject the new object to insert to the array
  336. @returns the index at which the new object was added
  337. @see add, sort
  338. */
  339. template <class ElementComparator>
  340. int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept
  341. {
  342. const ScopedLockType lock (getLock());
  343. const int index = findInsertIndexInSortedArray (comparator, data.elements.getData(), newObject, 0, numUsed);
  344. insert (index, newObject);
  345. return index;
  346. }
  347. /** Inserts or replaces an object in the array, assuming it is sorted.
  348. This is similar to addSorted, but if a matching element already exists, then it will be
  349. replaced by the new one, rather than the new one being added as well.
  350. */
  351. template <class ElementComparator>
  352. void addOrReplaceSorted (ElementComparator& comparator,
  353. ObjectClass* newObject) noexcept
  354. {
  355. const ScopedLockType lock (getLock());
  356. const int index = findInsertIndexInSortedArray (comparator, data.elements.getData(), newObject, 0, numUsed);
  357. if (index > 0 && comparator.compareElements (newObject, data.elements [index - 1]) == 0)
  358. set (index - 1, newObject); // replace an existing object that matches
  359. else
  360. insert (index, newObject); // no match, so insert the new one
  361. }
  362. //==============================================================================
  363. /** Removes an object from the array.
  364. This will remove the object at a given index and move back all the
  365. subsequent objects to close the gap.
  366. If the index passed in is out-of-range, nothing will happen.
  367. The object that is removed will have its reference count decreased,
  368. and may be deleted if not referenced from elsewhere.
  369. @param indexToRemove the index of the element to remove
  370. @see removeObject, removeRange
  371. */
  372. void remove (const int indexToRemove)
  373. {
  374. const ScopedLockType lock (getLock());
  375. if (isPositiveAndBelow (indexToRemove, numUsed))
  376. {
  377. ObjectClass** const e = data.elements + indexToRemove;
  378. if (*e != nullptr)
  379. (*e)->decReferenceCount();
  380. --numUsed;
  381. const int numberToShift = numUsed - indexToRemove;
  382. if (numberToShift > 0)
  383. memmove (e, e + 1, numberToShift * sizeof (ObjectClass*));
  384. if ((numUsed << 1) < data.numAllocated)
  385. minimiseStorageOverheads();
  386. }
  387. }
  388. /** Removes and returns an object from the array.
  389. This will remove the object at a given index and return it, moving back all
  390. the subsequent objects to close the gap. If the index passed in is out-of-range,
  391. nothing will happen and a null pointer will be returned.
  392. @param indexToRemove the index of the element to remove
  393. @see remove, removeObject, removeRange
  394. */
  395. ObjectClassPtr removeAndReturn (const int indexToRemove)
  396. {
  397. ObjectClassPtr removedItem;
  398. const ScopedLockType lock (getLock());
  399. if (isPositiveAndBelow (indexToRemove, numUsed))
  400. {
  401. ObjectClass** const e = data.elements + indexToRemove;
  402. if (*e != nullptr)
  403. {
  404. removedItem = *e;
  405. (*e)->decReferenceCount();
  406. }
  407. --numUsed;
  408. const int numberToShift = numUsed - indexToRemove;
  409. if (numberToShift > 0)
  410. memmove (e, e + 1, numberToShift * sizeof (ObjectClass*));
  411. if ((numUsed << 1) < data.numAllocated)
  412. minimiseStorageOverheads();
  413. }
  414. return removedItem;
  415. }
  416. /** Removes the first occurrence of a specified object from the array.
  417. If the item isn't found, no action is taken. If it is found, it is
  418. removed and has its reference count decreased.
  419. @param objectToRemove the object to try to remove
  420. @see remove, removeRange
  421. */
  422. void removeObject (ObjectClass* const objectToRemove)
  423. {
  424. const ScopedLockType lock (getLock());
  425. remove (indexOf (objectToRemove));
  426. }
  427. /** Removes a range of objects from the array.
  428. This will remove a set of objects, starting from the given index,
  429. and move any subsequent elements down to close the gap.
  430. If the range extends beyond the bounds of the array, it will
  431. be safely clipped to the size of the array.
  432. The objects that are removed will have their reference counts decreased,
  433. and may be deleted if not referenced from elsewhere.
  434. @param startIndex the index of the first object to remove
  435. @param numberToRemove how many objects should be removed
  436. @see remove, removeObject
  437. */
  438. void removeRange (const int startIndex,
  439. const int numberToRemove)
  440. {
  441. const ScopedLockType lock (getLock());
  442. const int start = jlimit (0, numUsed, startIndex);
  443. const int end_ = jlimit (0, numUsed, startIndex + numberToRemove);
  444. if (end_ > start)
  445. {
  446. int i;
  447. for (i = start; i < end_; ++i)
  448. {
  449. if (data.elements[i] != nullptr)
  450. {
  451. data.elements[i]->decReferenceCount();
  452. data.elements[i] = nullptr; // (in case one of the destructors accesses this array and hits a dangling pointer)
  453. }
  454. }
  455. const int rangeSize = end_ - start;
  456. ObjectClass** e = data.elements + start;
  457. i = numUsed - end_;
  458. numUsed -= rangeSize;
  459. while (--i >= 0)
  460. {
  461. *e = e [rangeSize];
  462. ++e;
  463. }
  464. if ((numUsed << 1) < data.numAllocated)
  465. minimiseStorageOverheads();
  466. }
  467. }
  468. /** Removes the last n objects from the array.
  469. The objects that are removed will have their reference counts decreased,
  470. and may be deleted if not referenced from elsewhere.
  471. @param howManyToRemove how many objects to remove from the end of the array
  472. @see remove, removeObject, removeRange
  473. */
  474. void removeLast (int howManyToRemove = 1)
  475. {
  476. const ScopedLockType lock (getLock());
  477. if (howManyToRemove > numUsed)
  478. howManyToRemove = numUsed;
  479. while (--howManyToRemove >= 0)
  480. remove (numUsed - 1);
  481. }
  482. /** Swaps a pair of objects in the array.
  483. If either of the indexes passed in is out-of-range, nothing will happen,
  484. otherwise the two objects at these positions will be exchanged.
  485. */
  486. void swap (const int index1,
  487. const int index2) noexcept
  488. {
  489. const ScopedLockType lock (getLock());
  490. if (isPositiveAndBelow (index1, numUsed)
  491. && isPositiveAndBelow (index2, numUsed))
  492. {
  493. std::swap (data.elements [index1],
  494. data.elements [index2]);
  495. }
  496. }
  497. /** Moves one of the objects to a different position.
  498. This will move the object to a specified index, shuffling along
  499. any intervening elements as required.
  500. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  501. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  502. @param currentIndex the index of the object to be moved. If this isn't a
  503. valid index, then nothing will be done
  504. @param newIndex the index at which you'd like this object to end up. If this
  505. is less than zero, it will be moved to the end of the array
  506. */
  507. void move (const int currentIndex,
  508. int newIndex) noexcept
  509. {
  510. if (currentIndex != newIndex)
  511. {
  512. const ScopedLockType lock (getLock());
  513. if (isPositiveAndBelow (currentIndex, numUsed))
  514. {
  515. if (! isPositiveAndBelow (newIndex, numUsed))
  516. newIndex = numUsed - 1;
  517. ObjectClass* const value = data.elements [currentIndex];
  518. if (newIndex > currentIndex)
  519. {
  520. memmove (data.elements + currentIndex,
  521. data.elements + currentIndex + 1,
  522. (newIndex - currentIndex) * sizeof (ObjectClass*));
  523. }
  524. else
  525. {
  526. memmove (data.elements + newIndex + 1,
  527. data.elements + newIndex,
  528. (currentIndex - newIndex) * sizeof (ObjectClass*));
  529. }
  530. data.elements [newIndex] = value;
  531. }
  532. }
  533. }
  534. //==============================================================================
  535. /** This swaps the contents of this array with those of another array.
  536. If you need to exchange two arrays, this is vastly quicker than using copy-by-value
  537. because it just swaps their internal pointers.
  538. */
  539. void swapWithArray (ReferenceCountedArray& otherArray) noexcept
  540. {
  541. const ScopedLockType lock1 (getLock());
  542. const ScopedLockType lock2 (otherArray.getLock());
  543. data.swapWith (otherArray.data);
  544. std::swap (numUsed, otherArray.numUsed);
  545. }
  546. //==============================================================================
  547. /** Compares this array to another one.
  548. @returns true only if the other array contains the same objects in the same order
  549. */
  550. bool operator== (const ReferenceCountedArray& other) const noexcept
  551. {
  552. const ScopedLockType lock2 (other.getLock());
  553. const ScopedLockType lock1 (getLock());
  554. if (numUsed != other.numUsed)
  555. return false;
  556. for (int i = numUsed; --i >= 0;)
  557. if (data.elements [i] != other.data.elements [i])
  558. return false;
  559. return true;
  560. }
  561. /** Compares this array to another one.
  562. @see operator==
  563. */
  564. bool operator!= (const ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse>& other) const noexcept
  565. {
  566. return ! operator== (other);
  567. }
  568. //==============================================================================
  569. /** Sorts the elements in the array.
  570. This will use a comparator object to sort the elements into order. The object
  571. passed must have a method of the form:
  572. @code
  573. int compareElements (ElementType first, ElementType second);
  574. @endcode
  575. ..and this method must return:
  576. - a value of < 0 if the first comes before the second
  577. - a value of 0 if the two objects are equivalent
  578. - a value of > 0 if the second comes before the first
  579. To improve performance, the compareElements() method can be declared as static or const.
  580. @param comparator the comparator to use for comparing elements.
  581. @param retainOrderOfEquivalentItems if this is true, then items
  582. which the comparator says are equivalent will be
  583. kept in the order in which they currently appear
  584. in the array. This is slower to perform, but may
  585. be important in some cases. If it's false, a faster
  586. algorithm is used, but equivalent elements may be
  587. rearranged.
  588. @see sortArray
  589. */
  590. template <class ElementComparator>
  591. void sort (ElementComparator& comparator,
  592. const bool retainOrderOfEquivalentItems = false) const noexcept
  593. {
  594. (void) comparator; // if you pass in an object with a static compareElements() method, this
  595. // avoids getting warning messages about the parameter being unused
  596. const ScopedLockType lock (getLock());
  597. sortArray (comparator, data.elements.getData(), 0, size() - 1, retainOrderOfEquivalentItems);
  598. }
  599. //==============================================================================
  600. /** Reduces the amount of storage being used by the array.
  601. Arrays typically allocate slightly more storage than they need, and after
  602. removing elements, they may have quite a lot of unused space allocated.
  603. This method will reduce the amount of allocated storage to a minimum.
  604. */
  605. void minimiseStorageOverheads() noexcept
  606. {
  607. const ScopedLockType lock (getLock());
  608. data.shrinkToNoMoreThan (numUsed);
  609. }
  610. /** Increases the array's internal storage to hold a minimum number of elements.
  611. Calling this before adding a large known number of elements means that
  612. the array won't have to keep dynamically resizing itself as the elements
  613. are added, and it'll therefore be more efficient.
  614. */
  615. void ensureStorageAllocated (const int minNumElements)
  616. {
  617. const ScopedLockType lock (getLock());
  618. data.ensureAllocatedSize (minNumElements);
  619. }
  620. //==============================================================================
  621. /** Returns the CriticalSection that locks this array.
  622. To lock, you can call getLock().enter() and getLock().exit(), or preferably use
  623. an object of ScopedLockType as an RAII lock for it.
  624. */
  625. inline const TypeOfCriticalSectionToUse& getLock() const noexcept { return data; }
  626. /** Returns the type of scoped lock to use for locking this array */
  627. typedef typename TypeOfCriticalSectionToUse::ScopedLockType ScopedLockType;
  628. private:
  629. //==============================================================================
  630. ArrayAllocationBase <ObjectClass*, TypeOfCriticalSectionToUse> data;
  631. int numUsed;
  632. };
  633. #endif // __JUCE_REFERENCECOUNTEDARRAY_JUCEHEADER__