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.

822 lines
31KB

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