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
30#include <map>
31
32namespace fawkes {
33
34template <typename KeyType, typename ValueType, typename LessKey = std::less<KeyType>>
35class LockMultiMap : public std::multimap<KeyType, ValueType, LessKey>
36{
37public:
40 virtual ~LockMultiMap();
41
42 void lock() const;
43 bool try_lock() const;
44 void unlock() const;
46
47 void erase_locked(const KeyType &key);
48
51
53 operator=(const std::map<KeyType, ValueType, LessKey> &l);
54
55private:
56 mutable RefPtr<Mutex> mutex_;
57};
58
59/** @class LockMultiMap <core/utils/lock_map.h>
60 * Multi-Map with a lock.
61 * This class provides a multimap that has an intrinsic lock. The lock
62 * can be applied with the regular locking methods.
63 *
64 * @see Mutex
65 * @ingroup FCL
66 * @author Tim Niemueller
67 */
68
69/** Constructor. */
70template <typename KeyType, typename ValueType, typename LessKey>
72{
73}
74
75/** Copy constructor.
76 * @param lm LockMultiMap to copy
77 */
78template <typename KeyType, typename ValueType, typename LessKey>
81: std::map<KeyType, ValueType, LessKey>::map(lm), mutex_(new Mutex())
82{
83}
84
85/** Destructor. */
86template <typename KeyType, typename ValueType, typename LessKey>
88{
89}
90
91/** Lock list. */
92template <typename KeyType, typename ValueType, typename LessKey>
93void
95{
96 mutex_->lock();
97}
98
99/** Try to lock list.
100 * @return true, if the lock has been aquired, false otherwise.
101 */
102template <typename KeyType, typename ValueType, typename LessKey>
103bool
105{
106 return mutex_->try_lock();
107}
108
109/** Unlock list. */
110template <typename KeyType, typename ValueType, typename LessKey>
111void
113{
114 return mutex_->unlock();
115}
116
117/** Remove item with lock.
118 * The map is automatically locked and unlocked during the removal.
119 * @param key key of the value to erase
120 */
121template <typename KeyType, typename ValueType, typename LessKey>
122void
124{
125 mutex_->lock();
126 std::map<KeyType, ValueType, LessKey>::erase(key);
127 mutex_->unlock();
128}
129
130/** Get access to the internal mutex.
131 * Can be used with MutexLocker.
132 * @return internal mutex
133 */
134template <typename KeyType, typename ValueType, typename LessKey>
137{
138 return mutex_;
139}
140
141/** Copy values from another LockMultiMap.
142 * Copies the values one by one. Both instances are locked during the copying and
143 * this instance is cleared before copying.
144 * @param ll map to copy
145 * @return reference to this instance
146 */
147template <typename KeyType, typename ValueType, typename LessKey>
151{
152 mutex_->lock();
153 ll.lock();
154 this->clear();
156 for (i = ll.begin(); i != ll.end(); ++i) {
157 this->insert(*i);
158 }
159 ll.unlock();
160 mutex_->unlock();
161
162 return *this;
163}
164
165/** Copy values from a standard map.
166 * Copies the values one by one. This instance is locked during the copying and
167 * cleared.
168 * @param l map to copy
169 * @return reference to this instance
170 */
171template <typename KeyType, typename ValueType, typename LessKey>
173LockMultiMap<KeyType, ValueType, LessKey>::operator=(const std::map<KeyType, ValueType, LessKey> &l)
174{
175 mutex_->lock();
176 this->clear();
177 typename std::map<KeyType, ValueType, LessKey>::const_iterator i;
178 for (i = l.begin(); i != l.end(); ++i) {
179 this->insert(*i);
180 }
181 mutex_->unlock();
182
183 return *this;
184}
185
186} // end namespace fawkes
187
188#endif
Multi-Map with a lock.
Definition: lock_multimap.h:36
void unlock() const
Unlock list.
LockMultiMap(const LockMultiMap< KeyType, ValueType, LessKey > &lm)
Copy constructor.
Definition: lock_multimap.h:79
LockMultiMap< KeyType, ValueType, LessKey > & operator=(const LockMultiMap< KeyType, ValueType, LessKey > &ll)
Copy values from another LockMultiMap.
void lock() const
Lock list.
Definition: lock_multimap.h:94
LockMultiMap< KeyType, ValueType, LessKey > & operator=(const std::map< KeyType, ValueType, LessKey > &l)
Copy values from a standard map.
bool try_lock() const
Try to lock list.
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
virtual ~LockMultiMap()
Destructor.
Definition: lock_multimap.h:87
void erase_locked(const KeyType &key)
Remove item with lock.
LockMultiMap()
Constructor.
Definition: lock_multimap.h:71
Mutex mutual exclusion lock.
Definition: mutex.h:33
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Fawkes library namespace.