00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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