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 
30 #include <cstdlib>
31 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
32 # include <functional>
33 # include <unordered_map>
34 #elif __GLIBCXX__ > 20080305
35 # include <tr1/unordered_map>
36 #else
37 # include <ext/hash_map>
38 #endif
39 
40 namespace fawkes {
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();
60  LockHashMap(const LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &lh);
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 /** @class LockHashMap core/utils/lock_hashmap.h
76  * Hash map with a lock.
77  * This class provides a hash map that has an intrinsic lock. The lock can be applied
78  * with the regular locking methods.
79  *
80  * @see Mutex
81  * @ingroup FCL
82  * @author Tim Niemueller
83  */
84 
85 /** Constructor. */
86 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
88 {
89 }
90 
91 /** Copy constructor.
92  * @param lh LockHashMap to copy
93  */
94 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
97 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
98 : std::unordered_map<KeyType, ValueType, HashFunction, EqualKey>::unordered_map(lh)
99 #elif __GLIBCXX__ > 20080305
100 : std::tr1::unordered_map<KeyType, ValueType, HashFunction, EqualKey>::unordered_map(lh)
101 #else
102 : gnu_cxx_::hash_map<KeyType, ValueType, HashFunction, EqualKey>::hash_map(lh)
103 #endif
104  ,
105  mutex_(new Mutex())
106 {
107 }
108 
109 /** Destructor. */
110 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
112 {
113 }
114 
115 /** Lock map. */
116 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
117 void
119 {
120  mutex_->lock();
121 }
122 
123 /** Try to lock map.
124  * @return true, if the lock has been aquired, false otherwise.
125  */
126 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
127 bool
129 {
130  return mutex_->try_lock();
131 }
132 
133 /** Unlock map. */
134 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
135 void
137 {
138  return mutex_->unlock();
139 }
140 
141 /** Get access to the internal mutex.
142  * Can be used with MutexLocker.
143  * @return internal mutex
144  */
145 template <typename KeyType, typename ValueType, class HashFunction, typename EqualKey>
148 {
149  return mutex_;
150 }
151 
152 /** Copy values from another LockHashMap.
153  * Copies the values one by one. Both instances are locked during the copying and
154  * this instance is cleared before copying.
155  * @param ll hash map to copy
156  * @return reference to this instance
157  */
158 template <typename KeyType, typename ValueType, class HashFunction, typename EqualKey>
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 } // end namespace fawkes
177 
178 #endif
fawkes::LockHashMap::try_lock
bool try_lock() const
Try to lock map.
Definition: lock_hashmap.h:134
fawkes::Mutex
Definition: mutex.h:38
fawkes::LockHashMap::mutex
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_hashmap.h:153
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:57
fawkes::LockHashMap::operator=
LockHashMap< KeyType, ValueType, HashFunction, EqualKey > & operator=(const LockHashMap< KeyType, ValueType, HashFunction, EqualKey > &ll)
Copy values from another LockHashMap.
Definition: lock_hashmap.h:167
fawkes::LockHashMap::LockHashMap
LockHashMap()
Constructor.
Definition: lock_hashmap.h:93
fawkes::LockHashMap::~LockHashMap
virtual ~LockHashMap()
Destructor.
Definition: lock_hashmap.h:117
fawkes
fawkes::LockHashMap::unlock
void unlock() const
Unlock map.
Definition: lock_hashmap.h:142
fawkes::LockHashMap
Definition: lock_hashmap.h:61
fawkes::LockHashMap::lock
void lock() const
Lock map.
Definition: lock_hashmap.h:124