From: Dejan L. <dlo...@us...> - 2004-06-13 13:20:59
|
Update of /cvsroot/rtk/rtk/rtk In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16594 Added Files: Queue.h Log Message: Ordinary Queue class --- NEW FILE: Queue.h --- #ifndef _RTK_QUEUE_H_ #define _RTK_QUEUE_H_ 1 /** * * RTK * Fast and easy cross-platform GUI ToolKit. * * Copyright (C) 2001-200x RTK Development Team * * This library is free software; you can redistribute it and/or modify it * under the terms of the slightly modified (see the "EXCEPTION NOTICE" part * of RTK Library License) GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * and along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA . * * Also you should have received a copy of RTK Library License, if not please * write an e-mail to some of RTK authors (listed in file AUTHORS). * * Bug reports: bu...@rt... * Suggestions: rf...@rt... ***************************************************************************/ /** * $filename.$ext * $description * Authors (chronological order): * Dejan Lozanovic, null§rtk.cx * Contributors (chronological order): * $fname $lname, $email ***************************************************************************/ //--------------------------------------------------------------------------- #include "Export.h" #include "Mutex.h" #include "SList.h" // for SLNode struct //--------------------------------------------------------------------------- namespace Rtk { // Single-List Element type #define QueueNode SLNode /// S free data handler /// Called whenever element is removed from list. /// Parameter 'data' is pointer to data of removed element. typedef void (*FreeDataFunc)(void *data); /** * Queue or FIFO(first in, first out) class */ class RTK_API Queue { public: // --------- CONSTRUCTORS ---------------------------------- /** * Default constructor. * @param sort is order ascendencig or descendencing */ Queue(): _count(0), _head(0), _tail(0), _free_func(0), _mutex(0) { } // --------- DESTRUCTORS ----------------------------------- /** * Default destructor. */ ~Queue(); // --------- GET METHODS ----------------------------------- /** * Returns number of items in the list. */ int GetCount() const { return this->_count; } /** * Returns pointer to _head node in Queue. * @return QueueNode pointer to first node in Queue. */ QueueNode* GetFirst() const { return this->_head; } /** * Get free data function. */ FreeDataFunc GetFreeDataFunc() { return _free_func; } /** * Get Mutex from Queue */ Mutex* GetMutex() { return _mutex; } // --------- SET METHODS ----------------------------------- /** * Set function to free content of element. * Function is called whenever element is removed from list. */ void SetFreeDataFunc(FreeDataFunc func) { _free_func = func; } /** * Set mutex for Queue, if mutex is NULL then mutex is disabled */ void SetMutex(Mutex* mutex) { _mutex = mutex; } // --------- OTHER METHODS --------------------------------- /** * Clears the Queue. This wi ll call 'Free Data Handler' */ void Clear(); /** * Insert new element to Queue, element would be * placed to the last position * @param data Data to insert */ bool Enqueue(void *data); /** * Remove first element from Queue * This will call 'Free Data Handler' * @return Data assigned to removed element, or NULL on error */ void *Dequeue(); /** * Get number of items containing given data. * @param data Search for this data * @return Number of elements found, that contains given data. */ int Contains(void *data) const; /** * Returns TRUE if given node is _head node in Queue. * @param element QueueNode* Node that should be checked. * @return TRUE if given node is _head node in Queue, FALSE otherwize. */ bool IsFirst(const QueueNode* element) const { return ((element == this->_head) ? true : false); } /** * Returns TRUE if given node is _tail node in Queue. * @param element QueueNode* Node that should be checked. * @return TRUE if given node oElement is _tail node in Queue, FALSE otherwize. */ bool IsLast(const QueueNode* element) const { return ((element->next == 0) ? true : false); } protected: // Members int _count; /// Size of the list object (how many elements are stored inside) bool _sort; /// How priority queue is sorted QueueNode* _head; /// Pointer to _head element of the List. QueueNode* _tail; /// Pointer to _tail element of the List. FreeDataFunc _free_func; /// Free data function Mutex* _mutex; /// Thread safe }; // Queue }; // Rtk namespace #endif |