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.

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