151 lines
2.4 KiB
C++
151 lines
2.4 KiB
C++
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#include "list.h"
|
||
|
|
||
|
ListBase::ListBase()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ListBase::~ListBase()
|
||
|
{
|
||
|
AutoMutex am(mutex);
|
||
|
while(!head.isEmpty())
|
||
|
head.getNext()->del();
|
||
|
}
|
||
|
|
||
|
void ListBase::add(const void* part)
|
||
|
{
|
||
|
ListEntryBase* entry = new ListEntryBase(part);
|
||
|
|
||
|
AutoMutex am(mutex);
|
||
|
entry->add(&head, head.getNext());
|
||
|
}
|
||
|
|
||
|
void ListBase::addTail(const void* part)
|
||
|
{
|
||
|
ListEntryBase* entry = new ListEntryBase(part);
|
||
|
|
||
|
AutoMutex am(mutex);
|
||
|
entry->add(head.getPrev(), &head);
|
||
|
}
|
||
|
|
||
|
void* ListBase::get()
|
||
|
{
|
||
|
AutoMutex am(mutex);
|
||
|
ListEntryBase* entry = head.getNext();
|
||
|
void* retval = entry->getPart();
|
||
|
|
||
|
if (!head.isEmpty()) {
|
||
|
entry->del();
|
||
|
delete entry;
|
||
|
}
|
||
|
return retval;
|
||
|
}
|
||
|
|
||
|
void* ListBase::getTail()
|
||
|
{
|
||
|
AutoMutex am(mutex);
|
||
|
ListEntryBase* entry = head.getPrev();
|
||
|
void* retval = entry->getPart();
|
||
|
|
||
|
if (!head.isEmpty()) {
|
||
|
entry->del();
|
||
|
delete entry;
|
||
|
}
|
||
|
return retval;
|
||
|
}
|
||
|
|
||
|
bool ListBase::isEmpty() const
|
||
|
{
|
||
|
AutoMutex am((Mutex&)mutex);
|
||
|
return head.isEmpty();
|
||
|
}
|
||
|
|
||
|
IteratorBase* ListBase::createIterator()
|
||
|
{
|
||
|
return new ListIteratorBase(this);
|
||
|
}
|
||
|
|
||
|
ListBase::ListEntryBase::ListEntryBase(const void* part)
|
||
|
: prev(this), next(this), part(part)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ListBase::ListEntryBase::~ListEntryBase()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void ListBase::ListEntryBase::add(ListEntryBase* prev_, ListEntryBase* next_)
|
||
|
{
|
||
|
this->next = next_;
|
||
|
this->prev = prev_;
|
||
|
next_->prev = this;
|
||
|
prev_->next = this;
|
||
|
}
|
||
|
|
||
|
void ListBase::ListEntryBase::del()
|
||
|
{
|
||
|
next->prev = prev;
|
||
|
prev->next = next;
|
||
|
next = prev = this;
|
||
|
}
|
||
|
|
||
|
bool ListBase::ListEntryBase::isEmpty() const
|
||
|
{
|
||
|
return (this->prev == this) && (this->next == this);
|
||
|
}
|
||
|
|
||
|
void* ListBase::ListEntryBase::getPart() const
|
||
|
{
|
||
|
// const void* -> void*
|
||
|
return (void*)part;
|
||
|
}
|
||
|
|
||
|
ListBase::ListEntryBase* ListBase::ListEntryBase::getPrev() const
|
||
|
{
|
||
|
return prev;
|
||
|
}
|
||
|
|
||
|
ListBase::ListEntryBase* ListBase::ListEntryBase::getNext() const
|
||
|
{
|
||
|
return next;
|
||
|
}
|
||
|
|
||
|
ListBase::ListIteratorBase::ListIteratorBase(ListBase* list)
|
||
|
: list(list)
|
||
|
{
|
||
|
list->mutex.lock();
|
||
|
reset();
|
||
|
}
|
||
|
|
||
|
ListBase::ListIteratorBase::~ListIteratorBase()
|
||
|
{
|
||
|
list->mutex.unlock();
|
||
|
}
|
||
|
|
||
|
bool ListBase::ListIteratorBase::hasNext()
|
||
|
{
|
||
|
return (pos->getNext() != &list->head);
|
||
|
}
|
||
|
|
||
|
void* ListBase::ListIteratorBase::next()
|
||
|
{
|
||
|
pos = pos->getNext();
|
||
|
return pos->getPart();
|
||
|
}
|
||
|
|
||
|
void ListBase::ListIteratorBase::remove()
|
||
|
{
|
||
|
ListEntryBase* tmp = pos->getPrev();
|
||
|
pos->del();
|
||
|
pos = tmp;
|
||
|
}
|
||
|
|
||
|
void ListBase::ListIteratorBase::reset()
|
||
|
{
|
||
|
pos = &list->head;
|
||
|
}
|
||
|
|