X-Git-Url: https://jfr.im/git/irc/gameservirc.git/blobdiff_plain/85ce9d3ecb3e6888dae590b3a8e347e0a2131bd9..bb668fcf666c70130d77a458e7d3d805e1694b70:/gameserv/list.h diff --git a/gameserv/list.h b/gameserv/list.h index e9e5656..e51d6f5 100644 --- a/gameserv/list.h +++ b/gameserv/list.h @@ -1,10 +1,16 @@ #ifndef LIST_H #define LIST_H -#include +#include #include #include "listnode.h" #include "aClient.h" +#include "extern.h" +#include "options.h" + +using std::cout; +using std::endl; +using std::flush; template class List { @@ -18,6 +24,7 @@ class List { bool remove( T * ); bool isEmpty() const; void print() const; + ListNode < T > *Find( T * ); ListNode < T > *First() { return firstPtr; }; ListNode < T > *Last() { return lastPtr; }; private: @@ -35,20 +42,26 @@ List::~List() { if (!isEmpty()) { - cout << "Destroying Nodes" << endl; + #ifdef DEBUGMODE + log("Destroying Nodes"); + #endif ListNode *currentPtr = firstPtr, *tempPtr; - while (currentPtr != 0) + while (currentPtr) { tempPtr = currentPtr; - currentPtr = currentPtr->next; -// if (!tempPtr->getData()->stats || tempPtr->getData()->stats->started == 0) + currentPtr = currentPtr->Next(); + + #ifdef DEBUGMODE + log("Deleting Memory address: %s", tempPtr->getData()); + #endif + delete tempPtr; -// else -// tempPtr->getData()->stats->started = 0; } - cout << "All Nodes destroyed" << endl; + #ifdef DEBUGMODE + log("All Nodes destroyed"); + #endif } } @@ -129,7 +142,7 @@ bool List::removeFromBack( T &value ) template bool List::isEmpty() const - { return firstPtr == 0; } + { return firstPtr == 0 && lastPtr == 0; } template ListNode *List::getNewNode( const T &value) @@ -140,7 +153,23 @@ ListNode *List::getNewNode( const T &value) return ptr; } +template +ListNode *List::Find( T *value ) +{ + if (isEmpty()) {return NULL;} + + ListNode *currentPtr; + currentPtr = firstPtr; + while (currentPtr) + { + if (currentPtr->getData() == value) + return currentPtr; + currentPtr = currentPtr->Next(); + } + return NULL; +} +#ifdef DEBUGMODE template void List::print() const { @@ -154,16 +183,16 @@ void List::print() const currentPtr = firstPtr; while (currentPtr) { - cout << "aClient: " << *currentPtr->getData(); + cout << "aClient: " << *currentPtr->getData() << flush; if (currentPtr->getData()->stats) - cout << " Player Name:" << currentPtr->getData()->stats->name; + cout << " Player Name: " << currentPtr->getData()->stats->name + << " Password: " << currentPtr->getData()->stats->password << flush; cout << endl; currentPtr = currentPtr->next; } - - cout << endl; } +#endif template bool List::remove(T *remPtr) @@ -171,6 +200,9 @@ bool List::remove(T *remPtr) ListNode *newPtr = firstPtr; T *testPtr; + if (isEmpty()) + return false; + while (newPtr) { testPtr = newPtr->getData(); @@ -178,12 +210,18 @@ bool List::remove(T *remPtr) { if (firstPtr == lastPtr) { - firstPtr = lastPtr = 0; + #ifdef DEBUGMODE + log("One Element. Deleting it"); + #endif + firstPtr = lastPtr = NULL; delete newPtr; return true; } else if (newPtr != lastPtr && newPtr != firstPtr) { + #ifdef DEBUGMODE + log("Many elements, this one is in the middle. Deleting it, linking front to back, and back to front."); + #endif newPtr->prev->next = newPtr->next; newPtr->next->prev = newPtr->prev; delete newPtr; @@ -191,6 +229,9 @@ bool List::remove(T *remPtr) } else if (newPtr == lastPtr) { + #ifdef DEBUGMODE + log("This was the last element. Deleting it, and pointing the tail to its previous element."); + #endif lastPtr = newPtr->prev; lastPtr->next = 0; delete newPtr; @@ -198,6 +239,9 @@ bool List::remove(T *remPtr) } else if (newPtr == firstPtr) { + #ifdef DEBUGMODE + log("This was the first element. Deleting it, and pointing the head to its next element."); + #endif firstPtr = newPtr->next; firstPtr->prev = 0; delete newPtr;