JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_LinkedListPointer.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
55template <class ObjectType>
57{
58public:
59 //==============================================================================
62 : item (nullptr)
63 {
64 }
65
67 explicit LinkedListPointer (ObjectType* const headItem) noexcept
68 : item (headItem)
69 {
70 }
71
74 {
75 item = newItem;
76 return *this;
77 }
78
80 : item (other.item)
81 {
82 other.item = nullptr;
83 }
84
86 {
87 jassert (this != &other); // hopefully the compiler should make this situation impossible!
88
89 item = other.item;
90 other.item = nullptr;
91 return *this;
92 }
93
94 //==============================================================================
96 inline operator ObjectType*() const noexcept
97 {
98 return item;
99 }
100
102 inline ObjectType* get() const noexcept
103 {
104 return item;
105 }
106
115 {
116 auto* l = this;
117
118 while (l->item != nullptr)
119 l = &(l->item->nextListItem);
120
121 return *l;
122 }
123
128 int size() const noexcept
129 {
130 int total = 0;
131
132 for (auto* i = item; i != nullptr; i = i->nextListItem)
133 ++total;
134
135 return total;
136 }
137
142 LinkedListPointer& operator[] (int index) noexcept
143 {
144 auto* l = this;
145
146 while (--index >= 0 && l->item != nullptr)
147 l = &(l->item->nextListItem);
148
149 return *l;
150 }
151
156 const LinkedListPointer& operator[] (int index) const noexcept
157 {
158 auto* l = this;
159
160 while (--index >= 0 && l->item != nullptr)
161 l = &(l->item->nextListItem);
162
163 return *l;
164 }
165
167 bool contains (const ObjectType* const itemToLookFor) const noexcept
168 {
169 for (auto* i = item; i != nullptr; i = i->nextListItem)
170 if (itemToLookFor == i)
171 return true;
172
173 return false;
174 }
175
176 //==============================================================================
181 {
182 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
183 jassert (newItem != nullptr);
184 jassert (newItem->nextListItem == nullptr);
185 newItem->nextListItem = item;
186 item = newItem;
187 JUCE_END_IGNORE_WARNINGS_MSVC
188 }
189
195 {
196 jassert (newItem != nullptr);
197 auto* l = this;
198
199 while (index != 0 && l->item != nullptr)
200 {
201 l = &(l->item->nextListItem);
202 --index;
203 }
204
205 l->insertNext (newItem);
206 }
207
212 {
213 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011 28182)
214 jassert (newItem != nullptr);
215 jassert (newItem->nextListItem == nullptr);
216
217 auto oldItem = item;
218 item = newItem;
219 item->nextListItem = oldItem->nextListItem.item;
220 oldItem->nextListItem.item = nullptr;
221 return oldItem;
222 JUCE_END_IGNORE_WARNINGS_MSVC
223 }
224
232 {
233 getLast().item = newItem;
234 }
235
241 {
242 auto* insertPoint = this;
243
244 for (auto* i = other.item; i != nullptr; i = i->nextListItem)
245 {
246 insertPoint->insertNext (new ObjectType (*i));
247 insertPoint = &(insertPoint->item->nextListItem);
248 }
249 }
250
256 {
257 auto oldItem = item;
258
259 if (oldItem != nullptr)
260 {
261 item = oldItem->nextListItem;
262 oldItem->nextListItem.item = nullptr;
263 }
264
265 return oldItem;
266 }
267
272 {
273 if (auto* l = findPointerTo (itemToRemove))
274 l->removeNext();
275 }
276
281 {
282 while (item != nullptr)
283 {
284 auto oldItem = item;
285 item = oldItem->nextListItem;
286 delete oldItem;
287 }
288 }
289
295 {
296 auto* l = this;
297
298 while (l->item != nullptr)
299 {
300 if (l->item == itemToLookFor)
301 return l;
302
303 l = &(l->item->nextListItem);
304 }
305
306 return nullptr;
307 }
308
313 void copyToArray (ObjectType** destArray) const noexcept
314 {
315 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
316 jassert (destArray != nullptr);
317
318 for (auto* i = item; i != nullptr; i = i->nextListItem)
319 *destArray++ = i;
320
321 JUCE_END_IGNORE_WARNINGS_MSVC
322 }
323
326 {
327 std::swap (item, other.item);
328 }
329
330 //==============================================================================
339 {
340 public:
344 : endOfList (&endOfListPointer)
345 {
346 // This can only be used to add to the end of a list.
347 jassert (endOfListPointer.item == nullptr);
348 }
349
351 void append (ObjectType* const newItem) noexcept
352 {
353 *endOfList = newItem;
354 endOfList = &(newItem->nextListItem);
355 }
356
357 private:
358 LinkedListPointer* endOfList;
359
361 };
362
363private:
364 //==============================================================================
365 ObjectType* item;
366
368};
369
370} // namespace juce
Allows efficient repeated insertions into a list.
Appender(LinkedListPointer &endOfListPointer) noexcept
Creates an appender which will add items to the given list.
void append(ObjectType *const newItem) noexcept
Appends an item to the list.
Helps to manipulate singly-linked lists of objects.
LinkedListPointer & getLast() noexcept
Returns the last item in the list which this pointer points to.
LinkedListPointer * findPointerTo(ObjectType *const itemToLookFor) noexcept
Finds a pointer to a given item.
LinkedListPointer & operator[](int index) noexcept
Returns the item at a given index in the list.
ObjectType * replaceNext(ObjectType *const newItem) noexcept
Replaces the object that this pointer points to, appending the rest of the list to the new object,...
void append(ObjectType *const newItem)
Adds an item to the end of the list.
void deleteAll()
Iterates the list, calling the delete operator on all of its elements and leaving this pointer empty.
LinkedListPointer & operator=(ObjectType *const newItem) noexcept
Sets this pointer to point to a new list.
bool contains(const ObjectType *const itemToLookFor) const noexcept
Returns true if the list contains the given item.
ObjectType * removeNext() noexcept
Removes the head item from the list.
LinkedListPointer(ObjectType *const headItem) noexcept
Creates a pointer to a list whose head is the item provided.
void insertNext(ObjectType *const newItem)
Inserts an item into the list, placing it before the item that this pointer currently points to.
ObjectType * get() const noexcept
Returns the item which this pointer points to.
void addCopyOfList(const LinkedListPointer &other)
Creates copies of all the items in another list and adds them to this one.
int size() const noexcept
Returns the number of items in the list.
void insertAtIndex(int index, ObjectType *newItem)
Inserts an item at a numeric index in the list.
void copyToArray(ObjectType **destArray) const noexcept
Copies the items in the list to an array.
LinkedListPointer() noexcept
Creates a null pointer to an empty list.
void swapWith(LinkedListPointer &other) noexcept
Swaps this pointer with another one.
void remove(ObjectType *const itemToRemove)
Removes a specific item from the list.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T swap(T... args)