OpenShot Audio Library | OpenShotAudio  0.6.0
juce_LinkedListPointer.h
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 
23 namespace juce
24 {
25 
26 //==============================================================================
55 template <class ObjectType>
57 {
58 public:
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 
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  //==============================================================================
180  void insertNext (ObjectType* const newItem)
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 
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  {
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 
231  void append (ObjectType* const newItem)
232  {
233  getLast().item = newItem;
234  }
235 
240  void addCopyOfList (const LinkedListPointer& other)
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 
280  void deleteAll()
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  {
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 
325  void swapWith (LinkedListPointer& other) noexcept
326  {
327  std::swap (item, other.item);
328  }
329 
330  //==============================================================================
338  class Appender
339  {
340  public:
343  Appender (LinkedListPointer& endOfListPointer) noexcept
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 
360  JUCE_DECLARE_NON_COPYABLE (Appender)
361  };
362 
363 private:
364  //==============================================================================
365  ObjectType* item;
366 
367  JUCE_DECLARE_NON_COPYABLE (LinkedListPointer)
368 };
369 
370 } // namespace juce
Appender(LinkedListPointer &endOfListPointer) noexcept
void append(ObjectType *const newItem) noexcept
ObjectType * get() const noexcept
ObjectType * removeNext() noexcept
LinkedListPointer & operator[](int index) noexcept
void append(ObjectType *const newItem)
ObjectType * replaceNext(ObjectType *const newItem) noexcept
LinkedListPointer * findPointerTo(ObjectType *const itemToLookFor) noexcept
bool contains(const ObjectType *const itemToLookFor) const noexcept
LinkedListPointer & operator=(ObjectType *const newItem) noexcept
LinkedListPointer(ObjectType *const headItem) noexcept
void insertNext(ObjectType *const newItem)
void addCopyOfList(const LinkedListPointer &other)
void insertAtIndex(int index, ObjectType *newItem)
void copyToArray(ObjectType **destArray) const noexcept
void swapWith(LinkedListPointer &other) noexcept
LinkedListPointer & getLast() noexcept
void remove(ObjectType *const itemToRemove)