Fawkes API  Fawkes Development Version
lock_hashmap.h
1 
2 /***************************************************************************
3  * lock_hashmap.h - Lockable hash map
4  *
5  * Created: Fri May 11 22:40:09 2007
6  * Copyright 2006-2007 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_HASHMAP_H_
25 #define __CORE_UTILS_LOCK_HASHMAP_H_
26 
27 #include <core/threading/mutex.h>
28 #include <core/utils/refptr.h>
29 #include <cstdlib>
30 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
31 # include <unordered_map>
32 # include <functional>
33 #elif __GLIBCXX__ > 20080305
34 # include <tr1/unordered_map>
35 #else
36 # include <ext/hash_map>
37 #endif
38 
39 namespace fawkes {
40 
41 
42 template <class KeyType,
43  class ValueType,
44 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
45  class HashFunction = std::hash<KeyType>,
46  class EqualKey = std::equal_to<KeyType> >
47 class LockHashMap : public std::unordered_map<KeyType, ValueType, HashFunction, EqualKey>
48 #elif __GLIBCXX__ > 20080305
49  class HashFunction = std::tr1::hash<KeyType>,
50  class EqualKey = std::equal_to<KeyType> >
51 class LockHashMap : public std::tr1::unordered_map<KeyType, ValueType, HashFunction, EqualKey>
52 #else
53  class HashFunction = __gnu_cxx::hash<KeyType>,
54  class EqualKey = std::equal_to<KeyType> >
55 class LockHashMap : public __gnu_cxx::hash_map<KeyType, ValueType, HashFunction, EqualKey>
56 #endif
57 {
58  public:
59  LockHashMap();
61  virtual ~LockHashMap();
62 
63  void lock() const;
64  bool try_lock() const;
65  void unlock() const;
66  RefPtr<Mutex> mutex() const;
67 
70 
71  private:
72  mutable RefPtr<Mutex> __mutex;
73 
74 };
75 
76 
77 /** @class LockHashMap core/utils/lock_hashmap.h
78  * Hash map with a lock.
79  * This class provides a hash map that has an intrinsic lock. The lock can be applied
80  * with the regular locking methods.
81  *
82  * @see Mutex
83  * @ingroup FCL
84  * @author Tim Niemueller
85  */
86 
87 
88 /** Constructor. */
89 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
91  : __mutex(new Mutex())
92 {
93 }
94 
95 
96 /** Copy constructor.
97  * @param lh LockHashMap to copy
98  */
99 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
101 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
102  : std::unordered_map<KeyType, ValueType, HashFunction, EqualKey>::unordered_map(lh)
103 #elif __GLIBCXX__ > 20080305
104  : std::tr1::unordered_map<KeyType, ValueType, HashFunction, EqualKey>::unordered_map(lh)
105 #else
106  : __gnu_cxx::hash_map<KeyType, ValueType, HashFunction, EqualKey>::hash_map(lh)
107 #endif
108  , __mutex(new Mutex())
109 {
110 }
111 
112 
113 /** Destructor. */
114 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
116 {
117 }
118 
119 
120 /** Lock map. */
121 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
122 void
124 {
125  __mutex->lock();
126 }
127 
128 
129 /** Try to lock map.
130  * @return true, if the lock has been aquired, false otherwise.
131  */
132 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
133 bool
135 {
136  return __mutex->try_lock();
137 }
138 
139 
140 /** Unlock map. */
141 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
142 void
144 {
145  return __mutex->unlock();
146 }
147 
148 
149 /** Get access to the internal mutex.
150  * Can be used with MutexLocker.
151  * @return internal mutex
152  */
153 template <typename KeyType, typename ValueType, class HashFunction, typename EqualKey>
156 {
157  return __mutex;
158 }
159 
160 
161 /** Copy values from another LockHashMap.
162  * Copies the values one by one. Both instances are locked during the copying and
163  * this instance is cleared before copying.
164  * @param ll hash map to copy
165  * @return reference to this instance
166  */
167 template <typename KeyType, typename ValueType, class HashFunction, typename EqualKey>
171 {
172  __mutex->lock();
173  ll.lock();
174  this->clear();
176  for (i = ll.begin(); i != ll.end(); ++i) {
177  this->insert(*i);
178  }
179  ll.unlock();
180  __mutex->unlock();
181 
182  return *this;
183 }
184 
185 } // end namespace fawkes
186 
187 #endif
LockHashMap< KeyType, ValueType, HashFunction, EqualKey > & operator=(const LockHashMap< KeyType, ValueType, HashFunction, EqualKey > &ll)
Copy values from another LockHashMap.
Definition: lock_hashmap.h:169
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_hashmap.h:155
Fawkes library namespace.
LockHashMap()
Constructor.
Definition: lock_hashmap.h:90
virtual ~LockHashMap()
Destructor.
Definition: lock_hashmap.h:115
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
bool try_lock() const
Try to lock map.
Definition: lock_hashmap.h:134
Hash map with a lock.
Definition: lock_hashmap.h:55
void unlock() const
Unlock map.
Definition: lock_hashmap.h:143
Mutex mutual exclusion lock.
Definition: mutex.h:32
void lock() const
Lock map.
Definition: lock_hashmap.h:123