|
| 1 | +/* |
| 2 | +
|
| 3 | +Thread-safe generic event library for C++ (17+) |
| 4 | +version 1.0.0 |
| 5 | +https://github.com/leventkaragol/libcpp-event-hub |
| 6 | +
|
| 7 | +If you encounter any issues, please submit a ticket at https://github.com/leventkaragol/libcpp-event-hub/issues |
| 8 | +
|
| 9 | +Copyright (c) 2024 Levent KARAGÖL |
| 10 | +
|
| 11 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +of this software and associated documentation files (the "Software"), to deal |
| 13 | +in the Software without restriction, including without limitation the rights |
| 14 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +copies of the Software, and to permit persons to whom the Software is |
| 16 | +furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | +The above copyright notice and this permission notice shall be included in all |
| 19 | +copies or substantial portions of the Software. |
| 20 | +
|
| 21 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | +SOFTWARE. |
| 28 | +
|
| 29 | +*/ |
| 30 | + |
| 31 | +#ifndef LIBCPP_EVENT_HUB_HPP |
| 32 | +#define LIBCPP_EVENT_HUB_HPP |
| 33 | + |
| 34 | +#include <string> |
| 35 | +#include <unordered_map> |
| 36 | +#include <functional> |
| 37 | +#include <shared_mutex> |
| 38 | +#include <any> |
| 39 | +#include <iostream> |
| 40 | + |
| 41 | +namespace lklibs |
| 42 | +{ |
| 43 | + /** |
| 44 | + * @brief Thread-safe generic Event hub class |
| 45 | + */ |
| 46 | + class EventHub |
| 47 | + { |
| 48 | + public: |
| 49 | + using ListenerFunction = std::function<void(const std::string&, const std::string&, const std::any&)>; |
| 50 | + |
| 51 | + using ListenerId = size_t; |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Get the singleton instance of the EventHub |
| 55 | + * |
| 56 | + * @return EventHub& |
| 57 | + */ |
| 58 | + static EventHub& getInstance() |
| 59 | + { |
| 60 | + static EventHub instance; |
| 61 | + return instance; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @brief Emit an event with generic type |
| 66 | + * |
| 67 | + * @tparam EventData: Type of the data to be sent with the event |
| 68 | + * @param eventName: Name of the event |
| 69 | + * @param sender: Sender of the event |
| 70 | + * @param data: Data to be sent with the event |
| 71 | + */ |
| 72 | + template <typename EventData> |
| 73 | + void emit(const std::string& eventName, const std::string& sender, const EventData& data) |
| 74 | + { |
| 75 | + std::shared_lock lock(mutex_); |
| 76 | + |
| 77 | + std::any eventData = data; |
| 78 | + |
| 79 | + if (listeners_.find(eventName) != listeners_.end()) |
| 80 | + { |
| 81 | + for (const auto& [id, listener] : listeners_.at(eventName)) |
| 82 | + { |
| 83 | + listener(eventName, sender, eventData); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (listeners_.find("*") != listeners_.end()) |
| 88 | + { |
| 89 | + for (const auto& [id, listener] : listeners_.at("*")) |
| 90 | + { |
| 91 | + listener(eventName, sender, eventData); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @brief Add a listener for an event |
| 98 | + * |
| 99 | + * @tparam EventData: Type of the data to be sent with the event |
| 100 | + * @param eventName: Name of the event |
| 101 | + * @param listener: Listener function to be called when the event is emitted |
| 102 | + * @return Listener Id |
| 103 | + */ |
| 104 | + template <typename EventData> |
| 105 | + ListenerId addListener(const std::string& eventName, std::function<void(const std::string&, const std::string&, const EventData&)> listener) |
| 106 | + { |
| 107 | + std::unique_lock lock(mutex_); |
| 108 | + |
| 109 | + auto id = nextListenerId_++; |
| 110 | + |
| 111 | + auto wrappedListener = [listener](const std::string& eventName, const std::string& sender, const std::any& data) |
| 112 | + { |
| 113 | + if (data.type() == typeid(EventData)) |
| 114 | + { |
| 115 | + listener(eventName, sender, std::any_cast<const EventData&>(data)); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + listeners_[eventName][id] = wrappedListener; |
| 120 | + |
| 121 | + return id; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @brief Remove a listener for an event |
| 126 | + * |
| 127 | + * @param eventName: Name of the event |
| 128 | + * @param listenerId: Id of the listener to be removed |
| 129 | + */ |
| 130 | + void removeListener(const std::string& eventName, ListenerId listenerId) |
| 131 | + { |
| 132 | + std::unique_lock lock(mutex_); |
| 133 | + |
| 134 | + if (listeners_.find(eventName) != listeners_.end()) |
| 135 | + { |
| 136 | + listeners_[eventName].erase(listenerId); |
| 137 | + |
| 138 | + if (listeners_[eventName].empty()) |
| 139 | + { |
| 140 | + listeners_.erase(eventName); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private: |
| 146 | + EventHub() = default; |
| 147 | + EventHub(const EventHub&) = delete; |
| 148 | + EventHub& operator=(const EventHub&) = delete; |
| 149 | + |
| 150 | + std::unordered_map<std::string, std::unordered_map<ListenerId, ListenerFunction>> listeners_; |
| 151 | + mutable std::shared_mutex mutex_; |
| 152 | + ListenerId nextListenerId_ = 0; |
| 153 | + }; |
| 154 | +} |
| 155 | + |
| 156 | +#endif // LIBCPP_EVENT_HUB_HPP |
0 commit comments