/* * 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_CHANNEL_HPP #define ANTKEEPER_EVENT_CHANNEL_HPP #include #include #include #include #include #include #include #include namespace event { template class publisher; /** * Channel through which messages are published. * * @tparam T Message type. */ template class channel { public: /// Message type. typedef T message_type; /// Subscriber function object type. typedef subscriber subscriber_type; /** * Subscribes a function object to messages published through this channel. * * @param subscriber Subscriber function object which will received published messages. * * @return Shared subscription object which will unsubscribe the subscriber on destruction. */ [[nodiscard]] std::shared_ptr subscribe(subscriber_type&& subscriber) { // Construct shared pointer to subscriber function std::shared_ptr shared_subscriber = std::make_shared(std::move(subscriber)); // Append subscriber to subscriber list and store iterator auto iterator = subscribers.insert(subscribers.end(), shared_subscriber); // Construct and return a shared subscription object which removes the subscriber from the subscriber list when unsubscribed or destructed return std::make_shared ( std::static_pointer_cast(shared_subscriber), [this, iterator = std::move(iterator)] { this->subscribers.erase(iterator); } ); } /** * Subscribes a message dispatcher to messages published through this channel. * * @param dispatcher Message dispatcher which will received published messages. * * @return Shared subscription object which will unsubscribe the queue on destruction. */ [[nodiscard]] std::shared_ptr subscribe(event::dispatcher& dispatcher) { return subscribe ( [&dispatcher](const message_type& message) { dispatcher.dispatch(message); } ); } /** * Subscribes a message queue to messages published through this channel. * * @param queue Message queue which will received published messages. * * @return Shared subscription object which will unsubscribe the queue on destruction. */ [[nodiscard]] std::shared_ptr subscribe(event::queue& queue) { return subscribe ( [&queue](const message_type& message) { queue.enqueue(message); } ); } private: friend class publisher; /// List of subscribers. std::list> subscribers; }; } // namespace event #endif // ANTKEEPER_EVENT_CHANNEL_HPP