Fawkes API  Fawkes Development Version
lock_multimap.h
1 
2 /***************************************************************************
3  * lock_multimap.h - Lockable multimap
4  *
5  * Created: Fri Aug 12 11:52:25 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CORE_UTILS_LOCK_MULTIMAP_H_
25 #define __CORE_UTILS_LOCK_MULTIMAP_H_
26 
27 #include <core/threading/mutex.h>
28 #include <core/utils/refptr.h>
29 #include <map>
30 
31 namespace fawkes {
32 
33 
34 template <typename KeyType,
35  typename ValueType,
36  typename LessKey = std::less<KeyType> >
37 class LockMultiMap : public std::multimap<KeyType, ValueType, LessKey>
38 {
39  public:
40  LockMultiMap();
42  virtual ~LockMultiMap();
43 
44  void lock() const;
45  bool try_lock() const;
46  void unlock() const;
47  RefPtr<Mutex> mutex() const;
48 
49  void erase_locked(const KeyType &key);
50 
53 
55  operator=(const std::map<KeyType, ValueType, LessKey> &l);
56 
57  private:
58  mutable RefPtr<Mutex> __mutex;
59 
60 };
61 
62 
63 /** @class LockMultiMap <core/utils/lock_map.h>
64  * Multi-Map with a lock.
65  * This class provides a multimap that has an intrinsic lock. The lock
66  * can be applied with the regular locking methods.
67  *
68  * @see Mutex
69  * @ingroup FCL
70  * @author Tim Niemueller
71  */
72 
73 
74 /** Constructor. */
75 template <typename KeyType, typename ValueType, typename LessKey>
77  : __mutex(new Mutex())
78 {}
79 
80 
81 /** Copy constructor.
82  * @param lm LockMultiMap to copy
83  */
84 template <typename KeyType, typename ValueType, typename LessKey>
86  : std::map<KeyType, ValueType, LessKey>::map(lm),
87  __mutex(new Mutex())
88 {}
89 
90 
91 /** Destructor. */
92 template <typename KeyType, typename ValueType, typename LessKey>
94 {}
95 
96 
97 /** Lock list. */
98 template <typename KeyType, typename ValueType, typename LessKey>
99 void
101 {
102  __mutex->lock();
103 }
104 
105 
106 /** Try to lock list.
107  * @return true, if the lock has been aquired, false otherwise.
108  */
109 template <typename KeyType, typename ValueType, typename LessKey>
110 bool
112 {
113  return __mutex->try_lock();
114 }
115 
116 
117 /** Unlock list. */
118 template <typename KeyType, typename ValueType, typename LessKey>
119 void
121 {
122  return __mutex->unlock();
123 }
124 
125 
126 /** Remove item with lock.
127  * The map is automatically locked and unlocked during the removal.
128  * @param key key of the value to erase
129  */
130 template <typename KeyType, typename ValueType, typename LessKey>
131 void
133 {
134  __mutex->lock();
135  std::map<KeyType, ValueType, LessKey>::erase(key);
136  __mutex->unlock();
137 }
138 
139 
140 /** Get access to the internal mutex.
141  * Can be used with MutexLocker.
142  * @return internal mutex
143  */
144 template <typename KeyType, typename ValueType, typename LessKey>
147 {
148  return __mutex;
149 }
150 
151 
152 
153 /** Copy values from another LockMultiMap.
154  * Copies the values one by one. Both instances are locked during the copying and
155  * this instance is cleared before copying.
156  * @param ll map to copy
157  * @return reference to this instance
158  */
159 template <typename KeyType, typename ValueType, typename LessKey>
162 {
163  __mutex->lock();
164  ll.lock();
165  this->clear();
167  for (i = ll.begin(); i != ll.end(); ++i) {
168  this->insert(*i);
169  }
170  ll.unlock();
171  __mutex->unlock();
172 
173  return *this;
174 }
175 
176 
177 /** Copy values from a standard map.
178  * Copies the values one by one. This instance is locked during the copying and
179  * cleared.
180  * @param l map to copy
181  * @return reference to this instance
182  */
183 template <typename KeyType, typename ValueType, typename LessKey>
185 LockMultiMap<KeyType, ValueType, LessKey>::operator=(const std::map<KeyType, ValueType, LessKey> &l)
186 {
187  __mutex->lock();
188  this->clear();
189  typename std::map<KeyType, ValueType, LessKey>::const_iterator i;
190  for (i = l.begin(); i != l.end(); ++i) {
191  this->insert(*i);
192  }
193  __mutex->unlock();
194 
195  return *this;
196 }
197 
198 
199 } // end namespace fawkes
200 
201 #endif
void lock() const
Lock list.
Fawkes library namespace.
LockMultiMap()
Constructor.
Definition: lock_multimap.h:76
STL namespace.
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
bool try_lock() const
Try to lock list.
LockMultiMap< KeyType, ValueType, LessKey > & operator=(const LockMultiMap< KeyType, ValueType, LessKey > &ll)
Copy values from another LockMultiMap.
Multi-Map with a lock.
Definition: lock_multimap.h:37
virtual ~LockMultiMap()
Destructor.
Definition: lock_multimap.h:93
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
void erase_locked(const KeyType &key)
Remove item with lock.
Mutex mutual exclusion lock.
Definition: mutex.h:32
void unlock() const
Unlock list.