29 const uint64_t
id = _nextId++;
30 _entries.push_back(std::make_unique<Entry>(Entry{.Id = id, .Item = std::move(item)}));
38 const uint64_t
id =
Add(std::move(item));
45 const auto it = Locate(
id);
46 if (it == _entries.end() || (*it)->Removed)
52 if (_dispatchDepth > 0)
55 (*it)->Removed =
true;
67 if (_dispatchDepth == 0)
72 for (
auto& entry : _entries)
74 entry->Removed =
true;
78 bool Empty()
const {
return _live == 0; }
80 size_t Size()
const {
return _live; }
85 const auto it = Locate(
id);
86 return it != _entries.end() && !(*it)->Removed ? &(*it)->Item :
nullptr;
91 void DispatchIf(std::predicate<const T&>
auto&& pred, std::invocable<T&>
auto&& fn)
93 const size_t count = _entries.size();
99 DispatchScope scope{*
this};
100 for (
size_t i = 0; i < count; ++i)
103 Entry& entry = *_entries[i];
104 if (!entry.Removed && pred(entry.Item))
114 DispatchIf([](
const T&) {
return true; }, std::forward<decltype(fn)>(fn));
122 bool Removed =
false;
128 explicit DispatchScope(CallbackRegistry& registry) : Registry(registry) { ++Registry._dispatchDepth; }
132 if (--Registry._dispatchDepth == 0 && Registry._entries.size() != Registry._live)
134 std::erase_if(Registry._entries, [](
const auto& entry) { return entry->Removed; });
137 DispatchScope(
const DispatchScope&) =
delete;
138 DispatchScope& operator=(
const DispatchScope&) =
delete;
140 CallbackRegistry& Registry;
144 auto Locate(uint64_t
id)
146 const auto it = std::ranges::lower_bound(_entries,
id, {}, [](
const auto& entry) {
return entry->Id; });
147 return it != _entries.end() && (*it)->Id ==
id ? it : _entries.end();
150 std::vector<std::unique_ptr<Entry>> _entries;
152 int _dispatchDepth = 0;
153 uint64_t _nextId = 1;