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