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