Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wefts_stack.h

Go to the documentation of this file.
00001 /*
00002    wefts_stack.h
00003    Waitable template FIFO structure.
00004 
00005    $Id: wefts_stack.h,v 1.2 2004/03/08 20:28:53 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : 2003-08-03 17:50
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 /**************************************************************************
00015 *   This program is free software; you can redistribute it and/or modify  *
00016 *   it under the terms of the GNU Library General Public License as       *
00017 *   published by the Free Software Foundation; either version 2.1 of the  *
00018 *   License, or (at your option) any later version.                       *
00019 ***************************************************************************/
00020 
00021 
00022 #ifndef WT_STACK_H
00023 #define WT_STACK_H
00024 
00025 #include <wefts_fastcond.h>
00026 #include <list>
00027 
00028 namespace Wefts {
00029 
00056 template <class T>
00057 
00058 class Stack {
00059 private:
00060    void p_waitForFree() {
00061       while ( m_list.size() >= m_maxLen )
00062          m_cond.wait();
00063    }
00064 
00065    void p_waitForFull() {
00066       while ( m_list.size() == 0 )
00067          m_cond.wait();
00068    }
00069 
00070 protected:
00071    FastCondition m_cond;
00072    unsigned long m_maxLen;
00073    std::list<T> m_list;
00074 
00075 public:
00082    Stack( unsigned int maxLen = 0) {
00083       m_maxLen = maxLen;
00084    }
00085 
00097    bool push( T elem, const bool wait=false)
00098    {
00099       m_cond.lock();
00100       if ( m_maxLen > 0 && m_list.size() >= m_maxLen )
00101       {
00102          if ( wait ) {
00103             // this is for compilers that canno expand loops inline
00104             p_waitForFree();
00105          }
00106          else {
00107             m_cond.unlock();
00108             return false;
00109          }
00110       }
00111       m_list.push_back( elem );
00112       m_cond.signal();
00113       m_cond.unlock();
00114       return true;
00115    }
00116 
00136    bool pop( T &elem, const bool wait=true)
00137    {
00138       m_cond.lock();
00139       if ( m_list.size() == 0 )
00140       {
00141          if ( wait ) {
00142             // this is for compilers that canno expand loops inline
00143             p_waitForFull();
00144          }
00145          else {
00146             m_cond.unlock();
00147             return false;
00148          }
00149       }
00150       elem = m_list.back();
00151       m_list.pop_back();
00152       m_cond.signal();
00153       m_cond.unlock();
00154       return true;
00155    }
00156 
00157 };
00158 
00159 }
00160 
00161 #endif
00162 /* end of wefts_stack.h */

Generated on Sat Mar 13 21:05:44 2004 for Wefts by doxygen 1.3.5