29 using Cancel = std::function<void()>;
50 void dispose() noexcept;
53 bool disposed() const noexcept {
return disposed_; }
60 bool disposed_ =
true;
80template <
typename... Args>
84 using Callback = std::function<void(
const Args &...)>;
105 auto entry = std::make_shared<Entry>();
106 entry->id = state_->nextId++;
107 entry->callback = std::move(
callback);
108 state_->entries.push_back(entry);
110 const std::weak_ptr<State> weakState = state_;
113 entry->active =
false;
114 if (
const auto state = weakState.lock()) {
116 std::erase_if(state->entries,
117 [&entry](const std::shared_ptr<Entry> &candidate) { return candidate == entry; });
128 const auto snapshot = state_->entries;
129 for (
const auto &entry : snapshot) {
130 if (!entry->active || !entry->callback)
continue;
148 template <
typename FailureHandler>
150 [[nodiscard]] std::size_t
notifyChecked(FailureHandler &&onFailure,
const Args &...args) {
151 const auto snapshot = state_->entries;
152 std::size_t failures = 0;
153 for (
const auto &entry : snapshot) {
154 if (!entry->active || !entry->callback)
continue;
156 if (!entry->active || !
callback)
continue;
162 std::forward<FailureHandler>(onFailure)();
169 std::size_t
size() const noexcept {
170 std::size_t
count = 0;
171 for (
const auto &entry : state_->entries)
172 if (entry->active) ++
count;
179 std::erase_if(state_->entries, [](
const std::shared_ptr<Entry> &entry) { return !entry->active; });
184 std::uint64_t
id = 0;
190 std::uint64_t nextId = 1;
191 std::vector<std::shared_ptr<Entry>>
entries;
194 std::shared_ptr<State> state_;
std::unordered_map< std::string, QuestRuntime > entries
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Thread-affine callback registry with mutation-safe dispatch.
Subscription subscribe(Callback callback)
Registers a callback owned by the returned Subscription.
std::size_t notifyChecked(FailureHandler &&onFailure, const Args &...args)
Notifies all snapshotted callbacks and contains unknown exceptions.
Observer(Observer &&)=delete
Observer & operator=(const Observer &)=delete
std::function< void(const Args &...)> Callback
void compactInactive()
Removes inactive entries without invoking user callbacks.
Observer()
Constructs a Observer.
std::size_t size() const noexcept
Returns the number of currently active registrations.
Observer(const Observer &)=delete
~Observer()=default
Releases Observer resources.
Observer & operator=(Observer &&)=delete
void notify(const Args &...args)
Synchronously notifies a snapshot of current registrations.
Move-only RAII token that owns one observer registration.
Subscription() noexcept=default
Creates an inactive token.
bool isDisposed() const noexcept
Compatibility spelling for disposed().
std::function< void()> Cancel
Build metadata (engine git commit, build time, third-party version).