载入中...
搜索中...
未找到
RuntimeRegistry.h
浏览该文件的文档.
1#pragma once
2
12#include "common/Export.h"
13#include "common/Result.h"
15
16#include <atomic>
17#include <cstdint>
18#include <memory>
19#include <optional>
20#include <type_traits>
21#include <utility>
22#include <vector>
23
24namespace eve::script {
25
32template <class T>
33using Owned = std::unique_ptr<T>;
34
35namespace detail {
36
38using OwnedInstanceDestroy = void (*)(void*) noexcept;
39
41template <class T>
43void ownedInstanceDestroy(void* pointer) noexcept {
44 delete static_cast<T*>(pointer);
45}
46
49 std::uint32_t index = 0;
50 std::uint32_t generation = 0;
51};
52
68public:
70 explicit RuntimeSlotStore(OwnedInstanceDestroy destroy) noexcept : destroy_(destroy), ownerEpoch_(nextEpoch()) {}
71
74 // A RuntimePin holds this store's address, so relocating the store would leave that pin
75 // unpinning the moved-from store while the payload lives in the destination. The store
76 // must therefore stay at a fixed address for its whole lifetime.
81
89 [[nodiscard]] EVENGINE_API Result<RuntimeSlotCoordinates> emplace(void* object);
90
92 [[nodiscard]] EVENGINE_API void* resolve(std::uint32_t index, std::uint32_t generation,
93 std::uint64_t ownerEpoch) const noexcept;
94
96 [[nodiscard]] EVENGINE_API Result<void> erase(std::uint32_t index, std::uint32_t generation,
97 std::uint64_t ownerEpoch);
98
100 [[nodiscard]] EVENGINE_API bool isStale(std::uint32_t index, std::uint32_t generation,
101 std::uint64_t ownerEpoch) const noexcept;
102
112 [[nodiscard]] EVENGINE_API Result<void*> pin(std::uint32_t index, std::uint32_t generation,
113 std::uint64_t ownerEpoch);
114
123 EVENGINE_API void unpin(std::uint32_t index, std::uint64_t ownerEpoch) noexcept;
124
126 EVENGINE_API void clear();
127
138 template <class F>
140 void forEachLive(F&& visit) {
141 for (std::uint32_t index = 0; index < slots_.size(); ++index) {
142 void* object = slots_[index].object;
143 if (object == nullptr) continue;
145 visit(index, object);
146 }
147 }
148
150 template <class F>
152 void forEachLive(F&& visit) const {
153 for (std::uint32_t index = 0; index < slots_.size(); ++index) {
154 const void* object = slots_[index].object;
155 if (object == nullptr) continue;
157 visit(index, object);
158 }
159 }
160
162 [[nodiscard]] std::uint64_t ownerEpoch() const noexcept { return ownerEpoch_; }
163
164private:
165 struct Slot {
166 std::uint32_t generation = 1;
167 bool retired = false;
169 bool orphaned = false;
171 std::uint32_t pins = 0;
172 void* object = nullptr;
173 };
174
175 [[nodiscard]] std::optional<std::uint32_t> findSlot(std::uint32_t index, std::uint32_t generation,
176 std::uint64_t ownerEpoch) const noexcept;
177 [[nodiscard]] static bool coordinatesValid(std::uint32_t index, std::uint32_t generation) noexcept;
179 [[nodiscard]] static std::optional<std::uint32_t> nextGeneration(std::uint32_t current) noexcept;
180 [[nodiscard]] EVENGINE_API static std::uint64_t nextEpoch() noexcept;
181 void destroySlot(Slot& slot) noexcept;
182
183 OwnedInstanceDestroy destroy_;
184 std::uint64_t ownerEpoch_;
185 std::vector<Slot> slots_;
186 std::vector<std::uint32_t> freeSlots_;
187
188 inline static std::atomic<std::uint64_t> nextEpoch_{1};
189};
190
191} // namespace detail
192
193template <class T, class Tag>
194class RuntimeObjectRegistry;
195
207template <class T, class Tag>
210public:
212
214 RuntimePin() = default;
215 RuntimePin(const RuntimePin&) = delete;
216 RuntimePin& operator=(const RuntimePin&) = delete;
217
219 RuntimePin(RuntimePin&& other) noexcept
221 : store_(other.store_), reference_(other.reference_), object_(other.object_) {
222 other.store_ = nullptr;
223 other.object_ = nullptr;
224 }
225
227 RuntimePin& operator=(RuntimePin&& other) noexcept {
228 if (this != &other) {
230 release();
231 store_ = other.store_;
232 reference_ = other.reference_;
233 object_ = other.object_;
234 other.store_ = nullptr;
235 other.object_ = nullptr;
236 }
237 return *this;
238 }
239
241 ~RuntimePin() { release(); }
242
244 [[nodiscard]] bool isBound() const noexcept { return store_ != nullptr && object_ != nullptr; }
245
247 [[nodiscard]] const Ref& reference() const noexcept { return reference_; }
248
250 [[nodiscard]] T* get() const noexcept { return static_cast<T*>(object_); }
251
253 [[nodiscard]] Borrowed<T> borrow() const noexcept { return Borrowed<T>(get(), reference_.ownerEpoch); }
254
256 [[nodiscard]] T* operator->() const noexcept { return get(); }
257
259 [[nodiscard]] T& operator*() const noexcept { return *get(); }
260
261private:
262 friend class RuntimeObjectRegistry<T, Tag>;
263
264 RuntimePin(detail::RuntimeSlotStore* store, Ref reference, void* object) noexcept
265 : store_(store), reference_(reference), object_(object) {}
266
267 void release() noexcept {
268 if (store_ != nullptr) store_->unpin(reference_.handle.index(), reference_.ownerEpoch);
269 store_ = nullptr;
270 object_ = nullptr;
271 }
272
273 detail::RuntimeSlotStore* store_ = nullptr;
274 Ref reference_{};
275 void* object_ = nullptr;
276};
277
290template <class T, class Tag>
293public:
296
298 RuntimeObjectRegistry() : store_(std::make_unique<detail::RuntimeSlotStore>(&detail::ownedInstanceDestroy<T>)) {}
301 // The store lives behind a stable heap address, so moving the registry relocates only
302 // that pointer: pins issued before the move keep referencing the same store and stay
303 // valid, which is why the registry may be movable while RuntimeSlotStore is not.
307 RuntimeObjectRegistry& operator=(RuntimeObjectRegistry&&) noexcept = default;
308
314 [[nodiscard]] eve::Result<Ref> emplace(Owned<T> object) {
315 if (!object) {
318 "runtime registry cannot own a null object", {}, {},
319 "runtime.registry"));
320 }
321 auto slot = store_->emplace(static_cast<void*>(object.get()));
322 if (!slot.ok()) return eve::Result<Ref>::failure(slot.status());
323 const detail::RuntimeSlotCoordinates coordinates = slot.value();
324 object.release();
326 return eve::Result<Ref>::success(Ref{Handle(coordinates.index, coordinates.generation), store_->ownerEpoch()});
327 }
328
340 [[nodiscard]] Borrowed<T> resolve(Ref ref) noexcept {
341 return Borrowed<T>(
342 static_cast<T*>(store_->resolve(ref.handle.index(), ref.handle.generation(), ref.ownerEpoch)),
343 store_->ownerEpoch());
344 }
345
347 [[nodiscard]] Borrowed<const T> resolve(Ref ref) const noexcept {
348 return Borrowed<const T>(
349 static_cast<const T*>(store_->resolve(ref.handle.index(), ref.handle.generation(), ref.ownerEpoch)),
350 store_->ownerEpoch());
351 }
352
359 [[nodiscard]] eve::Result<void> erase(Ref ref) {
360 return store_->erase(ref.handle.index(), ref.handle.generation(), ref.ownerEpoch);
361 }
362
373 auto object = store_->pin(ref.handle.index(), ref.handle.generation(), ref.ownerEpoch);
374 if (!object.ok()) return eve::Result<RuntimePin<T, Tag>>::failure(object.status());
375 return eve::Result<RuntimePin<T, Tag>>::success(RuntimePin<T, Tag>(store_.get(), ref, object.value()));
376 }
377
379 [[nodiscard]] bool isStale(Ref ref) const noexcept {
380 return store_->isStale(ref.handle.index(), ref.handle.generation(), ref.ownerEpoch);
381 }
382
396 template <class F>
398 void forEachLive(F&& visit) {
399 store_->forEachLive(
400 [&](std::uint32_t index, void* object) { visit(index, static_cast<T&>(*static_cast<T*>(object))); });
401 }
402
404 template <class F>
406 void forEachLive(F&& visit) const {
407 store_->forEachLive([&](std::uint32_t index, const void* object) {
409 visit(index, static_cast<const T&>(*static_cast<const T*>(object)));
410 });
411 }
412
414 void clear() { store_->clear(); }
415
417 [[nodiscard]] std::uint64_t ownerEpoch() const noexcept { return store_->ownerEpoch(); }
418
419private:
421 std::unique_ptr<detail::RuntimeSlotStore> store_;
422};
423
424} // namespace eve::script
std::vector< std::unique_ptr< ScriptClimbingEventSink > > retired
Generation generation
Definition ECS.cpp:40
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:31
wgpu::PopErrorScopeStatus status
void(* destroy)(void *)
std::string pointer
int detail
std::string slot
Move-only, checked operation results for the common layer.
Handle-ref and borrowed observation types without the slot-map registry.
double current
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Process-local slot identity tagged by the registry that owns it.
Non-owning observation of a registry-owned object.
Slot/generation registry whose objects are exclusively unique-owned.
eve::Result< void > erase(Ref ref)
Destroys the object identified by a live handle.
bool isStale(Ref ref) const noexcept
Reports whether a non-invalid handle can no longer resolve.
std::uint64_t ownerEpoch() const noexcept
Returns the non-reusable lifetime epoch of this registry.
void clear()
Invalidates all slots and releases every unique-owned object.
void forEachLive(F &&visit)
Visit every live object with its slot index, in slot order.
Borrowed< T > resolve(Ref ref) noexcept
Resolves a live object as a non-owning observation.
Borrowed< const T > resolve(Ref ref) const noexcept
Const overload of resolve(); see the non-const form for its contract.
eve::Result< RuntimePin< T, Tag > > pin(Ref ref)
Keeps one entry alive until the returned pin is released.
RuntimeObjectRegistry(const RuntimeObjectRegistry &)=delete
eve::Result< Ref > emplace(Owned< T > object)
Transfers one unique-owned object into a fresh registry slot.
void forEachLive(F &&visit) const
Read-only overload: the callback receives const T&.
RuntimeObjectRegistry()
Creates an empty registry with a unique owner-lifetime epoch.
RuntimeObjectRegistry(RuntimeObjectRegistry &&) noexcept=default
Constructs a RuntimeObjectRegistry.
RuntimeObjectRegistry & operator=(const RuntimeObjectRegistry &)=delete
RAII keep-alive for one RuntimeObjectRegistry entry.
RuntimeHandleRef< Tag > Ref
Borrowed< T > borrow() const noexcept
Borrows the pinned object together with its owner epoch.
RuntimePin()=default
Constructs an unbound pin.
~RuntimePin()
Releases the keep-alive; an erased object is destroyed here.
RuntimePin & operator=(RuntimePin &&other) noexcept
Move-assigns, releasing any keep-alive this pin already held.
T * get() const noexcept
Borrows the pinned object; valid until the pin is released.
T * operator->() const noexcept
Pointer-like access to the pinned object.
RuntimePin(RuntimePin &&other) noexcept
Moves the keep-alive to a new pin; the source becomes unbound.
T & operator*() const noexcept
Dereferences the pinned object.
RuntimePin(const RuntimePin &)=delete
RuntimePin & operator=(const RuntimePin &)=delete
bool isBound() const noexcept
Whether this pin still keeps an object alive.
const Ref & reference() const noexcept
The reference this pin was created from.
Tag-free slot store behind every RuntimeObjectRegistry<T, Tag>.
RuntimeSlotStore & operator=(RuntimeSlotStore &&)=delete
Result< void > erase(std::uint32_t index, std::uint32_t generation, std::uint64_t ownerEpoch)
Destroys the object in a live slot, then advances or retires it.
void forEachLive(F &&visit)
Visits every live slot with its index, in slot order.
void * resolve(std::uint32_t index, std::uint32_t generation, std::uint64_t ownerEpoch) const noexcept
Borrows the live object for these coordinates, or null when stale.
RuntimeSlotStore(RuntimeSlotStore &&)=delete
bool isStale(std::uint32_t index, std::uint32_t generation, std::uint64_t ownerEpoch) const noexcept
Whether a non-invalid handle can no longer resolve.
RuntimeSlotStore & operator=(const RuntimeSlotStore &)=delete
Result< void * > pin(std::uint32_t index, std::uint32_t generation, std::uint64_t ownerEpoch)
Keeps one live slot's object alive across erase() and clear().
Result< RuntimeSlotCoordinates > emplace(void *object)
Stores one object in a fresh or recycled slot.
~RuntimeSlotStore()
Releases RuntimeSlotStore resources.
void forEachLive(F &&visit) const
Read-only overload: the callback receives const void*.
std::uint64_t ownerEpoch() const noexcept
Returns the non-reusable lifetime epoch of this store.
void unpin(std::uint32_t index, std::uint64_t ownerEpoch) noexcept
Releases one pin, destroying the object when it was the last one on an erased slot.
void clear()
Destroys every live object and invalidates every prior handle.
RuntimeSlotStore(OwnedInstanceDestroy destroy) noexcept
Constructs an empty store that owns no slot.
RuntimeSlotStore(const RuntimeSlotStore &)=delete
void(*)(void *) noexcept OwnedInstanceDestroy
Destroys one owned C++ wrapper without knowing its type.
void ownedInstanceDestroy(void *pointer) noexcept
Type-erased cleanup used when the VM never took the object.
std::unique_ptr< T > Owned
C++ owning storage used by an object factory.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
Handle plus owner-lifetime epoch for a non-ECS registry object.
RuntimeHandle< Tag > handle
Slot coordinates without the domain tag.