OpenShot Audio Library | OpenShotAudio: juce_LinkedListPointer.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.1

Loading...
Searching...
No Matches
juce_LinkedListPointer.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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>
56 class LinkedListPointer
57{
58public:
59 //==============================================================================
61 LinkedListPointer() noexcept
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
79 LinkedListPointer (LinkedListPointer&& other) noexcept
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
114 LinkedListPointer& getLast() noexcept
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 //==============================================================================
180 void insertNext (ObjectType* const newItem)
181 {
182 jassert (newItem != nullptr);
183 jassert (newItem->nextListItem == nullptr);
184 newItem->nextListItem = item;
185 item = newItem;
186 }
187
192 void insertAtIndex (int index, ObjectType* newItem)
193 {
194 jassert (newItem != nullptr);
195 auto* l = this;
196
197 while (index != 0 && l->item != nullptr)
198 {
199 l = &(l->item->nextListItem);
200 --index;
201 }
202
203 l->insertNext (newItem);
204 }
205
209 ObjectType* replaceNext (ObjectType* const newItem) noexcept
210 {
211 jassert (newItem != nullptr);
212 jassert (newItem->nextListItem == nullptr);
213
214 auto oldItem = item;
215 item = newItem;
216 item->nextListItem = oldItem->nextListItem.item;
217 oldItem->nextListItem.item = nullptr;
218 return oldItem;
219 }
220
227 void append (ObjectType* const newItem)
228 {
229 getLast().item = newItem;
230 }
231
236 void addCopyOfList (const LinkedListPointer& other)
237 {
238 auto* insertPoint = this;
239
240 for (auto* i = other.item; i != nullptr; i = i->nextListItem)
241 {
242 insertPoint->insertNext (new ObjectType (*i));
243 insertPoint = &(insertPoint->item->nextListItem);
244 }
245 }
246
251 ObjectType* removeNext() noexcept
252 {
253 auto oldItem = item;
254
255 if (oldItem != nullptr)
256 {
257 item = oldItem->nextListItem;
258 oldItem->nextListItem.item = nullptr;
259 }
260
261 return oldItem;
262 }
263
267 void remove (ObjectType* const itemToRemove)
268 {
269 if (auto* l = findPointerTo (itemToRemove))
270 l->removeNext();
271 }
272
276 void deleteAll()
277 {
278 while (item != nullptr)
279 {
280 auto oldItem = item;
281 item = oldItem->nextListItem;
282 delete oldItem;
283 }
284 }
285
290 LinkedListPointer* findPointerTo (ObjectType* const itemToLookFor) noexcept
291 {
292 auto* l = this;
293
294 while (l->item != nullptr)
295 {
296 if (l->item == itemToLookFor)
297 return l;
298
299 l = &(l->item->nextListItem);
300 }
301
302 return nullptr;
303 }
304
309 void copyToArray (ObjectType** destArray) const noexcept
310 {
311 jassert (destArray != nullptr);
312
313 for (auto* i = item; i != nullptr; i = i->nextListItem)
314 *destArray++ = i;
315 }
316
318 void swapWith (LinkedListPointer& other) noexcept
319 {
320 std::swap (item, other.item);
321 }
322
323 //==============================================================================
331 class Appender
332 {
333 public:
336 Appender (LinkedListPointer& endOfListPointer) noexcept
337 : endOfList (&endOfListPointer)
338 {
339 // This can only be used to add to the end of a list.
340 jassert (endOfListPointer.item == nullptr);
341 }
342
344 void append (ObjectType* const newItem) noexcept
345 {
346 *endOfList = newItem;
347 endOfList = &(newItem->nextListItem);
348 }
349
350 private:
351 LinkedListPointer* endOfList;
352
353 JUCE_DECLARE_NON_COPYABLE (Appender)
354 };
355
356private:
357 //==============================================================================
358 ObjectType* item;
359
360 JUCE_DECLARE_NON_COPYABLE (LinkedListPointer)
361};
362
363} // namespace juce
juce::LinkedListPointer::Appender
Definition: juce_LinkedListPointer.h:332
juce::LinkedListPointer::Appender::Appender
Appender(LinkedListPointer &endOfListPointer) noexcept
Definition: juce_LinkedListPointer.h:336
juce::LinkedListPointer::Appender::append
void append(ObjectType *const newItem) noexcept
Definition: juce_LinkedListPointer.h:344
juce::LinkedListPointer
Definition: juce_LinkedListPointer.h:57
juce::LinkedListPointer::getLast
LinkedListPointer & getLast() noexcept
Definition: juce_LinkedListPointer.h:114
juce::LinkedListPointer::findPointerTo
LinkedListPointer * findPointerTo(ObjectType *const itemToLookFor) noexcept
Definition: juce_LinkedListPointer.h:290
juce::LinkedListPointer::operator[]
LinkedListPointer & operator[](int index) noexcept
Definition: juce_LinkedListPointer.h:142
juce::LinkedListPointer::replaceNext
ObjectType * replaceNext(ObjectType *const newItem) noexcept
Definition: juce_LinkedListPointer.h:209
juce::LinkedListPointer::append
void append(ObjectType *const newItem)
Definition: juce_LinkedListPointer.h:227
juce::LinkedListPointer::deleteAll
void deleteAll()
Definition: juce_LinkedListPointer.h:276
juce::LinkedListPointer::operator=
LinkedListPointer & operator=(ObjectType *const newItem) noexcept
Definition: juce_LinkedListPointer.h:73
juce::LinkedListPointer::contains
bool contains(const ObjectType *const itemToLookFor) const noexcept
Definition: juce_LinkedListPointer.h:167
juce::LinkedListPointer::removeNext
ObjectType * removeNext() noexcept
Definition: juce_LinkedListPointer.h:251
juce::LinkedListPointer::LinkedListPointer
LinkedListPointer(ObjectType *const headItem) noexcept
Definition: juce_LinkedListPointer.h:67
juce::LinkedListPointer::insertNext
void insertNext(ObjectType *const newItem)
Definition: juce_LinkedListPointer.h:180
juce::LinkedListPointer::get
ObjectType * get() const noexcept
Definition: juce_LinkedListPointer.h:102
juce::LinkedListPointer::addCopyOfList
void addCopyOfList(const LinkedListPointer &other)
Definition: juce_LinkedListPointer.h:236
juce::LinkedListPointer::size
int size() const noexcept
Definition: juce_LinkedListPointer.h:128
juce::LinkedListPointer::insertAtIndex
void insertAtIndex(int index, ObjectType *newItem)
Definition: juce_LinkedListPointer.h:192
juce::LinkedListPointer::copyToArray
void copyToArray(ObjectType **destArray) const noexcept
Definition: juce_LinkedListPointer.h:309
juce::LinkedListPointer::LinkedListPointer
LinkedListPointer() noexcept
Definition: juce_LinkedListPointer.h:61
juce::LinkedListPointer::swapWith
void swapWith(LinkedListPointer &other) noexcept
Definition: juce_LinkedListPointer.h:318
juce::LinkedListPointer::remove
void remove(ObjectType *const itemToRemove)
Definition: juce_LinkedListPointer.h:267