Loading...
Searching...
No Matches
juce_LinkedListPointer.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
template <class ObjectType>
class LinkedListPointer
{
public:
//==============================================================================
LinkedListPointer() noexcept
: item (nullptr)
{
}
explicit LinkedListPointer (ObjectType* const headItem) noexcept
: item (headItem)
{
}
LinkedListPointer& operator= (ObjectType* const newItem) noexcept
{
item = newItem;
return *this;
}
LinkedListPointer (LinkedListPointer&& other) noexcept
: item (other.item)
{
other.item = nullptr;
}
LinkedListPointer& operator= (LinkedListPointer&& other) noexcept
{
jassert (this != &other); // hopefully the compiler should make this situation impossible!
item = other.item;
other.item = nullptr;
return *this;
}
//==============================================================================
inline operator ObjectType*() const noexcept
{
return item;
}
inline ObjectType* get() const noexcept
{
return item;
}
LinkedListPointer& getLast() noexcept
{
auto* l = this;
while (l->item != nullptr)
l = &(l->item->nextListItem);
return *l;
}
int size() const noexcept
{
int total = 0;
for (auto* i = item; i != nullptr; i = i->nextListItem)
++total;
return total;
}
LinkedListPointer& operator[] (int index) noexcept
{
auto* l = this;
while (--index >= 0 && l->item != nullptr)
l = &(l->item->nextListItem);
return *l;
}
const LinkedListPointer& operator[] (int index) const noexcept
{
auto* l = this;
while (--index >= 0 && l->item != nullptr)
l = &(l->item->nextListItem);
return *l;
}
bool contains (const ObjectType* const itemToLookFor) const noexcept
{
for (auto* i = item; i != nullptr; i = i->nextListItem)
if (itemToLookFor == i)
return true;
return false;
}
//==============================================================================
void insertNext (ObjectType* const newItem)
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
jassert (newItem != nullptr);
jassert (newItem->nextListItem == nullptr);
newItem->nextListItem = item;
item = newItem;
JUCE_END_IGNORE_WARNINGS_MSVC
}
void insertAtIndex (int index, ObjectType* newItem)
{
jassert (newItem != nullptr);
auto* l = this;
while (index != 0 && l->item != nullptr)
{
l = &(l->item->nextListItem);
--index;
}
l->insertNext (newItem);
}
ObjectType* replaceNext (ObjectType* const newItem) noexcept
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011 28182)
jassert (newItem != nullptr);
jassert (newItem->nextListItem == nullptr);
auto oldItem = item;
item = newItem;
item->nextListItem = oldItem->nextListItem.item;
oldItem->nextListItem.item = nullptr;
return oldItem;
JUCE_END_IGNORE_WARNINGS_MSVC
}
void append (ObjectType* const newItem)
{
getLast().item = newItem;
}
void addCopyOfList (const LinkedListPointer& other)
{
auto* insertPoint = this;
for (auto* i = other.item; i != nullptr; i = i->nextListItem)
{
insertPoint->insertNext (new ObjectType (*i));
insertPoint = &(insertPoint->item->nextListItem);
}
}
ObjectType* removeNext() noexcept
{
auto oldItem = item;
if (oldItem != nullptr)
{
item = oldItem->nextListItem;
oldItem->nextListItem.item = nullptr;
}
return oldItem;
}
void remove (ObjectType* const itemToRemove)
{
if (auto* l = findPointerTo (itemToRemove))
l->removeNext();
}
void deleteAll()
{
while (item != nullptr)
{
auto oldItem = item;
item = oldItem->nextListItem;
delete oldItem;
}
}
LinkedListPointer* findPointerTo (ObjectType* const itemToLookFor) noexcept
{
auto* l = this;
while (l->item != nullptr)
{
if (l->item == itemToLookFor)
return l;
l = &(l->item->nextListItem);
}
return nullptr;
}
void copyToArray (ObjectType** destArray) const noexcept
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
jassert (destArray != nullptr);
for (auto* i = item; i != nullptr; i = i->nextListItem)
*destArray++ = i;
JUCE_END_IGNORE_WARNINGS_MSVC
}
void swapWith (LinkedListPointer& other) noexcept
{
std::swap (item, other.item);
}
//==============================================================================
class Appender
{
public:
Appender (LinkedListPointer& endOfListPointer) noexcept
: endOfList (&endOfListPointer)
{
// This can only be used to add to the end of a list.
jassert (endOfListPointer.item == nullptr);
}
void append (ObjectType* const newItem) noexcept
{
*endOfList = newItem;
endOfList = &(newItem->nextListItem);
}
private:
LinkedListPointer* endOfList;
JUCE_DECLARE_NON_COPYABLE (Appender)
};
private:
//==============================================================================
ObjectType* item;
JUCE_DECLARE_NON_COPYABLE (LinkedListPointer)
};
} // namespace juce
juce::LinkedListPointer::Appender
Definition juce_LinkedListPointer.h:339
juce::LinkedListPointer::Appender::Appender
Appender(LinkedListPointer &endOfListPointer) noexcept
Definition juce_LinkedListPointer.h:343
juce::LinkedListPointer::Appender::append
void append(ObjectType *const newItem) noexcept
Definition juce_LinkedListPointer.h:351
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:294
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:211
juce::LinkedListPointer::append
void append(ObjectType *const newItem)
Definition juce_LinkedListPointer.h:231
juce::LinkedListPointer::deleteAll
void deleteAll()
Definition juce_LinkedListPointer.h:280
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:255
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:240
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:194
juce::LinkedListPointer::copyToArray
void copyToArray(ObjectType **destArray) const noexcept
Definition juce_LinkedListPointer.h:313
juce::LinkedListPointer::LinkedListPointer
LinkedListPointer() noexcept
Definition juce_LinkedListPointer.h:61
juce::LinkedListPointer::swapWith
void swapWith(LinkedListPointer &other) noexcept
Definition juce_LinkedListPointer.h:325
juce::LinkedListPointer::remove
void remove(ObjectType *const itemToRemove)
Definition juce_LinkedListPointer.h:271