lock_map.h

00001
00002 /***************************************************************************
00003  *  lock_map.h - Lockable map
00004  *
00005  *  Created: Sat May 12 10:45:15 2007
00006  *  Copyright  2006-2009  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_MAP_H_
00025 #define __CORE_UTILS_LOCK_MAP_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 LockMap : public std::map<KeyType, ValueType, LessKey>
00038 {
00039  public:
00040   LockMap();
00041   LockMap(const LockMap<KeyType, ValueType, LessKey> &lm);
00042   virtual ~LockMap();
00043
00044   void           lock();
00045   bool           try_lock();
00046   void           unlock();
00047   RefPtr<Mutex>  mutex() const;
00048
00049   void     erase_locked(const KeyType &key);
00050
00051  private:
00052   RefPtr<Mutex>  __mutex;
00053
00054 };
00055
00056 
00057 /** @class LockMap <core/utils/lock_map.h>
00058  * Map with a lock.
00059  * This class provides a map that has an intrinsic lock. The lock can be applied
00060  * with the regular locking methods.
00061  *
00062  * @see Mutex
00063  * @ingroup FCL
00064  * @author Tim Niemueller
00065  */
00066
00067 
00068 /** Constructor. */
00069 template <typename KeyType, typename ValueType, typename LessKey>
00070 LockMap<KeyType, ValueType, LessKey>::LockMap()
00071   : __mutex(new Mutex())
00072 {}
00073
00074 
00075 /** Copy constructor.
00076  * @param lm LockMap to copy
00077  */
00078 template <typename KeyType, typename ValueType, typename LessKey>
00079 LockMap<KeyType, ValueType, LessKey>::LockMap(const LockMap<KeyType, ValueType, LessKey> &lm)
00080   : std::map<KeyType, ValueType, LessKey>::map(lm),
00081     __mutex(new Mutex())
00082 {}
00083
00084 
00085 /** Destructor. */
00086 template <typename KeyType, typename ValueType, typename LessKey>
00087 LockMap<KeyType, ValueType, LessKey>::~LockMap()
00088 {}
00089
00090 
00091 /** Lock list. */
00092 template <typename KeyType, typename ValueType, typename LessKey>
00093 void
00094 LockMap<KeyType, ValueType, LessKey>::lock()
00095 {
00096   __mutex->lock();
00097 }
00098
00099 
00100 /** Try to lock list.
00101  * @return true, if the lock has been aquired, false otherwise.
00102  */
00103 template <typename KeyType, typename ValueType, typename LessKey>
00104 bool
00105 LockMap<KeyType, ValueType, LessKey>::try_lock()
00106 {
00107   return __mutex->try_lock();
00108 }
00109
00110 
00111 /** Unlock list. */
00112 template <typename KeyType, typename ValueType, typename LessKey>
00113 void
00114 LockMap<KeyType, ValueType, LessKey>::unlock()
00115 {
00116   return __mutex->unlock();
00117 }
00118
00119 
00120 /** Remove item with lock.
00121  * The map is automatically locked and unlocked during the removal.
00122  * @param key key of the value to erase
00123  */
00124 template <typename KeyType, typename ValueType, typename LessKey>
00125 void
00126 LockMap<KeyType, ValueType, LessKey>::erase_locked(const KeyType &key)
00127 {
00128   __mutex->lock();
00129   std::map<KeyType, ValueType, LessKey>::erase(key);
00130   __mutex->unlock();
00131 }
00132
00133 
00134 /** Get access to the internal mutex.
00135  * Can be used with MutexLocker.
00136  * @return internal mutex
00137  */
00138 template <typename KeyType, typename ValueType, typename LessKey>
00139 RefPtr<Mutex>
00140 LockMap<KeyType, ValueType, LessKey>::mutex() const
00141 {
00142   return __mutex;
00143 }
00144
00145
00146 } // end namespace fawkes
00147
00148 #endif