Antkeeper  0.0.1
publisher.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_PUBLISHER_HPP
21 #define ANTKEEPER_EVENT_PUBLISHER_HPP
22 
23 #include <algorithm>
24 #include <execution>
25 #include <engine/event/channel.hpp>
26 
27 namespace event {
28 
34 template <class T>
35 class publisher
36 {
37 public:
39  typedef T message_type;
40 
43 
53  template <class ExecutionPolicy>
54  void publish(ExecutionPolicy&& policy, const message_type& message) const
55  {
56  std::for_each
57  (
58  policy,
59  std::begin(m_channel.subscribers),
60  std::end(m_channel.subscribers),
61  [&](const auto& subscriber)
62  {
63  (*subscriber)(message);
64  }
65  );
66  }
67 
68  void publish(const message_type& message) const
69  {
70  publish(std::execution::seq, message);
71  }
73 
78  [[nodiscard]] inline const channel_type& channel() const noexcept
79  {
80  return m_channel;
81  }
82 
83  [[nodiscard]] inline channel_type& channel() noexcept
84  {
85  return m_channel;
86  }
88 
89 private:
90  channel_type m_channel;
91 };
92 
93 } // namespace event
94 
95 #endif // ANTKEEPER_EVENT_PUBLISHER_HPP
Publishes messages to subscribers.
Definition: publisher.hpp:36
T message_type
Message type.
Definition: publisher.hpp:39
void publish(const message_type &message) const
Publishes a message.
Definition: publisher.hpp:68
void publish(ExecutionPolicy &&policy, const message_type &message) const
Publishes a message.
Definition: publisher.hpp:54
channel_type & channel() noexcept
Returns the channel through which messages are published.
Definition: publisher.hpp:83
channel< message_type > channel_type
Channel type.
Definition: publisher.hpp:42
const channel_type & channel() const noexcept
Returns the channel through which messages are published.
Definition: publisher.hpp:78
Publish-subscribe messaging.
Definition: channel.hpp:32
std::function< void(const T &)> subscriber
Subscriber function object type.
Definition: subscriber.hpp:33