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