Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <any>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <typeindex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::EventBus
|
||||
{
|
||||
|
||||
// ── EventBus ──────────────────────────────────────────────────────────────────
|
||||
// Synchronous publish/subscribe bus keyed by event type.
|
||||
// Handlers are invoked on the caller's thread inside publish().
|
||||
// Not thread-safe; use external locking if shared across threads.
|
||||
|
||||
class EventBus
|
||||
{
|
||||
public:
|
||||
using HandlerId = std::size_t;
|
||||
|
||||
// Subscribe to events of type Event. Returns a HandlerId for unsubscription.
|
||||
template <typename Event>
|
||||
HandlerId subscribe(std::function<void(const Event&)> handler)
|
||||
{
|
||||
const auto key = std::type_index{typeid(Event)};
|
||||
const HandlerId id = next_id_++;
|
||||
handlers_[key].push_back(Entry{id, [h = std::move(handler)](const std::any& ev) {
|
||||
h(std::any_cast<const Event&>(ev));
|
||||
}});
|
||||
return id;
|
||||
}
|
||||
|
||||
// Publish an event to all subscribers of its type.
|
||||
template <typename Event>
|
||||
void publish(const Event& event)
|
||||
{
|
||||
const auto key = std::type_index{typeid(Event)};
|
||||
const auto it = handlers_.find(key);
|
||||
if (it == handlers_.end()) return;
|
||||
for (const auto& e : it->second) e.fn(event);
|
||||
}
|
||||
|
||||
// Unsubscribe by id. O(n) per event type — acceptable for small handler counts.
|
||||
void unsubscribe(HandlerId id)
|
||||
{
|
||||
for (auto& [key, list] : handlers_) {
|
||||
const auto before = list.size();
|
||||
std::erase_if(list, [id](const Entry& e) { return e.id == id; });
|
||||
if (list.size() != before) return;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all handlers for a given event type.
|
||||
template <typename Event>
|
||||
void clear()
|
||||
{
|
||||
handlers_.erase(std::type_index{typeid(Event)});
|
||||
}
|
||||
|
||||
void clear_all() { handlers_.clear(); }
|
||||
|
||||
template <typename Event>
|
||||
[[nodiscard]] std::size_t subscriber_count() const
|
||||
{
|
||||
const auto it = handlers_.find(std::type_index{typeid(Event)});
|
||||
return it == handlers_.end() ? 0 : it->second.size();
|
||||
}
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
HandlerId id;
|
||||
std::function<void(const std::any&)> fn;
|
||||
};
|
||||
|
||||
std::unordered_map<std::type_index, std::vector<Entry>> handlers_;
|
||||
HandlerId next_id_{1};
|
||||
};
|
||||
|
||||
} // namespace Void::EventBus
|
||||
Reference in new issue
Block a user