|
| 1 | +#include <algorithm> |
| 2 | +#include <any> |
| 3 | +#include <functional> |
| 4 | +#include <iostream> |
| 5 | +#include <map> |
| 6 | +#include <mutex> |
| 7 | +#include <shared_mutex> |
| 8 | +#include <string> |
| 9 | +#include <tuple> |
| 10 | +#include <type_traits> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace primordial_core { |
| 14 | + |
| 15 | +template <typename T> |
| 16 | +struct function_traits; |
| 17 | + |
| 18 | +template <typename R, typename... Args> |
| 19 | +struct function_traits<R (*)(Args...)> { |
| 20 | + using args_type = std::tuple<Args...>; |
| 21 | +}; |
| 22 | + |
| 23 | +template <typename R, typename... Args> |
| 24 | +struct function_traits<std::function<R(Args...)>> { |
| 25 | + using args_type = std::tuple<Args...>; |
| 26 | +}; |
| 27 | + |
| 28 | +template <typename C, typename R, typename... Args> |
| 29 | +struct function_traits<R (C::*)(Args...) const> { |
| 30 | + using args_type = std::tuple<Args...>; |
| 31 | +}; |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +struct function_traits : public function_traits<decltype(&T::operator())> {}; |
| 35 | + |
| 36 | +class CallbackManager { |
| 37 | + template <typename... Args> |
| 38 | + struct CallbackList { |
| 39 | + struct Entry { |
| 40 | + int priority; |
| 41 | + std::function<void(Args...)> func; |
| 42 | + uint64_t id; |
| 43 | + |
| 44 | + static bool compare(const Entry& a, const Entry& b) { |
| 45 | + return a.priority > b.priority; |
| 46 | + } |
| 47 | + }; |
| 48 | + std::vector<Entry> list; |
| 49 | + |
| 50 | + void add(int p, std::function<void(Args...)> f, uint64_t id) { |
| 51 | + list.push_back({p, std::move(f), id}); |
| 52 | + std::stable_sort(list.begin(), list.end(), Entry::compare); |
| 53 | + } |
| 54 | + |
| 55 | + void invoke(Args... args) const { |
| 56 | + for (const auto& e : list) { |
| 57 | + e.func(args...); |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + std::map<std::string, std::any> events; |
| 63 | + mutable std::shared_mutex mutex; |
| 64 | + uint64_t next_id = 0; |
| 65 | + |
| 66 | + template <typename... Args> |
| 67 | + void register_impl(const std::string& name, std::function<void(Args...)> f, int p) { |
| 68 | + std::unique_lock lock(mutex); |
| 69 | + using ListT = CallbackList<Args...>; |
| 70 | + |
| 71 | + if (events.find(name) == events.end()) { |
| 72 | + events[name] = ListT{}; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + auto& container = std::any_cast<ListT&>(events[name]); |
| 77 | + container.add(p, std::move(f), ++next_id); |
| 78 | + } catch (const std::bad_any_cast&) { |
| 79 | + throw std::runtime_error("Signature mismatch for event: " + name); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + template <typename Callable, typename... Args> |
| 84 | + void deduce_and_register(const std::string& name, Callable&& c, int p, std::tuple<Args...>) { |
| 85 | + std::function<void(Args...)> f = std::forward<Callable>(c); |
| 86 | + register_impl<Args...>(name, std::move(f), p); |
| 87 | + } |
| 88 | + |
| 89 | +public: |
| 90 | + template <typename Callable> |
| 91 | + void addCallback(const std::string& name, Callable&& cb, int priority = 0) { |
| 92 | + using traits = function_traits<std::decay_t<Callable>>; |
| 93 | + using ArgsT = typename traits::args_type; |
| 94 | + deduce_and_register(name, std::forward<Callable>(cb), priority, ArgsT{}); |
| 95 | + } |
| 96 | + |
| 97 | + template <typename... Args> |
| 98 | + void invokeCallback(const std::string& name, Args&&... args) { |
| 99 | + std::shared_lock lock(mutex); |
| 100 | + auto it = events.find(name); |
| 101 | + if (it == events.end()) { |
| 102 | + std::cerr << "Warning: Event '" << name << "' not found\n"; |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + using ListT = CallbackList<std::decay_t<Args>...>; |
| 107 | + |
| 108 | + try { |
| 109 | + const auto& container = std::any_cast<const ListT&>(it->second); |
| 110 | + container.invoke(std::forward<Args>(args)...); |
| 111 | + } catch (const std::bad_any_cast&) { |
| 112 | + std::cerr << "Error: Invoke arguments do not match stored signature for '" << name << "'\n"; |
| 113 | + } |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +} // namespace primordial_core |
0 commit comments