From: Dejan L. <dlo...@us...> - 2004-06-13 13:50:20
|
Update of /cvsroot/rtk/rtk/src/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11426 Added Files: Queue.cpp Log Message: RTK Queue --- NEW FILE: Queue.cpp --- /** * * 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... ***************************************************************************/ /** * Queue.cpp * $description * Authors (chronological order): * Dejan Lozanovic, null§rtk.cx * Contributors (chronological order): * $fname $lname, $email ***************************************************************************/ // includes for this file... // We don't want precompiled headers in Borland C++ Builder... #if defined (_BORLANDC__) # pragma hdrstop #endif #include <rtk/Queue.h> #include <stdlib.h> // package also #if defined(__BORLANDC__) # pragma package(smart_init) #endif namespace Rtk { #define CALL_FREE_DATA(d) if(_free_func) (*_free_func)((d)) bool Queue::Enqueue(void *data) { if(_mutex) _mutex->Lock(); // Allocate space for new element QueueNode* new_element = (QueueNode*)malloc(sizeof(QueueNode)); if (new_element == (QueueNode*)0) // NULL return false; new_element->data = data; new_element->next=(QueueNode*)0; if (_head == (QueueNode*)0) // Queue is empty { _head=_tail=new_element; } else { _tail->next=new_element; } _count++; if(_mutex) _mutex->Unlock(); return true; } // Enqueue(void *data) //--------------------------------------------------------------------------- Queue::~Queue() { Clear(); } // ~List() //--------------------------------------------------------------------------- void Queue::Clear() { // Remove each element. QueueNode* element_node; if(_mutex) _mutex->Lock(); element_node = _head; while (element_node) { QueueNode* element_next = element_node->next; CALL_FREE_DATA(element_node->data); free(element_node); element_node = element_next; } _head = _tail= 0; _count = 0; if(_mutex) _mutex->Unlock(); } //--------------------------------------------------------------------------- void *Queue::Dequeue() { void *data = NULL; QueueNode *element_node; if (_count == 0) return data; // We cannot remove element from an EMPTY list object! if(_mutex) _mutex->Lock(); element_node=_head->next; data=_head->data; CALL_FREE_DATA(_head->data); free(_head); _head=element_node; // Adjust the _count of the Queue _count--; if(_mutex) _mutex->Unlock(); // Everything is OK, return data return data; } }; //namespace RTK |