Support Class Library
A set of tools providing classes and utility
All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
AsyncQueue.h
Go to the documentation of this file.
1 #pragma once
2 #include <queue>
3 #include <deque>
4 #include <initializer_list>
5 #include <scl/async/with.h>
6 #include <scl/utils/Optional.h>
7 
8 namespace scl{
9  namespace async{
10  template <class T, class Lock = std::mutex, template<class> class Guard = std::lock_guard, class Container = std::deque<T>>
11  class AsyncQueue{
12  public:
13  using queue_type = std::queue<T, Container>;
14  using size_type = typename queue_type::size_type;
15  using value_type = typename queue_type::value_type;
16 
19 
20  protected:
22 
23  public:
24  AsyncQueue() : queue_{} {
25  }
26 
27  AsyncQueue(AsyncQueue&&) noexcept = default;
28  AsyncQueue& operator=(AsyncQueue&&) noexcept = default;
29  AsyncQueue(const AsyncQueue&) = delete;
30  AsyncQueue& operator=(const AsyncQueue&) = delete;
31 
32  AsyncQueue(std::initializer_list<value_type> list) : AsyncQueue(){
33  with(this->queue_, [&](queue_type& queue){
34  for(auto&& elem : list)
35  queue.emplace(std::move(elem));
36  });
37  }
38 
39 
44  size_type size() const{
45  return with(this->queue_, [&](const queue_type& queue){
46  return queue.size();
47  });
48  }
49 
53  size_type length() const{
54  return this->size();
55  }
56 
61  bool isEmpty() const{
62  return this->size() <= 0;
63  }
64 
69  optional_type front() const{ //TODO: Modify to use optional ref?
70  return with(this->queue_, [&](const queue_type& queue){
71  return queue.empty() ? optional_type{} : optional_type{queue.front()};
72  });
73  }
74 
80  return with(this->queue_, [&](const queue_type& queue){
81  return queue.empty() ? optional_type{} : optional_type{queue.back()};
82  });
83  }
84 
88  optional_type first() const{ return this->front(); }
89  optional_type head() const{ return this->front(); }
90 
94  optional_type last() const{ return this->back(); }
95  optional_type tail() const{ return this->back(); }
96 
102  AsyncQueue& push(const value_type& elem){
103  return with(this->queue_, [&](queue_type& queue){
104  queue.emplace(elem);
105  return *this;
106  });
107  }
108 
115  return with(this->queue_, [&](queue_type& queue){
116  queue.emplace(std::move(elem));
117  return *this;
118  });
119  }
120 
127  template <class... Args>
128  AsyncQueue& pushEmplace(Args&&... args){
129  return with(this->queue_, [&](queue_type& queue){
130  queue.emplace(std::forward<Args>(args)...);
131  return this;
132  });
133  }
134 
140  return with(this->queue_, [](queue_type& queue){
141  optional_type ret = queue.empty() ? optional_type{} : optional_type{queue.front()};
142  queue.pop();
143  return ret;
144  });
145  }
146 
147 
151  AsyncQueue& queue(const value_type& elem){ return this->push(elem); }
152  AsyncQueue& enqueue(const value_type& elem){ return this->push(elem); }
153  AsyncQueue& queue(value_type&& elem){ return this->push(std::move(elem)); }
154  AsyncQueue& enqueue(value_type&& elem){ return this->push(std::move(elem)); }
155 
159  optional_type dequeue(){ return this->pop(); }
160  };
161  }
162 }
AsyncQueue & push(const value_type &elem)
Add an element to the queue by copy.
Definition: AsyncQueue.h:102
AsyncQueue & operator=(AsyncQueue &&) noexcept=default
optional_type pop()
Removes the first element of the queue.
Definition: AsyncQueue.h:139
optional_type front() const
Retrieve the front element of the queue.
Definition: AsyncQueue.h:69
optional_type last() const
Alias for AsyncQueue::back.
Definition: AsyncQueue.h:94
AsyncQueue & queue(const value_type &elem)
Alias for AsyncQueue::push.
Definition: AsyncQueue.h:151
optional_type back() const
Retrieve the back element of the queue.
Definition: AsyncQueue.h:79
Global namespace of the SCL.
Definition: alias.hpp:3
AsyncQueue & enqueue(const value_type &elem)
Definition: AsyncQueue.h:152
optional_type first() const
Alias for AsyncQueue::front.
Definition: AsyncQueue.h:88
AsyncQueue & queue(value_type &&elem)
Definition: AsyncQueue.h:153
bool isEmpty() const
Determine whether or not the queue is empty.
Definition: AsyncQueue.h:61
optional_type tail() const
Definition: AsyncQueue.h:95
typename queue_type::value_type value_type
Definition: AsyncQueue.h:15
AsyncQueue & enqueue(value_type &&elem)
Definition: AsyncQueue.h:154
guarded_type queue_
Definition: AsyncQueue.h:21
AsyncQueue & pushEmplace(Args &&... args)
Construct and add an element to the queue.
Definition: AsyncQueue.h:128
typename queue_type::size_type size_type
Definition: AsyncQueue.h:14
AsyncQueue & push(value_type &&elem)
Add an element to the queue by move.
Definition: AsyncQueue.h:114
auto with(Resource &&res, F &&f) -> scl::tools::meta ::return_t< F >
Execute a transaction in a guarded manner.
Definition: with.h:14
size_type size() const
Determine the size of the queue.
Definition: AsyncQueue.h:44
optional_type dequeue()
Alias for AsyncQueue::pop.
Definition: AsyncQueue.h:159
size_type length() const
Alias for AsyncQueue::size.
Definition: AsyncQueue.h:53
optional_type head() const
Definition: AsyncQueue.h:89
std::queue< T, Container > queue_type
Definition: AsyncQueue.h:13
AsyncQueue(std::initializer_list< value_type > list)
Definition: AsyncQueue.h:32