MADARA  3.4.1
ThreadSafeVector.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_THREADSAFE_VECTOR_CPP_
3 #define _MADARA_THREADSAFE_VECTOR_CPP_
4 
6 
7 template<typename T>
9 {
10 }
11 
12 template<typename T>
14  const ThreadSafeVector& rhs)
15 {
16  MADARA_GUARD_TYPE rhs_guard(rhs.mutex_);
17  vector_ = rhs.vector_;
18 }
19 
20 template<typename T>
22  const std::vector<T>& rhs)
23 {
24  vector_ = rhs;
25 }
26 
27 template<typename T>
29 {
30  clear();
31 }
32 
33 template<typename T>
35  const ThreadSafeVector<T>& rhs)
36 {
37  MADARA_GUARD_TYPE guard(mutex_);
38  MADARA_GUARD_TYPE rhs_guard(rhs.mutex_);
39 
40  if (this != &rhs)
41  {
42  vector_ = rhs.vector_;
43  }
44 }
45 
46 template<typename T>
47 void madara::utility::ThreadSafeVector<T>::operator=(const std::vector<T>& rhs)
48 {
49  MADARA_GUARD_TYPE guard(mutex_);
50 
51  if (this->vector_ != &rhs)
52  {
53  vector_ = rhs;
54  }
55 }
56 
57 template<typename T>
59 {
60  MADARA_GUARD_TYPE guard(mutex_);
61  return vector_[index];
62 }
63 
64 template<typename T>
66 {
67  MADARA_GUARD_TYPE guard(mutex_);
68  return vector_[index];
69 }
70 
71 template<typename T>
73 {
74  MADARA_GUARD_TYPE guard(mutex_);
75  vector_.push_back(value);
76 }
77 
78 template<typename T>
80 {
81  MADARA_GUARD_TYPE guard(mutex_);
82  return vector_.back();
83 }
84 
85 template<typename T>
87 {
88  MADARA_GUARD_TYPE guard(mutex_);
89  T result(vector_.back());
90 
91  vector_.pop_back();
92  return result;
93 }
94 
95 template<typename T>
97 {
98  MADARA_GUARD_TYPE guard(mutex_);
99 
100  if (index < vector_.size())
101  vector_.erase(vector_.begin() + index);
102 
103  return vector_.size();
104 }
105 
106 template<typename T>
108 {
109  MADARA_GUARD_TYPE guard(mutex_);
110  return vector_.back();
111 }
112 
113 template<typename T>
115 {
116  MADARA_GUARD_TYPE guard(mutex_);
117  vector_.resize(new_size);
118 }
119 
120 template<typename T>
122 {
123  MADARA_GUARD_TYPE guard(mutex_);
124  vector_.reserve(new_size);
125 }
126 
127 template<typename T>
129 {
130  MADARA_GUARD_TYPE guard(mutex_);
131  return vector_.size();
132 }
133 
134 template<typename T>
136 {
137  MADARA_GUARD_TYPE guard(mutex_);
138  return vector_.max_size();
139 }
140 
141 template<typename T>
143 {
144  MADARA_GUARD_TYPE guard(mutex_);
145  vector_.clear();
146 }
147 
148 template<typename T>
150 {
151  mutex_.MADARA_LOCK_LOCK();
152 }
153 
154 template<typename T>
156 {
157  mutex_.MADARA_LOCK_LOCK();
158 }
159 
160 template<typename T>
162 {
163  mutex_.MADARA_LOCK_UNLOCK();
164 }
165 
166 template<typename T>
168 {
169  mutex_.MADARA_LOCK_UNLOCK();
170 }
171 
172 #endif /* _MADARA_THREADSAFE_VECTOR_CPP_ */
Manages a thread safe STL vector.
void release(void) const
Unlocks the mutex.
T pop_back(void)
Returns the last element before removing it.
size_t max_size(void) const
returns the max size of the vector
T & operator[](size_t index)
Accesses an element of the vector.
size_t size(void) const
returns the current size of the vector
void resize(size_t new_size) const
Resizes the vector.
size_t erase(size_t index)
Erases an element.
void unlock(void) const
Unlocks the mutex.
void lock(void) const
Locks the mutex.
void acquire(void) const
Locks the mutex.
std::vector< T > vector_
the encapsulated vector
void clear(void)
Clears the vector.
void push_back(T &value)
Pushes a value onto the end of the vector.
void reserve(size_t new_size) const
Reserves a number of elements the vector.
const T & back(void) const
Returns the last element of the vector.
void operator=(const ThreadSafeVector &rhs)
Assignment operator.
MADARA_LOCK_TYPE mutex_
mutex for updating refcount_