/*
* Copyright (C) 2023 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see .
*/
#ifndef ANTKEEPER_EVENT_PUBLISHER_HPP
#define ANTKEEPER_EVENT_PUBLISHER_HPP
#include
#include
#include
namespace event {
/**
* Publishes messages to subscribers.
*
* @tparam T Message type.
*/
template
class publisher
{
public:
/// Message type.
typedef T message_type;
/// Channel type.
typedef channel channel_type;
/**
* Publishes a message.
*
* @tparam ExecutionPolicy Execution policy type.
*
* @param policy Execution policy to use.
* @param message Message to publish.
*/
/// @{
template
void publish(ExecutionPolicy&& policy, const message_type& message) const
{
std::for_each
(
policy,
std::begin(m_channel.subscribers),
std::end(m_channel.subscribers),
[&](const auto& subscriber)
{
(*subscriber)(message);
}
);
}
void publish(const message_type& message) const
{
publish(std::execution::seq, message);
}
/// @}
/**
* Returns the channel through which messages are published.
*/
/// @{
[[nodiscard]] inline const channel_type& channel() const noexcept
{
return m_channel;
}
[[nodiscard]] inline channel_type& channel() noexcept
{
return m_channel;
}
/// @}
private:
channel_type m_channel;
};
} // namespace event
#endif // ANTKEEPER_EVENT_PUBLISHER_HPP