LMMS
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
73 LinkedListPointer& operator= (ObjectType* const newItem) noexcept
74 {
75 item = newItem;
76 return *this;
77 }
78
80 : item (other.item)
81 {
82 other.item = nullptr;
83 }
84
85 LinkedListPointer& operator= (LinkedListPointer&& other) noexcept
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
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 //==============================================================================
180 void insertNext (ObjectType* const newItem)
181 {
183 jassert (newItem != nullptr);
184 jassert (newItem->nextListItem == nullptr);
185 newItem->nextListItem = item;
186 item = newItem;
188 }
189
194 void insertAtIndex (int index, ObjectType* newItem)
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
211 ObjectType* replaceNext (ObjectType* const newItem) noexcept
212 {
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;
223 }
224
231 void append (ObjectType* const newItem)
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
255 ObjectType* removeNext() noexcept
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
271 void remove (ObjectType* const itemToRemove)
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
294 LinkedListPointer* findPointerTo (ObjectType* const itemToLookFor) noexcept
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 {
316 jassert (destArray != nullptr);
317
318 for (auto* i = item; i != nullptr; i = i->nextListItem)
319 *destArray++ = i;
320
322 }
323
325 void swapWith (LinkedListPointer& other) noexcept
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:
359
361 };
362
363private:
364 //==============================================================================
365 ObjectType* item;
366
368};
369
370} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Appender(LinkedListPointer &endOfListPointer) noexcept
Definition juce_LinkedListPointer.h:343
void append(ObjectType *const newItem) noexcept
Definition juce_LinkedListPointer.h:351
LinkedListPointer * endOfList
Definition juce_LinkedListPointer.h:358
LinkedListPointer & getLast() noexcept
Definition juce_LinkedListPointer.h:114
LinkedListPointer * findPointerTo(ObjectType *const itemToLookFor) noexcept
Definition juce_LinkedListPointer.h:294
ObjectType * replaceNext(ObjectType *const newItem) noexcept
Definition juce_LinkedListPointer.h:211
LinkedListPointer(LinkedListPointer &&other) noexcept
Definition juce_LinkedListPointer.h:79
void append(ObjectType *const newItem)
Definition juce_LinkedListPointer.h:231
void deleteAll()
Definition juce_LinkedListPointer.h:280
bool contains(const ObjectType *const itemToLookFor) const noexcept
Definition juce_LinkedListPointer.h:167
ObjectType * removeNext() noexcept
Definition juce_LinkedListPointer.h:255
LinkedListPointer(ObjectType *const headItem) noexcept
Definition juce_LinkedListPointer.h:67
void insertNext(ObjectType *const newItem)
Definition juce_LinkedListPointer.h:180
ObjectType * get() const noexcept
Definition juce_LinkedListPointer.h:102
void addCopyOfList(const LinkedListPointer &other)
Definition juce_LinkedListPointer.h:240
int size() const noexcept
Definition juce_LinkedListPointer.h:128
void insertAtIndex(int index, ObjectType *newItem)
Definition juce_LinkedListPointer.h:194
void copyToArray(ObjectType **destArray) const noexcept
Definition juce_LinkedListPointer.h:313
LinkedListPointer() noexcept
Definition juce_LinkedListPointer.h:61
void swapWith(LinkedListPointer &other) noexcept
Definition juce_LinkedListPointer.h:325
ObjectType * item
Definition juce_LinkedListPointer.h:365
void remove(ObjectType *const itemToRemove)
Definition juce_LinkedListPointer.h:271
int * l
Definition inflate.c:1579
register unsigned i
Definition inflate.c:1575
#define JUCE_BEGIN_IGNORE_WARNINGS_MSVC(warnings)
Definition juce_CompilerWarnings.h:198
#define JUCE_END_IGNORE_WARNINGS_MSVC
Definition juce_CompilerWarnings.h:199
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition carla_juce.cpp:31
#define const
Definition zconf.h:137