//============================================================================= // fifo.hpp // Simple FIFO queue template classes. // // Usage // To define a first-in/first-out (FIFO) queue containing pointers to // objects of type 'Elem': // // class Elem { ... }; // The queue element type // // DrtFifo q; // A FIFO of pointers to Elem items // // To insert an item onto the head of FIFO queue 'q': // // Elem item; // An item // // q.put(&item); // Insert 'item' at head of FIFO // // To retrieve (and remove) the (oldest) item from the tail of the FIFO // queue: // // Elem * ep; // Pointer to an item // // ep = q.get(); // Remove item from tail of FIFO // if (ep == NULL) // Check for empty FIFO // ...FIFO is empty... // // To check for an empty queue: // // if (q.isEmpty()) // ...FIFO is empty... // // To determine the number of items currently in the queue: // // int n; // // n = q.size(); // Number of items in FIFO // // To empty the queue, removing all items (i.e., all pointers) from it: // // q.clear(); // Empty the FIFO // // Caveats // The type of the elements in a queue (type 'Elem') must be a pointer // type. // // History // 1.00, 1998-10-17, David R Tribble. // First cut. // // Author // David R. Tribble // dtribble@technologist.com // http://www.flash.net/~dtribble // // Copyright ©1998 by David R. Tribble, all rights reserved. // Permission is hereby granted to use, distribute, and modify this source // code provided that the original copyright and authorship notices remain // intact. //----------------------------------------------------------------------------- #ifndef drt_fifo_hpp #define drt_fifo_hpp 1 // Identification #ifndef NO_H_IDENT static const char drt_fifo_hpp_id[] = "@(#)fifo.hpp 1.00 1998-10-17 dtribble"; #endif // System includes #ifndef NULL #include #endif // Win32 DLL gibberish #undef DRTDECL #ifdef _WIN32 #ifdef drt_fifo_lib #define DRTDECL __declspec(dllexport) #else #define DRTDECL __declspec(dllimport) #endif #else #define DRTDECL #endif // Namespace #if !NO_NAMESPACES namespace Drt { #endif //----------------------------------------------------------------------------- // class DrtFifoImpl // Helper class for class DrtFifo. // // Notes // This class does all of the actual work for the member functions of // class DrtFifo. Thus, all template class instantiations share the same // code, and use only a small amount of inline code per instantiation. // // This class only contains shared (static) member functions and has no // data (variable) members. // // Class DrtFifo inherits this class privately, so that clients of class // DrtFifo cannot access any of the member functions of this class. //----------------------------------------------------------------------------- #define DrtFifoImpl_VERS 100 // Class version class DRTDECL DrtFifoImpl { public: // Shared constants static const int VERS; // Class version protected: // Types typedef void * ElemPtr; // Generic item (pointer) type protected: // Functions /*void*/ ~DrtFifoImpl(); // Destructor /*DrtFifoImpl*/ DrtFifoImpl(); // Default constructor /*DrtFifoImpl*/ DrtFifoImpl(const DrtFifoImpl &r); // Copy constructor bool put(ElemPtr); // Add an item to queue ElemPtr get(); // Remove an item from queue unsigned size() const; // Get number of items in queue bool isEmpty() const; // Check for empty queue void clear(); // Empty the queue bool copy(const DrtFifoImpl &r); // Copy another queue private: // Types //------------------------------------------------------------------------- // struct Bucket // Contains a single element in the queue. //------------------------------------------------------------------------- #define DrtFifoImpl_Bucket_VERS 100 // Struct version struct Bucket // Container for a queue item { public: // Variables ElemPtr m_elem; // A queue item Bucket * m_prev; // Previous bucket in the queue }; private: // Variables Bucket * m_head; // First queue bucket unsigned m_size; // Number of items in the queue private: // Functions // Constructors and destructors not provided const DrtFifoImpl & operator =(const DrtFifoImpl &r); // Assignment operator }; //----------------------------------------------------------------------------- // class DrtFifo [template] // Simple first-in/first-out (FIFO) queue template class. // // This template declares a FIFO queue containing elements of type 'Elem'. // // Usage // See the description at the top of this header file. // // Notes // This class inherits class DrtFifoImpl privately, so that clients of // class DrtFifo cannot access any of the member functions of class // DrtFifoImpl. // // Caveats // Type 'Elem' must be a pointer type. //----------------------------------------------------------------------------- #define DrtFifo_VERS DrtFifoImpl_VERS // Class version template class DRTDECL DrtFifo: private DrtFifoImpl { public: // Shared constants // /*inherited*/ static const int // VERS; // Class version public: // Functions inline /*void*/ ~DrtFifo(); // Destructor inline /*DrtFifo*/ DrtFifo(); // Default constructor inline /*DrtFifo*/ DrtFifo(const DrtFifo &r); // Copy constructor inline bool put(Elem e); // Add an item to queue inline Elem get(); // Remove an item from queue inline unsigned size() const; // Get number of items in queue inline bool isEmpty() const; // Check for empty queue inline void clear(); // Empty the queue public: // Operators inline const DrtFifo & operator =(const DrtFifo &r); // Assignment operator private: // Variables // (None) private: // Functions // (None) }; //----------------------------------------------------------------------------- // Inline member functions //----------------------------------------------------------------------------- template inline /*void*/ DrtFifo::~DrtFifo() { // Destructor // Do nothing } template inline /*DrtFifo*/ DrtFifo::DrtFifo(): DrtFifoImpl() { // Default constructor // Do nothing } template inline /*DrtFifo*/ DrtFifo::DrtFifo(const DrtFifo &r): DrtFifoImpl(r) { // Copy constructor // Do nothing } template inline const DrtFifo & DrtFifo::operator =(const DrtFifo &r) { // Assignment operator copy(r); return (*this); } template inline bool DrtFifo::put(Elem e) { // Add 'e' to head of queue return (put(static_cast(e))); } template inline Elem DrtFifo::get() { // Retrieve and remove tail of queue return (static_cast(get())); } template inline unsigned DrtFifo::size() const { // Determine number of items in the queue return (size()); } template inline bool DrtFifo::isEmpty() const { // Determine if queue is empty return (isEmpty()); } template inline void DrtFifo::clear() { // Empty the queue clear(); } // Namespace #if !NO_NAMESPACES } // namespace Drt #endif // Win32 DLL gibberish #ifdef _WIN32 #undef DRTDECL #define DRTDECL __declspec(dllimport) #endif #endif // drt_fifo_hpp // Settings for Emacs: // Local Variables: // tab-width: 8 // End: // End fifo.hpp