diff --git a/gamelist.h b/gamelist.h index c770d6d..01017f2 100644 --- a/gamelist.h +++ b/gamelist.h @@ -55,6 +55,8 @@ private: List buckets[MAX_BUCKETS]; int timeout; + +friend class GameList::GameListIterator; }; #endif // _GAMELIST_H_ diff --git a/list.h b/list.h index 76a8da6..2cd2efa 100644 --- a/list.h +++ b/list.h @@ -35,8 +35,13 @@ template class ListEntry; +template +class ListIterator; + + template class List { +friend class ListIterator; public: List() : head(0), tail(0) {} ~List() {} @@ -91,77 +96,78 @@ protected: List(const List& l); List& operator=(const List& l); - template - class ListIterator : public Iterator { - public: - ListIterator(List* list) - : list(list) - { - reset(); - } - - virtual ~ListIterator() {} - - virtual bool hasNext() - { - return (pos == NULL) ? (list->head != NULL) : (pos->next != NULL); - } - - virtual TT* next() - { - prev = pos; - pos = (pos == NULL) ? list->head : pos->next; - return (TT*)pos; - } - - virtual void remove() - { - // remove pre-head -> bail out - if (pos == NULL) - return; - - // remove first - if (pos == list->head) - list->head = pos->next; - - // remove middle - if (prev) - prev->next = pos->next; - - // remove last - if (pos == list->tail) - list->tail = (prev == NULL) ? NULL : prev; - - pos = prev; - } - - virtual void reset() - { - pos = NULL; - prev = NULL; - } - - protected: - ListIterator(const ListIterator& li); - ListIterator& operator=(const ListIterator& li); - - List* list; - - private: - ListEntry* pos; - ListEntry* prev; - }; - private: ListEntry *head; ListEntry *tail; }; +template +class ListIterator : public Iterator { +public: + ListIterator(List* list) + : list(list) + { + reset(); + } + + virtual ~ListIterator() {} + + virtual bool hasNext() + { + return (pos == NULL) ? (list->head != NULL) : (pos->next != NULL); + } + + virtual T* next() + { + prev = pos; + pos = (pos == NULL) ? list->head : pos->next; + return (T*)pos; + } + + virtual void remove() + { + // remove pre-head -> bail out + if (pos == NULL) + return; + + // remove first + if (pos == list->head) + list->head = pos->next; + + // remove middle + if (prev) + prev->next = pos->next; + + // remove last + if (pos == list->tail) + list->tail = (prev == NULL) ? NULL : prev; + + pos = prev; + } + + virtual void reset() + { + pos = NULL; + prev = NULL; + } + +protected: + ListIterator(const ListIterator& li); + ListIterator& operator=(const ListIterator& li); + + List* list; + +private: + ListEntry* pos; + ListEntry* prev; +}; + + template class ListEntry { friend class List; -friend class List::ListIterator; +friend class ListIterator; public: ListEntry() {} ~ListEntry() {} @@ -171,8 +177,13 @@ private: }; +template +class LockedListIterator; + + template class LockedList : private List { +friend class LockedListIterator; public: LockedList() {} ~LockedList() {} @@ -210,22 +221,23 @@ public: } private: - template - class LockedListIterator : public List::ListIterator { - public: - LockedListIterator(LockedList* list) - : List::ListIterator(list) - { - list->mutex.lock(); - } - - virtual ~LockedListIterator() - { - ((LockedList*)list)->mutex.unlock(); - } - }; - Mutex mutex; }; + +template +class LockedListIterator : public ListIterator { +public: + LockedListIterator(LockedList* list) + : ListIterator(list) + { + list->mutex.lock(); + } + + virtual ~LockedListIterator() + { + ((LockedList*)list)->mutex.unlock(); + } +}; + #endif //_LIST_H_