Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * lock_multimap.h - Lockable multimap 00004 * 00005 * Created: Fri Aug 12 11:52:25 2011 00006 * Copyright 2006-2011 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #ifndef __CORE_UTILS_LOCK_MULTIMAP_H_ 00025 #define __CORE_UTILS_LOCK_MULTIMAP_H_ 00026 00027 #include <core/threading/mutex.h> 00028 #include <core/utils/refptr.h> 00029 #include <map> 00030 00031 namespace fawkes { 00032 00033 00034 template <typename KeyType, 00035 typename ValueType, 00036 typename LessKey = std::less<KeyType> > 00037 class LockMultiMap : public std::multimap<KeyType, ValueType, LessKey> 00038 { 00039 public: 00040 LockMultiMap(); 00041 LockMultiMap(const LockMultiMap<KeyType, ValueType, LessKey> &lm); 00042 virtual ~LockMultiMap(); 00043 00044 void lock() const; 00045 bool try_lock() const; 00046 void unlock() const; 00047 RefPtr<Mutex> mutex() const; 00048 00049 void erase_locked(const KeyType &key); 00050 00051 LockMultiMap<KeyType, ValueType, LessKey> & 00052 operator=(const LockMultiMap<KeyType, ValueType, LessKey> &ll); 00053 00054 LockMultiMap<KeyType, ValueType, LessKey> & 00055 operator=(const std::map<KeyType, ValueType, LessKey> &l); 00056 00057 private: 00058 mutable RefPtr<Mutex> __mutex; 00059 00060 }; 00061 00062 00063 /** @class LockMultiMap <core/utils/lock_map.h> 00064 * Multi-Map with a lock. 00065 * This class provides a multimap that has an intrinsic lock. The lock 00066 * can be applied 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 ValueType, typename LessKey> 00076 LockMultiMap<KeyType, ValueType, LessKey>::LockMultiMap() 00077 : __mutex(new Mutex()) 00078 {} 00079 00080 00081 /** Copy constructor. 00082 * @param lm LockMultiMap to copy 00083 */ 00084 template <typename KeyType, typename ValueType, typename LessKey> 00085 LockMultiMap<KeyType, ValueType, LessKey>::LockMultiMap(const LockMultiMap<KeyType, ValueType, LessKey> &lm) 00086 : std::map<KeyType, ValueType, LessKey>::map(lm), 00087 __mutex(new Mutex()) 00088 {} 00089 00090 00091 /** Destructor. */ 00092 template <typename KeyType, typename ValueType, typename LessKey> 00093 LockMultiMap<KeyType, ValueType, LessKey>::~LockMultiMap() 00094 {} 00095 00096 00097 /** Lock list. */ 00098 template <typename KeyType, typename ValueType, typename LessKey> 00099 void 00100 LockMultiMap<KeyType, ValueType, 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 ValueType, typename LessKey> 00110 bool 00111 LockMultiMap<KeyType, ValueType, LessKey>::try_lock() const 00112 { 00113 return __mutex->try_lock(); 00114 } 00115 00116 00117 /** Unlock list. */ 00118 template <typename KeyType, typename ValueType, typename LessKey> 00119 void 00120 LockMultiMap<KeyType, ValueType, LessKey>::unlock() const 00121 { 00122 return __mutex->unlock(); 00123 } 00124 00125 00126 /** Remove item with lock. 00127 * The map is automatically locked and unlocked during the removal. 00128 * @param key key of the value to erase 00129 */ 00130 template <typename KeyType, typename ValueType, typename LessKey> 00131 void 00132 LockMultiMap<KeyType, ValueType, LessKey>::erase_locked(const KeyType &key) 00133 { 00134 __mutex->lock(); 00135 std::map<KeyType, ValueType, LessKey>::erase(key); 00136 __mutex->unlock(); 00137 } 00138 00139 00140 /** Get access to the internal mutex. 00141 * Can be used with MutexLocker. 00142 * @return internal mutex 00143 */ 00144 template <typename KeyType, typename ValueType, typename LessKey> 00145 RefPtr<Mutex> 00146 LockMultiMap<KeyType, ValueType, LessKey>::mutex() const 00147 { 00148 return __mutex; 00149 } 00150 00151 00152 00153 /** Copy values from another LockMultiMap. 00154 * Copies the values one by one. Both instances are locked during the copying and 00155 * this instance is cleared before copying. 00156 * @param ll map to copy 00157 * @return reference to this instance 00158 */ 00159 template <typename KeyType, typename ValueType, typename LessKey> 00160 LockMultiMap<KeyType, ValueType, LessKey> & 00161 LockMultiMap<KeyType, ValueType, LessKey>::operator=(const LockMultiMap<KeyType, ValueType, LessKey> &ll) 00162 { 00163 __mutex->lock(); 00164 ll.lock(); 00165 this->clear(); 00166 typename LockMultiMap<KeyType, ValueType, LessKey>::const_iterator i; 00167 for (i = ll.begin(); i != ll.end(); ++i) { 00168 this->insert(*i); 00169 } 00170 ll.unlock(); 00171 __mutex->unlock(); 00172 00173 return *this; 00174 } 00175 00176 00177 /** Copy values from a standard map. 00178 * Copies the values one by one. This instance is locked during the copying and 00179 * cleared. 00180 * @param l map to copy 00181 * @return reference to this instance 00182 */ 00183 template <typename KeyType, typename ValueType, typename LessKey> 00184 LockMultiMap<KeyType, ValueType, LessKey> & 00185 LockMultiMap<KeyType, ValueType, LessKey>::operator=(const std::map<KeyType, ValueType, LessKey> &l) 00186 { 00187 __mutex->lock(); 00188 this->clear(); 00189 typename std::map<KeyType, ValueType, LessKey>::const_iterator i; 00190 for (i = l.begin(); i != l.end(); ++i) { 00191 this->insert(*i); 00192 } 00193 __mutex->unlock(); 00194 00195 return *this; 00196 } 00197 00198 00199 } // end namespace fawkes 00200 00201 #endif