Fawkes API  Fawkes Development Version
lock_vector.h
1 
2 /***************************************************************************
3  * lock_vector.h - Lockable vector
4  *
5  * Created: Mon Jan 10 11:11:59 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_VECTOR_H_
25 #define __CORE_UTILS_LOCK_VECTOR_H_
26 
27 #include <core/threading/mutex.h>
28 #include <core/utils/refptr.h>
29 #include <vector>
30 
31 namespace fawkes {
32 #if 0 /* just to make Emacs auto-indent happy */
33 }
34 #endif
35 
36 template <typename Type>
37 class LockVector : public std::vector<Type>
38 {
39  public:
40  LockVector();
41  LockVector(const LockVector<Type> &lv);
42  virtual ~LockVector();
43  virtual void lock() const;
44  virtual bool try_lock() const;
45  virtual void unlock() const;
46  RefPtr<Mutex> mutex() const;
47 
48  void push_back_locked(const Type& x);
49  void pop_back_locked();
50  void erase_locked(typename std::vector<Type>::iterator pos);
51  void erase_locked(typename std::vector<Type>::iterator first,
52  typename std::vector<Type>::iterator last);
53 
55  LockVector<Type> & operator=(const std::vector<Type> &v);
56  private:
57  mutable RefPtr<Mutex> __mutex;
58 
59 };
60 
61 
62 /** @class LockVector <core/utils/lock_vector.h>
63  * Vector with a lock.
64  * This class provides a vector that has an intrinsic lock. The lock can be applied
65  * with the regular locking methods.
66  *
67  * @see Mutex
68  * @ingroup FCL
69  * @author Tim Niemueller
70  */
71 
72 
73 /** Constructor. */
74 template <typename Type>
76  : __mutex(new Mutex())
77 {}
78 
79 
80 /** Copy constructor.
81  * @param lv LockVector to copy
82  */
83 template <typename Type>
85  : std::vector<Type>::vector(lv), __mutex(new Mutex())
86 {}
87 
88 
89 /** Destructor. */
90 template <typename Type>
92 {}
93 
94 
95 /** Lock vector. */
96 template <typename Type>
97 void
99 {
100  __mutex->lock();
101 }
102 
103 
104 /** Try to lock vector.
105  * @return true, if the lock has been aquired, false otherwise.
106  */
107 template <typename Type>
108 bool
110 {
111  return __mutex->try_lock();
112 }
113 
114 
115 /** Unlock vector. */
116 template <typename Type>
117 void
119 {
120  return __mutex->unlock();
121 }
122 
123 
124 /** Push element to vector at back with lock protection.
125  * @param x element to add
126  */
127 template <typename Type>
128 void
130 {
131  __mutex->lock();
132  std::vector<Type>::push_back(x);
133  __mutex->unlock();
134 }
135 
136 
137 /** Remove last element with lock protection. */
138 template <typename Type>
139 void
141 {
142  __mutex->lock();
143  std::vector<Type>::pop_back();
144  __mutex->unlock();
145 }
146 
147 
148 /** Erase given element with lock protection.
149  * @param pos iterator for the object position to remove
150  */
151 template <typename Type>
152 void
153 LockVector<Type>::erase_locked(typename std::vector<Type>::iterator pos)
154 {
155  __mutex->lock();
156  std::vector<Type>::erase(pos);
157  __mutex->unlock();
158 }
159 
160 /** Erase given element range with lock protection.
161  * @param first iterator to first element to erase
162  * @param last iterator to first element not to erase
163  */
164 template <typename Type>
165 void
166 LockVector<Type>::erase_locked(typename std::vector<Type>::iterator first,
167  typename std::vector<Type>::iterator last)
168 {
169  __mutex->lock();
170  std::vector<Type>::erase(first, last);
171  __mutex->unlock();
172 }
173 
174 
175 /** Get access to the internal mutex.
176  * Can be used with MutexLocker.
177  * @return internal mutex
178  */
179 template <typename Type>
182 {
183  return __mutex;
184 }
185 
186 
187 /** Copy values from another LockVector.
188  * Copies the values one by one. Both instances are locked during the copying and
189  * this instance is cleared before copying.
190  * @param lv vector to copy
191  * @return reference to this instance
192  */
193 template <typename Type>
196 {
197  __mutex->lock();
198  lv.lock();
199  this->clear();
201  for (i = lv.begin(); i != lv.end(); ++i) {
202  this->push_back(*i);
203  }
204  lv.unlock();
205  __mutex->unlock();
206 
207  return *this;
208 }
209 
210 
211 /** Copy values from a standard vector.
212  * Copies the values one by one. This instance is locked during the copying and
213  * cleared.
214  * @param v vector to copy
215  * @return reference to this instance
216  */
217 template <typename Type>
219 LockVector<Type>::operator=(const std::vector<Type> &v)
220 {
221  __mutex->lock();
222  this->clear();
223  typename std::vector<Type>::const_iterator i;
224  for (i = v.begin(); i != v.end(); ++i) {
225  this->push_back(*i);
226  }
227  __mutex->unlock();
228 
229  return *this;
230 }
231 
232 } // end namespace fawkes
233 
234 #endif
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_vector.h:181
void erase_locked(typename std::vector< Type >::iterator pos)
Erase given element with lock protection.
Definition: lock_vector.h:153
Fawkes library namespace.
void pop_back_locked()
Remove last element with lock protection.
Definition: lock_vector.h:140
STL namespace.
virtual void lock() const
Lock vector.
Definition: lock_vector.h:98
LockVector()
Constructor.
Definition: lock_vector.h:75
void push_back_locked(const Type &x)
Push element to vector at back with lock protection.
Definition: lock_vector.h:129
virtual void unlock() const
Unlock vector.
Definition: lock_vector.h:118
LockVector< Type > & operator=(const LockVector< Type > &lv)
Copy values from another LockVector.
Definition: lock_vector.h:195
Vector with a lock.
Definition: lock_vector.h:37
virtual ~LockVector()
Destructor.
Definition: lock_vector.h:91
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
Mutex mutual exclusion lock.
Definition: mutex.h:32
virtual bool try_lock() const
Try to lock vector.
Definition: lock_vector.h:109