Antkeeper  0.0.1
queue.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_EVENT_QUEUE_HPP
21 #define ANTKEEPER_EVENT_QUEUE_HPP
22 
24 #include <functional>
25 #include <list>
26 
27 namespace event {
28 
32 class queue: public dispatcher
33 {
34 public:
42  template <class T>
43  void enqueue(const T& message)
44  {
45  messages.emplace_back
46  (
47  [this, message]()
48  {
49  this->dispatch<T>(message);
50  }
51  );
52  }
53 
57  void flush()
58  {
59  while (!messages.empty())
60  {
61  messages.front()();
62  messages.pop_front();
63  }
64  }
65 
69  void clear()
70  {
71  messages.clear();
72  }
73 
77  [[nodiscard]] inline bool empty() const noexcept
78  {
79  return messages.empty();
80  }
81 
82 private:
83  std::list<std::function<void()>> messages;
84 };
85 
86 } // namespace event
87 
88 #endif // ANTKEEPER_EVENT_QUEUE_HPP
Forwards messages from publishers to subscribers.
Definition: dispatcher.hpp:37
Collects messages from publishers to be dispatched to subscribers when desired.
Definition: queue.hpp:33
bool empty() const noexcept
Returns true if there are no messages in the queue, false otherwise.
Definition: queue.hpp:77
void flush()
Dispatches queued messages, in FIFO order, to subscribers.
Definition: queue.hpp:57
void enqueue(const T &message)
Adds a message to the queue, to be distributed later.
Definition: queue.hpp:43
void clear()
Removes all messages from the queue.
Definition: queue.hpp:69
Publish-subscribe messaging.
Definition: channel.hpp:32