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