MADARA  3.2.3
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>
34 void
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
49 {
50  MADARA_GUARD_TYPE guard (mutex_);
51 
52  if (this->vector_ != &rhs)
53  {
54  vector_ = rhs;
55  }
56 }
57 
58 template <typename T>
60 {
61  MADARA_GUARD_TYPE guard (mutex_);
62  return vector_[index];
63 }
64 
65 template <typename T>
67 {
68  MADARA_GUARD_TYPE guard (mutex_);
69  return vector_[index];
70 }
71 
72 template <typename T>
74 {
75  MADARA_GUARD_TYPE guard (mutex_);
76  vector_.push_back (value);
77 }
78 
79 template <typename T>
81 {
82  MADARA_GUARD_TYPE guard (mutex_);
83  return vector_.back ();
84 }
85 
86 template <typename T>
88 {
89  MADARA_GUARD_TYPE guard (mutex_);
90  T result (vector_.back ());
91 
92  vector_.pop_back ();
93  return result;
94 }
95 
96 template <typename T>
98 {
99  MADARA_GUARD_TYPE guard (mutex_);
100 
101  if (index < vector_.size ())
102  vector_.erase (vector_.begin () + index);
103 
104  return vector_.size ();
105 }
106 
107 template <typename T>
109 {
110  MADARA_GUARD_TYPE guard (mutex_);
111  return vector_.back ();
112 }
113 
114 template <typename T>
116 {
117  MADARA_GUARD_TYPE guard (mutex_);
118  vector_.resize (new_size);
119 }
120 
121 template <typename T>
123 {
124  MADARA_GUARD_TYPE guard (mutex_);
125  vector_.reserve (new_size);
126 }
127 
128 template <typename T>
130 {
131  MADARA_GUARD_TYPE guard (mutex_);
132  return vector_.size ();
133 }
134 
135 template <typename T>
137 {
138  MADARA_GUARD_TYPE guard (mutex_);
139  return vector_.max_size ();
140 }
141 
142 template <typename T>
144 {
145  MADARA_GUARD_TYPE guard (mutex_);
146  vector_.clear ();
147 }
148 
149 template <typename T>
151 {
152  mutex_.MADARA_LOCK_LOCK ();
153 }
154 
155 template <typename T>
157 {
158  mutex_.MADARA_LOCK_LOCK ();
159 }
160 
161 template <typename T>
163 {
164  mutex_.MADARA_LOCK_UNLOCK ();
165 }
166 
167 template <typename T>
169 {
170  mutex_.MADARA_LOCK_UNLOCK ();
171 }
172 
173 #endif /* _MADARA_THREADSAFE_VECTOR_CPP_ */
MADARA_LOCK_TYPE mutex_
mutex for updating refcount_
void lock(void) const
Locks the mutex.
void resize(size_t new_size) const
Resizes the vector.
void push_back(T &value)
Pushes a value onto the end of the vector.
void acquire(void) const
Locks the mutex.
size_t max_size(void) const
returns the max size of the vector
void release(void) const
Unlocks the mutex.
virtual ~ThreadSafeVector(void)
Destructor.
void unlock(void) const
Unlocks the mutex.
const T & back(void) const
Returns the last element of the vector.
size_t size(void) const
returns the current size of the vector
T & operator[](size_t index)
Accesses an element of the vector.
T pop_back(void)
Returns the last element before removing it.
void clear(void)
Clears the vector.
void reserve(size_t new_size) const
Reserves a number of elements the vector.
size_t erase(size_t index)
Erases an element.
Manages a thread safe STL vector.
std::vector< T > vector_
the encapsulated vector
void operator=(const ThreadSafeVector &rhs)
Assignment operator.