Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * lock_set.h - Lockable set 00004 * 00005 * Created: Fri Apr 9 15:24:51 2010 00006 * Copyright 2006-2010 Tim Niemueller [www.niemueller.de] 00007 * 2010 Christoph Schwering 00008 * 00009 ****************************************************************************/ 00010 00011 /* This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. A runtime exception applies to 00015 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00016 * 00017 * This program 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 Library General Public License for more details. 00021 * 00022 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00023 */ 00024 00025 #ifndef __CORE_UTILS_LOCK_SET_H_ 00026 #define __CORE_UTILS_LOCK_SET_H_ 00027 00028 #include <core/threading/mutex.h> 00029 #include <core/utils/refptr.h> 00030 #include <set> 00031 00032 namespace fawkes { 00033 00034 template <typename KeyType, 00035 typename LessKey = std::less<KeyType> > 00036 class LockSet : public std::set<KeyType, LessKey> 00037 { 00038 public: 00039 LockSet(); 00040 LockSet(const LockSet<KeyType, LessKey> &lm); 00041 virtual ~LockSet(); 00042 00043 void lock() const; 00044 bool try_lock() const; 00045 void unlock() const; 00046 RefPtr<Mutex> mutex() const; 00047 00048 /** Iterator. */ 00049 typedef typename std::set<KeyType, LessKey>::iterator iterator; 00050 00051 std::pair<iterator, bool> insert_locked(const KeyType &key); 00052 void erase_locked(const KeyType &key); 00053 00054 LockSet<KeyType, LessKey> & operator=(const LockSet<KeyType, LessKey> &ll); 00055 LockSet<KeyType, LessKey> & operator=(const std::set<KeyType, LessKey> &l); 00056 00057 private: 00058 mutable RefPtr<Mutex> __mutex; 00059 00060 }; 00061 00062 00063 /** @class LockSet <core/utils/lock_set.h> 00064 * Set with a lock. 00065 * This class provides a set that has an intrinsic lock. The lock can be applied 00066 * with the regular locking methods. 00067 * 00068 * @see Mutex 00069 * @ingroup FCL 00070 * @author Tim Niemueller 00071 */ 00072 00073 00074 /** Constructor. */ 00075 template <typename KeyType, typename LessKey> 00076 LockSet<KeyType, LessKey>::LockSet() 00077 : __mutex(new Mutex()) 00078 {} 00079 00080 00081 /** Copy constructor. 00082 * @param lm LockSet to copy 00083 */ 00084 template <typename KeyType, typename LessKey> 00085 LockSet<KeyType, LessKey>::LockSet(const LockSet<KeyType, LessKey> &lm) 00086 : std::set<KeyType, LessKey>::set(lm), 00087 __mutex(new Mutex()) 00088 {} 00089 00090 00091 /** Destructor. */ 00092 template <typename KeyType, typename LessKey> 00093 LockSet<KeyType, LessKey>::~LockSet() 00094 {} 00095 00096 00097 /** Lock list. */ 00098 template <typename KeyType, typename LessKey> 00099 void 00100 LockSet<KeyType, LessKey>::lock() const 00101 { 00102 __mutex->lock(); 00103 } 00104 00105 00106 /** Try to lock list. 00107 * @return true, if the lock has been aquired, false otherwise. 00108 */ 00109 template <typename KeyType, typename LessKey> 00110 bool 00111 LockSet<KeyType, LessKey>::try_lock() const 00112 { 00113 return __mutex->try_lock(); 00114 } 00115 00116 00117 /** Unlock list. */ 00118 template <typename KeyType, typename LessKey> 00119 void 00120 LockSet<KeyType, LessKey>::unlock() const 00121 { 00122 return __mutex->unlock(); 00123 } 00124 00125 00126 /** Insert item with lock. 00127 * The set is automatically locked and unlocked during the removal. 00128 * @param key key of the value to insert 00129 * @return iterator to inserted item 00130 */ 00131 template <typename KeyType, typename LessKey> 00132 std::pair<typename LockSet<KeyType, LessKey>::iterator, bool> 00133 LockSet<KeyType, LessKey>::insert_locked(const KeyType &key) 00134 { 00135 __mutex->lock(); 00136 std::pair<iterator, bool> ret = 00137 std::set<KeyType, LessKey>::insert(key); 00138 __mutex->unlock(); 00139 return ret; 00140 } 00141 00142 /** Remove item with lock. 00143 * The set is automatically locked and unlocked during the removal. 00144 * @param key key of the value to erase 00145 */ 00146 template <typename KeyType, typename LessKey> 00147 void 00148 LockSet<KeyType, LessKey>::erase_locked(const KeyType &key) 00149 { 00150 __mutex->lock(); 00151 std::set<KeyType, LessKey>::erase(key); 00152 __mutex->unlock(); 00153 } 00154 00155 00156 /** Get access to the internal mutex. 00157 * Can be used with MutexLocker. 00158 * @return internal mutex 00159 */ 00160 template <typename KeyType, typename LessKey> 00161 RefPtr<Mutex> 00162 LockSet<KeyType, LessKey>::mutex() const 00163 { 00164 return __mutex; 00165 } 00166 00167 00168 /** Copy values from another LockSet. 00169 * Copies the values one by one. Both instances are locked during the copying and 00170 * this instance is cleared before copying. 00171 * @param ll lock set to copy 00172 * @return reference to this instance 00173 */ 00174 template <typename KeyType, typename LessKey> 00175 LockSet<KeyType, LessKey> & 00176 LockSet<KeyType, LessKey>::operator=(const LockSet<KeyType, LessKey> &ll) 00177 { 00178 __mutex->lock(); 00179 ll.lock(); 00180 this->clear(); 00181 typename LockSet<KeyType, LessKey>::const_iterator i; 00182 for (i = ll.begin(); i != ll.end(); ++i) { 00183 this->insert(*i); 00184 } 00185 ll.unlock(); 00186 __mutex->unlock(); 00187 00188 return *this; 00189 } 00190 00191 00192 /** Copy values from a standard set. 00193 * Copies the values one by one. This instance is locked during the copying and 00194 * cleared. 00195 * @param l set to copy 00196 * @return reference to this instance 00197 */ 00198 template <typename KeyType, typename LessKey> 00199 LockSet<KeyType, LessKey> & 00200 LockSet<KeyType, LessKey>::operator=(const std::set<KeyType, LessKey> &l) 00201 { 00202 __mutex->lock(); 00203 this->clear(); 00204 typename std::set<KeyType, LessKey>::const_iterator i; 00205 for (i = l.begin(); i != l.end(); ++i) { 00206 this->insert(*i); 00207 } 00208 __mutex->unlock(); 00209 00210 return *this; 00211 } 00212 00213 00214 } // end namespace fawkes 00215 00216 #endif