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,139 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Observable
|
||||
{
|
||||
|
||||
// ── Handle — opaque subscription identifier ───────────────────────────────────
|
||||
|
||||
struct Handle
|
||||
{
|
||||
std::uint64_t id{0};
|
||||
[[nodiscard]] bool valid() const noexcept { return id != 0; }
|
||||
[[nodiscard]] bool operator==(const Handle&) const noexcept = default;
|
||||
};
|
||||
|
||||
// ── Observable<T> ─────────────────────────────────────────────────────────────
|
||||
// A value that observers can subscribe to via on_change().
|
||||
// Observers receive (old_value, new_value) on every set() call that changes
|
||||
// the stored value.
|
||||
|
||||
template <typename T>
|
||||
class Observable
|
||||
{
|
||||
public:
|
||||
using Listener = std::function<void(const T& old_val, const T& new_val)>;
|
||||
|
||||
explicit Observable(T initial = {}) : value_{std::move(initial)} {}
|
||||
|
||||
[[nodiscard]] const T& get() const noexcept { return value_; }
|
||||
[[nodiscard]] operator const T&() const noexcept { return value_; }
|
||||
|
||||
void set(T new_val)
|
||||
{
|
||||
if (new_val == value_) return;
|
||||
T old = std::move(value_);
|
||||
value_ = std::move(new_val);
|
||||
notify(old, value_);
|
||||
}
|
||||
|
||||
Observable& operator=(T new_val)
|
||||
{
|
||||
set(std::move(new_val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Handle on_change(Listener listener)
|
||||
{
|
||||
const Handle h{++next_id_};
|
||||
listeners_.push_back({h, std::move(listener)});
|
||||
return h;
|
||||
}
|
||||
|
||||
bool unsubscribe(Handle h)
|
||||
{
|
||||
const auto it =
|
||||
std::ranges::find_if(listeners_,
|
||||
[&](const auto& e) { return e.h == h; });
|
||||
if (it == listeners_.end()) return false;
|
||||
listeners_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t listener_count() const noexcept
|
||||
{
|
||||
return listeners_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
void notify(const T& old_val, const T& new_val)
|
||||
{
|
||||
for (auto& [h, fn] : listeners_) fn(old_val, new_val);
|
||||
}
|
||||
|
||||
T value_;
|
||||
std::uint64_t next_id_{0};
|
||||
|
||||
struct Entry
|
||||
{
|
||||
Handle h;
|
||||
Listener fn;
|
||||
};
|
||||
std::vector<Entry> listeners_;
|
||||
};
|
||||
|
||||
// ── Signal<Args...> ───────────────────────────────────────────────────────────
|
||||
// An event signal that observers connect to. emit() calls all connected slots.
|
||||
|
||||
template <typename... Args>
|
||||
class Signal
|
||||
{
|
||||
public:
|
||||
using Slot = std::function<void(const Args&...)>;
|
||||
|
||||
Handle connect(Slot slot)
|
||||
{
|
||||
const Handle h{++next_id_};
|
||||
slots_.push_back({h, std::move(slot)});
|
||||
return h;
|
||||
}
|
||||
|
||||
bool disconnect(Handle h)
|
||||
{
|
||||
const auto it =
|
||||
std::ranges::find_if(slots_,
|
||||
[&](const auto& e) { return e.h == h; });
|
||||
if (it == slots_.end()) return false;
|
||||
slots_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
void emit(const Args&... args) const
|
||||
{
|
||||
for (const auto& [h, fn] : slots_) fn(args...);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t slot_count() const noexcept
|
||||
{
|
||||
return slots_.size();
|
||||
}
|
||||
|
||||
void clear() noexcept { slots_.clear(); }
|
||||
|
||||
private:
|
||||
std::uint64_t next_id_{0};
|
||||
|
||||
struct Entry
|
||||
{
|
||||
Handle h;
|
||||
Slot fn;
|
||||
};
|
||||
std::vector<Entry> slots_;
|
||||
};
|
||||
|
||||
} // namespace Void::Observable
|
||||
Reference in new issue
Block a user