// Programmed by Anthony Barbachan // Programmed in Turbo C++ 3.0 // Note: This is the header file for the object library Queue // Purpose: To provide a Queue for ObjectBase associated data type. // Version 1.00 // Last Modified On 7/10/95 // Version 1.10 // Last Modified On 12/30/97 // Purpose: Added a Peek function for the rear object #ifndef __QUEUE_HPP__ #define __QUEUE_HPP__ #include #include "object.hpp" ////////////////////////////////////////////////////////////////////////////// // This class provides a Queue for any ObjectBase associated object. template class QueueOf { protected: ObjectType* front; ObjectType* back; unsigned long count; public: QueueOf() { front = NULL; count = 0ul; } ~QueueOf() { Flush(); } int Put(ObjectType* obj); ObjectType* Get(void); void Flush(void); ObjectType* Peek() { return front; } ObjectType* RearPeek() { return back; } int IsEmpty() { return front == NULL; } int NotEmpty() { return front != NULL; } int IsNotEmpty() { return front != NULL; } unsigned long NumObjects() { return count; } int operator ! () { return IsEmpty(); } }; ///////////////////////////////////////*************************************** ///////////////////////////////////////*************************************** // This function Puts an object onto the queue template int QueueOf::Put(ObjectType* obj) { if((count < ULONG_MAX) && (obj != NULL)) { if(NotEmpty()) // If the Queue is not empty { back->LinkNext(obj); // Link obj after the last object back = obj; // Make obj the new last object } else // If the Queue is empty front = back = obj; // Start a Queue with Object obj count++; // Incriment counter return 1; } else // If unable to complete operation return 0; } ///////////////////////////////////////*************************************** // This function Gets an object from the Queue template ObjectType* QueueOf::Get() { if(NotEmpty()) // If the Queue is not empty { ObjectType* temp = front; // Save address of the first object front = (ObjectType *) temp->UnLinkNext(); // Unlink old front and set new front count--; // Deincriment counter return temp; // Return old first object } else // If the Queue is empty return NULL; } ///////////////////////////////////////*************************************** template void QueueOf::Flush() { ObjectType* temp = NULL; while(front != NULL) // While the Queue is not empty { temp = front; // Save address of the first object front = (ObjectType *) temp->UnLinkNext(); // Unlink old front and set new front delete temp; } back = NULL; // Secure back pointer count = 0ul; // Reset counter } ///////////////////////////////////////*************************************** #endif