openshot-audio  0.1.6
juce_LinkedListPointer.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_LINKEDLISTPOINTER_H_INCLUDED
30 #define JUCE_LINKEDLISTPOINTER_H_INCLUDED
31 
32 
33 //==============================================================================
60 template <class ObjectType>
62 {
63 public:
64  //==============================================================================
67  : item (nullptr)
68  {
69  }
70 
72  explicit LinkedListPointer (ObjectType* const headItem) noexcept
73  : item (headItem)
74  {
75  }
76 
78  LinkedListPointer& operator= (ObjectType* const newItem) noexcept
79  {
80  item = newItem;
81  return *this;
82  }
83 
84  #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
86  : item (other.item)
87  {
88  other.item = nullptr;
89  }
90 
92  {
93  jassert (this != &other); // hopefully the compiler should make this situation impossible!
94 
95  item = other.item;
96  other.item = nullptr;
97  return *this;
98  }
99  #endif
100 
101  //==============================================================================
103  inline operator ObjectType*() const noexcept
104  {
105  return item;
106  }
107 
109  inline ObjectType* get() const noexcept
110  {
111  return item;
112  }
113 
122  {
123  LinkedListPointer* l = this;
124 
125  while (l->item != nullptr)
126  l = &(l->item->nextListItem);
127 
128  return *l;
129  }
130 
136  {
137  int total = 0;
138 
139  for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
140  ++total;
141 
142  return total;
143  }
144 
150  {
151  LinkedListPointer* l = this;
152 
153  while (--index >= 0 && l->item != nullptr)
154  l = &(l->item->nextListItem);
155 
156  return *l;
157  }
158 
163  const LinkedListPointer& operator[] (int index) const noexcept
164  {
165  const LinkedListPointer* l = this;
166 
167  while (--index >= 0 && l->item != nullptr)
168  l = &(l->item->nextListItem);
169 
170  return *l;
171  }
172 
174  bool contains (const ObjectType* const itemToLookFor) const noexcept
175  {
176  for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
177  if (itemToLookFor == i)
178  return true;
179 
180  return false;
181  }
182 
183  //==============================================================================
187  void insertNext (ObjectType* const newItem)
188  {
189  jassert (newItem != nullptr);
190  jassert (newItem->nextListItem == nullptr);
191  newItem->nextListItem = item;
192  item = newItem;
193  }
194 
199  void insertAtIndex (int index, ObjectType* newItem)
200  {
201  jassert (newItem != nullptr);
202  LinkedListPointer* l = this;
203 
204  while (index != 0 && l->item != nullptr)
205  {
206  l = &(l->item->nextListItem);
207  --index;
208  }
209 
210  l->insertNext (newItem);
211  }
212 
216  ObjectType* replaceNext (ObjectType* const newItem) noexcept
217  {
218  jassert (newItem != nullptr);
219  jassert (newItem->nextListItem == nullptr);
220 
221  ObjectType* const oldItem = item;
222  item = newItem;
223  item->nextListItem = oldItem->nextListItem.item;
224  oldItem->nextListItem.item = nullptr;
225  return oldItem;
226  }
227 
234  void append (ObjectType* const newItem)
235  {
236  getLast().item = newItem;
237  }
238 
243  void addCopyOfList (const LinkedListPointer& other)
244  {
245  LinkedListPointer* insertPoint = this;
246 
247  for (ObjectType* i = other.item; i != nullptr; i = i->nextListItem)
248  {
249  insertPoint->insertNext (new ObjectType (*i));
250  insertPoint = &(insertPoint->item->nextListItem);
251  }
252  }
253 
258  ObjectType* removeNext() noexcept
259  {
260  ObjectType* const oldItem = item;
261 
262  if (oldItem != nullptr)
263  {
264  item = oldItem->nextListItem;
265  oldItem->nextListItem.item = nullptr;
266  }
267 
268  return oldItem;
269  }
270 
274  void remove (ObjectType* const itemToRemove)
275  {
276  if (LinkedListPointer* const l = findPointerTo (itemToRemove))
277  l->removeNext();
278  }
279 
283  void deleteAll()
284  {
285  while (item != nullptr)
286  {
287  ObjectType* const oldItem = item;
288  item = oldItem->nextListItem;
289  delete oldItem;
290  }
291  }
292 
297  LinkedListPointer* findPointerTo (ObjectType* const itemToLookFor) noexcept
298  {
299  LinkedListPointer* l = this;
300 
301  while (l->item != nullptr)
302  {
303  if (l->item == itemToLookFor)
304  return l;
305 
306  l = &(l->item->nextListItem);
307  }
308 
309  return nullptr;
310  }
311 
316  void copyToArray (ObjectType** destArray) const noexcept
317  {
318  jassert (destArray != nullptr);
319 
320  for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
321  *destArray++ = i;
322  }
323 
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 
361  };
362 
363 private:
364  //==============================================================================
365  ObjectType* item;
366 
368 };
369 
370 
371 #endif // JUCE_LINKEDLISTPOINTER_H_INCLUDED
LinkedListPointer(ObjectType *const headItem) noexcept
Definition: juce_LinkedListPointer.h:72
LinkedListPointer & operator[](int index) noexcept
Definition: juce_LinkedListPointer.h:149
bool contains(const ObjectType *const itemToLookFor) const noexcept
Definition: juce_LinkedListPointer.h:174
#define noexcept
Definition: juce_CompilerSupport.h:141
void insertNext(ObjectType *const newItem)
Definition: juce_LinkedListPointer.h:187
LinkedListPointer * findPointerTo(ObjectType *const itemToLookFor) noexcept
Definition: juce_LinkedListPointer.h:297
void copyToArray(ObjectType **destArray) const noexcept
Definition: juce_LinkedListPointer.h:316
int size() const noexcept
Definition: juce_LinkedListPointer.h:135
#define const
Definition: juce_LinkedListPointer.h:61
ObjectType * replaceNext(ObjectType *const newItem) noexcept
Definition: juce_LinkedListPointer.h:216
void append(ObjectType *const newItem)
Definition: juce_LinkedListPointer.h:234
void insertAtIndex(int index, ObjectType *newItem)
Definition: juce_LinkedListPointer.h:199
Definition: juce_LinkedListPointer.h:338
LinkedListPointer & operator=(ObjectType *const newItem) noexcept
Definition: juce_LinkedListPointer.h:78
ObjectType * removeNext() noexcept
Definition: juce_LinkedListPointer.h:258
Appender(LinkedListPointer &endOfListPointer) noexcept
Definition: juce_LinkedListPointer.h:343
LinkedListPointer & getLast() noexcept
Definition: juce_LinkedListPointer.h:121
void append(ObjectType *const newItem) noexcept
Definition: juce_LinkedListPointer.h:351
#define jassert(a)
Definition: juce_PlatformDefs.h:146
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
void deleteAll()
Definition: juce_LinkedListPointer.h:283
LinkedListPointer() noexcept
Definition: juce_LinkedListPointer.h:66
void addCopyOfList(const LinkedListPointer &other)
Definition: juce_LinkedListPointer.h:243
void swapWith(LinkedListPointer &other) noexcept
Definition: juce_LinkedListPointer.h:325