00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : AtomicSet 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 AtomicSetHPP 00033 #define AtomicSetHPP 00034 00035 #include <zeusbase/System/CriticalSection.h> 00036 #include <zeusbase/System/CriticalScopeLock.hpp> 00037 #include <zeusbase/System/Set.hpp> 00038 00039 BEGIN_NAMESPACE_Zeus 00040 00041 /***************************************************************************/ 00044 /***************************************************************************/ 00045 template <class T> class TAtomicSet : public ISet<T> 00046 { 00047 public: 00048 00049 /**************************************************************************/ 00052 /**************************************************************************/ 00053 inline TAtomicSet() 00054 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)) 00055 {} 00056 00057 /**************************************************************************/ 00061 /**************************************************************************/ 00062 inline TAtomicSet(const T& rEmptyItem) 00063 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00064 m_setData(rEmptyItem) 00065 {} 00066 00067 /**************************************************************************/ 00071 /**************************************************************************/ 00072 inline TAtomicSet(const TSet<T>& rSet) 00073 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00074 m_setData(rSet) 00075 {} 00076 00077 /**************************************************************************/ 00081 /**************************************************************************/ 00082 inline TAtomicSet(const TAtomicSet<T>& rSet) 00083 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)) 00084 { 00085 *this = rSet; //this will also protect the rSet object 00086 } 00087 00088 /**************************************************************************/ 00092 /**************************************************************************/ 00093 inline TAtomicSet(const IList<T>& rList) 00094 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00095 m_setData(rList) 00096 {} 00097 00098 #ifdef USE_STL_BINDINGS 00099 /*********************************************************************/ 00103 /*********************************************************************/ 00104 inline TAtomicSet(const std::set<T>& rSet) 00105 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00106 m_setData(rSet) 00107 {} 00108 #endif 00109 00110 /**************************************************************************/ 00113 /**************************************************************************/ 00114 inline virtual ~TSet() 00115 { 00116 m_rLock.release(); 00117 } 00118 00119 /**************************************************************************/ 00122 /**************************************************************************/ 00123 inline virtual bool MQUALIFIER addItem(const T& rItem) 00124 { 00126 // LOCK 00127 TCriticalScopeLock Lock(m_rLock); 00128 return m_setData.addItem(rItem); 00130 } 00131 00132 /**************************************************************************/ 00135 /**************************************************************************/ 00136 inline virtual void MQUALIFIER clear() 00137 { 00139 // LOCK 00140 TCriticalScopeLock Lock(m_rLock); 00141 m_setData.clear(); 00143 } 00144 00145 /**************************************************************************/ 00148 /**************************************************************************/ 00149 inline virtual Int MQUALIFIER getCount() const 00150 { 00152 // LOCK 00153 TCriticalScopeLock Lock(m_rLock); 00154 return m_setData.getCount(); 00156 } 00157 00158 /**************************************************************************/ 00161 /**************************************************************************/ 00162 inline virtual bool MQUALIFIER hasItem(const T& rItem) const 00163 { 00165 // LOCK 00166 TCriticalScopeLock Lock(m_rLock); 00167 return m_setData.hasItem(rItem); 00169 } 00170 00171 /************************************************************************/ 00174 /************************************************************************/ 00175 inline virtual IListIterator<T>* MQUALIFIER getIterator() 00176 { 00178 // LOCK 00179 TCriticalScopeLock Lock(m_rLock); 00180 return m_setData.getIterator(); 00182 } 00183 00184 /************************************************************************/ 00187 /************************************************************************/ 00188 inline virtual const IListIterator<T>* MQUALIFIER getConstIterator() const 00189 { 00191 // LOCK 00192 TCriticalScopeLock Lock(m_rLock); 00193 return m_setData.getConstIterator(); 00195 } 00196 00197 /************************************************************************/ 00200 /************************************************************************/ 00201 inline virtual bool MQUALIFIER isEmpty() const 00202 { 00204 // LOCK 00205 TCriticalScopeLock Lock(m_rLock); 00206 return m_setData.isEmpty(); 00208 } 00209 00210 /************************************************************************/ 00213 /************************************************************************/ 00214 inline virtual void MQUALIFIER releaseIterator(const IListIterator<T>* pIterator) const 00215 { 00217 // LOCK 00218 TCriticalScopeLock Lock(m_rLock); 00219 m_setData.releaseIterator(pIterator); 00221 } 00222 00223 /************************************************************************/ 00226 /************************************************************************/ 00227 inline virtual bool MQUALIFIER removeItem(const T& rItem) 00228 { 00230 // LOCK 00231 TCriticalScopeLock Lock(m_rLock); 00232 return m_setData.removeItem(rItem); 00234 } 00235 00236 /************************************************************************/ 00239 /************************************************************************/ 00240 inline virtual void MQUALIFIER copyToList(IList<T>& rList) const 00241 { 00243 // LOCK 00244 TCriticalScopeLock Lock(m_rLock); 00245 m_setData.copyToList(rList); 00247 } 00248 00249 /************************************************************************/ 00252 /************************************************************************/ 00253 inline virtual void MQUALIFIER copyToSet(ISet<T>& rSet) const 00254 { 00256 // LOCK 00257 TCriticalScopeLock Lock(m_rLock); 00258 m_setData.copyToSet(rSet); 00260 } 00261 00262 /************************************************************************/ 00265 /************************************************************************/ 00266 inline virtual bool MQUALIFIER equals(const ISet<T>& rSet) const 00267 { 00269 // LOCK 00270 TCriticalScopeLock Lock(m_rLock); 00271 return m_setData.equals(rSet); 00273 } 00274 /************************************************************************/ 00277 /************************************************************************/ 00278 inline virtual T& MQUALIFIER getItem() 00279 { 00281 // LOCK 00282 TCriticalScopeLock Lock(m_rLock); 00283 return m_setData.getItem(); 00285 } 00286 00287 /************************************************************************/ 00290 /************************************************************************/ 00291 inline virtual const T& MQUALIFIER getItemConst() const 00292 { 00294 // LOCK 00295 TCriticalScopeLock Lock(m_rLock); 00296 return m_setData.getItemConst(); 00298 } 00299 00300 /************************************************************************/ 00303 /************************************************************************/ 00304 inline bool operator==(const ISet<T>& rSet) const 00305 { 00307 // LOCK 00308 TCriticalScopeLock Lock(m_rLock); 00309 return (m_setData == rSet); 00311 } 00312 00313 /************************************************************************/ 00316 /************************************************************************/ 00317 inline bool operator==(const TAtomicSet<T>& rSet) const 00318 { 00320 // LOCK 00321 m_rLock.enter(); 00322 rSet.m_rLock.enter(); 00323 00324 bool bRetval = (m_setData == rSet.m_setData); 00325 00326 rSet.m_rLock.leave(); 00327 m_rLock.leave(); 00329 return bRetval; 00330 } 00331 00332 /************************************************************************/ 00335 /************************************************************************/ 00336 inline bool operator!=(const ISet<T>& rSet) const 00337 { 00339 // LOCK 00340 TCriticalScopeLock Lock(m_rLock); 00341 return (m_setData != rSet); 00343 } 00344 00345 /************************************************************************/ 00348 /************************************************************************/ 00349 inline bool operator!=(const TAtomicSet<T>& rSet) const 00350 { 00352 // LOCK 00353 m_rLock.enter(); 00354 rSet.m_rLock.enter(); 00355 00356 bool bRetval = (m_setData != rSet.m_setData); 00357 00358 rSet.m_rLock.leave(); 00359 m_rLock.leave(); 00361 return bRetval; 00362 } 00363 00364 /************************************************************************/ 00367 /************************************************************************/ 00368 inline TAtomicSet<T>& operator=(const TAtomicSet<T>& rSet) 00369 { 00371 // LOCK 00372 m_rLock.enter(); 00373 rSet.m_rLock.enter(); 00374 00375 m_setData = rSet.m_setData; 00376 00377 rSet.m_rLock.leave(); 00378 m_rLock.leave(); 00380 00381 return *this; 00382 } 00383 00384 /************************************************************************/ 00387 /************************************************************************/ 00388 inline TAtomicSet<T>& operator=(const ISet<T>& rSet) 00389 { 00391 // LOCK 00392 TCriticalScopeLock Lock(m_rLock); 00393 m_setData = rSet; 00394 return *this; 00396 } 00397 00398 /************************************************************************/ 00401 /************************************************************************/ 00402 inline TSet<T>& operator=(const IList<T>& rList) 00403 { 00405 // LOCK 00406 TCriticalScopeLock Lock(m_rLock); 00407 m_setData = rList; 00408 return *this; 00410 } 00411 00412 private: 00414 TSet<T> m_setData; 00416 TCriticalSection& m_rLock; 00417 } 00418 00419