OpenShot Audio Library | OpenShotAudio  0.3.1
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 
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  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 
356 private:
357  //==============================================================================
358  ObjectType* item;
359 
360  JUCE_DECLARE_NON_COPYABLE (LinkedListPointer)
361 };
362 
363 } // 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)