Devel Lib
SafeQueue.h
1 #pragma once
2 
3 #include <queue>
4 
5 #include "Threading/LockGuard/LockGuard.h"
6 #include "Core/Typedef.h"
7 #include "Core/Global.h"
8 #include "Core/Exceptions.h"
9 
14 #define SafeQueueLockGuard(x) RecursiveLockGuard(x.mutex())
15 
20 #define SafeQueueLock(x) x.mutex().lock()
21 
26 #define SafeQueueUnlock(x) x.mutex().unlock()
27 
30 namespace Devel::Threading {
103  template<class T>
104  class CSafeQueue {
105  public:
108  const CMutex &mutex() const {
109  return this->m_oMutex;
110  }
111 
114  size_t size() const {
115  return this->m_aQueue.size();
116  }
117 
120  bool isEmpty() const {
121  return this->m_aQueue.empty();
122  }
123 
124  public:
127  void enqueue(const T &i_tValue) {
128  RecursiveLockGuard(this->m_oMutex);
129  this->m_aQueue.push(i_tValue);
130  }
131 
134  void enqueue(T &&i_tValue) {
135  RecursiveLockGuard(this->m_oMutex);
136  this->m_aQueue.push(std::move(i_tValue));
137  }
138 
139  public:
143  T dequeue(const bool i_fMove = true) {
144  RecursiveLockGuard(this->m_oMutex);
145 
146  T tValue = this->front(i_fMove);
147  this->pop();
148 
149  return tValue;
150  }
151 
152  public:
156  T front(const bool i_fMove = false) {
157  RecursiveLockGuard(this->m_oMutex);
158 
159  if (this->isEmpty()) {
160  throw NoEntryFoundException;
161  }
162 
163  return (i_fMove ? std::move(this->m_aQueue.front()) : this->m_aQueue.front());
164  }
165 
167  void pop() {
168  RecursiveLockGuard(this->m_oMutex);
169 
170  if (this->isEmpty()) {
171  throw NoEntryFoundException;
172  }
173 
174  this->m_aQueue.pop();
175  }
176 
178  void clear() {
179  RecursiveLockGuard(this->m_oMutex);
180 
181  try {
182  while (true) {
183  this->pop();
184  }
185  } catch (const std::range_error &) {}
186  }
187 
188  private:
191  std::queue<T> m_aQueue;
192 
195  CMutex m_oMutex;
196  };
197 };
This file contains a series of type definitions for the codebase.
A class for handling recursive mutexes.
Definition: Mutex.h:47
Definition: SafeQueue.h:104
void enqueue(T &&i_tValue)
Enqueues an rvalue reference to an element into the safe queue.
Definition: SafeQueue.h:134
bool isEmpty() const
Checks if the safe queue is empty.
Definition: SafeQueue.h:120
const CMutex & mutex() const
Returns a const reference to the mutex associated with the safe queue.
Definition: SafeQueue.h:108
void pop()
Removes the front element from the safe queue.
Definition: SafeQueue.h:167
void clear()
Removes all elements from the safe queue.
Definition: SafeQueue.h:178
T front(const bool i_fMove=false)
Returns a reference to the front element of the safe queue without removing it.
Definition: SafeQueue.h:156
T dequeue(const bool i_fMove=true)
Dequeues and returns an element from the safe queue.
Definition: SafeQueue.h:143
void enqueue(const T &i_tValue)
Enqueues an element into the safe queue.
Definition: SafeQueue.h:127
size_t size() const
Returns the number of elements in the safe queue.
Definition: SafeQueue.h:114
The namespace encapsulating threading related classes and functions in the Devel framework.