Antkeeper  0.0.1
channel.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_CHANNEL_HPP
21 #define ANTKEEPER_EVENT_CHANNEL_HPP
22 
23 #include <functional>
24 #include <list>
25 #include <memory>
26 #include <utility>
30 #include <engine/event/queue.hpp>
31 
32 namespace event {
33 
34 template <class T>
35 class publisher;
36 
42 template <class T>
43 class channel
44 {
45 public:
47  typedef T message_type;
48 
51 
59  [[nodiscard]] std::shared_ptr<subscription> subscribe(subscriber_type&& subscriber)
60  {
61  // Construct shared pointer to subscriber function
62  std::shared_ptr<subscriber_type> shared_subscriber = std::make_shared<subscriber_type>(std::move(subscriber));
63 
64  // Append subscriber to subscriber list and store iterator
65  auto iterator = subscribers.insert(subscribers.end(), shared_subscriber);
66 
67  // Construct and return a shared subscription object which removes the subscriber from the subscriber list when unsubscribed or destructed
68  return std::make_shared<subscription>
69  (
70  std::static_pointer_cast<void>(shared_subscriber),
71  [this, iterator = std::move(iterator)]
72  {
73  this->subscribers.erase(iterator);
74  }
75  );
76  }
77 
85  [[nodiscard]] std::shared_ptr<subscription> subscribe(event::dispatcher& dispatcher)
86  {
87  return subscribe
88  (
89  [&dispatcher](const message_type& message)
90  {
92  }
93  );
94  }
95 
103  [[nodiscard]] std::shared_ptr<subscription> subscribe(event::queue& queue)
104  {
105  return subscribe
106  (
107  [&queue](const message_type& message)
108  {
109  queue.enqueue<message_type>(message);
110  }
111  );
112  }
113 
114 private:
115  friend class publisher<T>;
116 
118  std::list<std::shared_ptr<subscriber_type>> subscribers;
119 };
120 
121 } // namespace event
122 
123 #endif // ANTKEEPER_EVENT_CHANNEL_HPP
Channel through which messages are published.
Definition: channel.hpp:44
std::shared_ptr< subscription > subscribe(subscriber_type &&subscriber)
Subscribes a function object to messages published through this channel.
Definition: channel.hpp:59
std::shared_ptr< subscription > subscribe(event::dispatcher &dispatcher)
Subscribes a message dispatcher to messages published through this channel.
Definition: channel.hpp:85
T message_type
Message type.
Definition: channel.hpp:47
subscriber< message_type > subscriber_type
Subscriber function object type.
Definition: channel.hpp:50
Forwards messages from publishers to subscribers.
Definition: dispatcher.hpp:37
void dispatch(const T &message) const
Dispatches a message to subscribers of the message type.
Definition: dispatcher.hpp:77
Publishes messages to subscribers.
Definition: publisher.hpp:36
Collects messages from publishers to be dispatched to subscribers when desired.
Definition: queue.hpp:33
void enqueue(const T &message)
Adds a message to the queue, to be distributed later.
Definition: queue.hpp:43
Publish-subscribe messaging.
Definition: channel.hpp:32
std::function< void(const T &)> subscriber
Subscriber function object type.
Definition: subscriber.hpp:33