00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : AtomicStack 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef AtomicStackHPP 00033 #define AtomicStackHPP 00034 00035 #include <zeusbase/System/CriticalSection.h> 00036 #include <zeusbase/System/CriticalScopeLock.hpp> 00037 #include <zeusbase/System/Stack.hpp> 00038 00039 BEGIN_NAMESPACE_Zeus 00040 00041 /******************************************************************************/ 00044 /******************************************************************************/ 00045 template <class T> class TAtomicStack : public IStack<T> 00046 { 00047 public: 00048 TAtomicStack(); 00049 TAtomicStack(const T& rEmptyObject); 00050 virtual ~TAtomicStack(); 00051 00052 //Methods of IStack 00053 virtual void MQUALIFIER copyToStack(IStack<T>& rStack) const; 00054 virtual void MQUALIFIER copyToList(IList<T>& rList) const; 00055 virtual void MQUALIFIER flush(); 00056 virtual Int MQUALIFIER getCount() const; 00057 virtual bool MQUALIFIER hasItem(const T& rItem) const; 00058 virtual bool MQUALIFIER hasAllItems(const IList<T>& rlstItems) const; 00059 virtual bool MQUALIFIER isEmpty() const; 00060 virtual void MQUALIFIER push(const T& rData); 00061 virtual void MQUALIFIER pushAll(const IList<T>& rlstItems); 00062 virtual T MQUALIFIER pop(); 00063 virtual T& MQUALIFIER peek(); 00064 00065 //operators 00066 TAtomicStack<T>& operator=(const TAtomicStack<T>& rStack); 00067 TAtomicStack<T>& operator=(const TStack<T>& rStack); 00068 TAtomicStack<T>& operator=(const IStack<T>& rStack); 00069 00070 private: 00072 TStack<T> m_stackData; 00074 TCriticalSection& m_rLock; 00075 }; 00076 00077 00078 /******************************************************************************/ 00081 /******************************************************************************/ 00082 template <class T> inline TAtomicStack<T>::TAtomicStack() 00083 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)) 00084 {} 00085 00086 /******************************************************************************/ 00089 /******************************************************************************/ 00090 template <class T> inline TAtomicStack<T>::TAtomicStack(const T& rEmptyitem) 00091 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00092 m_stackData(rEmptyitem) 00093 {} 00094 00095 00096 /******************************************************************************/ 00099 /******************************************************************************/ 00100 template <class T> inline TAtomicStack<T>::~TAtomicStack() 00101 { 00102 m_rLock.release(); 00103 } 00104 00105 /******************************************************************************/ 00108 /******************************************************************************/ 00109 template <class T> inline void MQUALIFIER TAtomicStack<T>::flush() 00110 { 00112 // LOCK 00113 TCriticalScopeLock Lock(m_rLock); 00114 m_stackData.flush(); 00116 } 00117 00118 00119 /******************************************************************************/ 00122 /******************************************************************************/ 00123 template <class T> inline void MQUALIFIER TAtomicStack<T>::push(const T& data) 00124 { 00126 // LOCK 00127 TCriticalScopeLock Lock(m_rLock); 00128 m_stackData.push(data); 00130 } 00131 00132 /******************************************************************************/ 00135 /******************************************************************************/ 00136 template <class T> inline void MQUALIFIER TAtomicStack<T>::pushAll(const IList<T>& rlstItems) 00137 { 00139 // LOCK 00140 TCriticalScopeLock Lock(m_rLock); 00141 m_stackData.pushAll(rlstItems); 00143 } 00144 00145 00146 /******************************************************************************/ 00149 /******************************************************************************/ 00150 template <class T> inline T MQUALIFIER TAtomicStack<T>::pop() 00151 { 00153 // LOCK 00154 TCriticalScopeLock Lock(m_rLock); 00155 return m_stackData.pop(); 00157 } 00158 00159 /******************************************************************************/ 00162 /******************************************************************************/ 00163 template <class T> inline T& MQUALIFIER TAtomicStack<T>::peek() 00164 { 00166 // LOCK 00167 TCriticalScopeLock Lock(m_rLock); 00168 return m_stackData.peek(); 00170 } 00171 00172 /******************************************************************************/ 00175 /******************************************************************************/ 00176 template <class T> inline Int MQUALIFIER TAtomicStack<T>::getCount() const 00177 { 00179 // LOCK 00180 TCriticalScopeLock Lock(m_rLock); 00181 return m_stackData.getCount(); 00183 } 00184 00185 /******************************************************************************/ 00188 /******************************************************************************/ 00189 template <class T> inline bool MQUALIFIER TAtomicStack<T>::hasItem(const T& rItem) const 00190 { 00192 // LOCK 00193 TCriticalScopeLock Lock(m_rLock); 00194 return m_stackData.hasItem(rItem); 00196 } 00197 00198 /******************************************************************************/ 00201 /******************************************************************************/ 00202 template <class T> inline bool MQUALIFIER TAtomicStack<T>::hasAllItems(const IList<T>& rlstItems) const 00203 { 00205 // LOCK 00206 TCriticalScopeLock Lock(m_rLock); 00207 return m_stackData.hasAllItems(rlstItems); 00209 } 00210 00211 /******************************************************************************/ 00214 /******************************************************************************/ 00215 template <class T> inline bool MQUALIFIER TAtomicStack<T>::isEmpty() const 00216 { 00218 // LOCK 00219 TCriticalScopeLock Lock(m_rLock); 00220 return m_stackData.isEmpty(); 00222 } 00223 00224 /******************************************************************************/ 00227 /******************************************************************************/ 00228 template <class T> inline void MQUALIFIER TAtomicStack<T>::copyToStack(IStack<T>& rStack) const 00229 { 00231 // LOCK 00232 TCriticalScopeLock Lock(m_rLock); 00233 m_stackData.copyToStack(rStack); 00235 } 00236 00237 /******************************************************************************/ 00240 /******************************************************************************/ 00241 template <class T> inline void MQUALIFIER TAtomicStack<T>::copyToList(IList<T>& rList) const 00242 { 00244 // LOCK 00245 TCriticalScopeLock Lock(m_rLock); 00246 m_stackData.copyToList(rList); 00248 } 00249 00250 /******************************************************************************/ 00254 /******************************************************************************/ 00255 template <class T> TAtomicStack<T>& TAtomicStack<T>::operator=(const TAtomicStack<T>& rStack) 00256 { 00258 // LOCK 00259 m_rLock.enter(); 00260 rStack.m_rLock.enter(); 00261 00262 m_stackData = rStack.m_stackData; 00263 00264 rStack.m_rLock.leave(); 00265 m_rLock.leave(); 00267 00268 return *this; 00269 } 00270 00271 /******************************************************************************/ 00275 /******************************************************************************/ 00276 template <class T> inline TAtomicStack<T>& TAtomicStack<T>::operator=(const TStack<T>& rStack) 00277 { 00279 // LOCK 00280 TCriticalScopeLock Lock(m_rLock); 00281 m_stackData = rStack; 00282 return *this; 00284 } 00285 00286 /******************************************************************************/ 00290 /******************************************************************************/ 00291 template <class T> inline TAtomicStack<T>& TAtomicStack<T>::operator=(const IStack<T>& rStack) 00292 { 00294 // LOCK 00295 TCriticalScopeLock Lock(m_rLock); 00296 m_stackData = rStack; 00297 return *this; 00299 } 00300 00301 END_NAMESPACE_Zeus 00302 00303 #endif