载入中...
搜索中...
未找到
DefinitionRuntime.h
浏览该文件的文档.
1#pragma once
2
14#include "common/Export.h"
15#include "common/Generation.h"
16#include "common/ResourceRef.h"
17#include "common/Result.h"
18
19#include <cstdint>
20#include <exception>
21#include <functional>
22#include <memory>
23#include <string>
24#include <type_traits>
25#include <utility>
26
27namespace eve::definition {
28
30enum class ReloadPolicy : std::uint8_t {
39};
40
51inline const char* reloadPolicyName(ReloadPolicy policy) noexcept {
52 switch (policy) {
53 case ReloadPolicy::KeepInstanceValues: return "keep_instance_values";
54 case ReloadPolicy::ReapplyDefaults: return "reapply_defaults";
55 case ReloadPolicy::RebuildInstance: return "rebuild_instance";
56 case ReloadPolicy::RejectWhileActive: return "reject_while_active";
57 }
58 return "unknown";
59}
60
72
74 [[nodiscard]] bool isValid() const noexcept { return reference.id().isValid() && !generation.isZero(); }
75
76 friend bool operator==(const DefinitionHandle&, const DefinitionHandle&) noexcept = default;
77};
78
91
96 if (instanceId.isNil())
99 eve::DiagnosticCode::InvalidArgument, "runtime instance identity must not be nil", "instanceId", {},
100 "common.definitions"));
101 if (!definition.id().isValid())
105 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "runtime definition reference is invalid",
106 "definition", {}, "common.definitions"));
110 eve::DiagnosticCode::InvalidArgument, "runtime definition generation must be positive",
111 "definitionGeneration", {}, "common.definitions"));
115 }
116
118 [[nodiscard]] DefinitionHandle definitionHandle() const noexcept {
120 }
121
123 [[nodiscard]] bool isValid() const noexcept {
125 }
126
128 friend bool operator==(const InstanceIdentity&, const InstanceIdentity&) noexcept = default;
129};
130
132enum class ReloadDisposition : std::uint8_t {
133 Unchanged,
134 Kept,
136 Rebuilt,
137};
138
151
167public:
169 using CloneFunction = void* (*)(const void*);
171 using AssignFunction = void (*)(void* destination, const void* source);
173 using DestroyFunction = void (*)(void*) noexcept;
180 std::function<eve::Result<void*>(const void*, const InstanceIdentity&, const DefinitionHandle&)>;
181
189 DestroyFunction destroy, bool active) noexcept;
192
196 RuntimeInstanceCore& operator=(const RuntimeInstanceCore& other);
200 RuntimeInstanceCore& operator=(RuntimeInstanceCore&& other) noexcept;
201
203 [[nodiscard]] const InstanceIdentity& identity() const noexcept { return identity_; }
205 [[nodiscard]] void* state() noexcept { return state_; }
207 [[nodiscard]] const void* state() const noexcept { return state_; }
209 [[nodiscard]] bool isActive() const noexcept { return active_; }
211 void setActive(bool active) noexcept { active_ = active; }
212
217 [[nodiscard]] eve::Result<void> checkDefinition(const DefinitionHandle& handle) const;
218
229 [[nodiscard]] eve::Result<ReloadOutcome> reload(const DefinitionHandle& next, ReloadPolicy policy,
230 const void* defaults, const RebuildFunction& rebuild);
231
239 [[nodiscard]] eve::Result<void> restoreExact(const InstanceIdentity& identity, void* state, bool active);
240
241private:
242 InstanceIdentity identity_;
243 void* state_ = nullptr;
244 CloneFunction clone_ = nullptr;
245 AssignFunction assign_ = nullptr;
246 DestroyFunction destroy_ = nullptr;
247 bool active_ = true;
248};
249
251template <class State>
253[[nodiscard]] void* cloneRuntimeState(const void* state) {
255 return new State(*static_cast<const State*>(state));
256}
257
259template <class State>
261void assignRuntimeState(void* destination, const void* source) {
262 *static_cast<State*>(destination) = *static_cast<const State*>(source);
263}
264
266template <class State>
268void destroyRuntimeState(void* state) noexcept {
269 delete static_cast<State*>(state);
270}
271
290template <class State>
293 static_assert(std::is_copy_constructible_v<State>, "RuntimeInstance state must be copy constructible");
294
295public:
297 std::function<eve::Result<State>(const State&, const InstanceIdentity&, const DefinitionHandle&)>;
298
299private:
302 : core_(std::move(identity), new State(std::move(state)), &cloneRuntimeState<State>, &assignRuntimeState<State>,
303 &destroyRuntimeState<State>, active) {}
304
305public:
311 RuntimeInstance(RuntimeInstance&&) noexcept = default;
313 RuntimeInstance& operator=(RuntimeInstance&&) noexcept = default;
315 ~RuntimeInstance() = default;
316
321 [[nodiscard]] static eve::Result<RuntimeInstance> create(eve::PersistentId instanceId,
322 eve::DefinitionRef definition,
323 eve::Generation definitionGeneration, State state,
324 bool active = true) {
325 auto identity = InstanceIdentity::create(instanceId, std::move(definition), definitionGeneration);
330 RuntimeInstance(std::move(identity).takeValue(), std::move(state), active));
331 }
332
334 [[nodiscard]] const InstanceIdentity& identity() const noexcept { return core_.identity(); }
336 [[nodiscard]] const State& state() const noexcept { return *static_cast<const State*>(core_.state()); }
338 [[nodiscard]] State& state() noexcept { return *static_cast<State*>(core_.state()); }
340 [[nodiscard]] bool isActive() const noexcept { return core_.isActive(); }
342 void setActive(bool active) noexcept { core_.setActive(active); }
343
349 return core_.checkDefinition(handle);
350 }
351
363 const State& defaults, RebuildFunction rebuild = {}) {
365 if (rebuild) {
366 erased = [callback = std::move(rebuild)](const void* state, const InstanceIdentity& identity,
368 auto rebuilt = callback(*static_cast<const State*>(state), identity, handle);
369 if (!rebuilt) return eve::Result<void*>::failure(rebuilt.status());
371 return eve::Result<void*>::success(new State(std::move(rebuilt).takeValue()));
372 };
373 }
374 return core_.reload(next, policy, &defaults, erased);
375 }
376
391 return core_.restoreExact(identity, new State(std::move(state)), active);
392 }
393
394private:
396};
397
398} // namespace eve::definition
bool & active
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:31
Runtime replacement generation for stale-handle rejection.
void(* destroy)(void *)
PrimitiveHandle handle
Strong asset, definition, object and generic resource references.
Move-only, checked operation results for the common layer.
const UnitySourceAsset & source
ViewPreparation callback
Stable definition reference backed by LogicalId.
Definition ResourceRef.h:85
const LogicalId & id() const noexcept
Return the stable logical definition identity.
Definition ResourceRef.h:97
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
bool isValid() const noexcept
Returns whether this value contains a valid namespace and name.
Definition Identity.h:433
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Type-erased bookkeeping shared by every RuntimeInstance<State>.
std::function< eve::Result< void * >(const void *, const InstanceIdentity &, const DefinitionHandle &)> RebuildFunction
Type-erased rebuild policy callback.
void(*)(void *destination, const void *source) AssignFunction
Assigns one payload from another payload of the same state type.
eve::Result< ReloadOutcome > reload(const DefinitionHandle &next, ReloadPolicy policy, const void *defaults, const RebuildFunction &rebuild)
Apply a definition replacement with an atomic payload policy.
void setActive(bool active) noexcept
Set active state; this does not reload or rebuild the instance.
eve::Result< void > checkDefinition(const DefinitionHandle &handle) const
Verify that a caller's definition handle is the exact current incarnation.
void * state() noexcept
Borrow the payload; null only after a move.
eve::Result< void > restoreExact(const InstanceIdentity &identity, void *state, bool active)
Swap in an already validated payload of this exact instance.
const void * state() const noexcept
Borrow the payload; null only after a move.
void(*)(void *) noexcept DestroyFunction
Destroys one payload; must not throw.
const InstanceIdentity & identity() const noexcept
Borrow the complete immutable identity.
bool isActive() const noexcept
Whether the instance participates in active simulation.
void *(*)(const void *) CloneFunction
Deep-copies one payload for the copy constructor and copy assignment.
Owns one strongly typed runtime state and its definition identity.
eve::Result< ReloadOutcome > reload(const DefinitionHandle &next, ReloadPolicy policy, const State &defaults, RebuildFunction rebuild={})
Apply a definition replacement with an atomic typed-state policy.
bool isActive() const noexcept
Whether the instance participates in active simulation.
std::function< eve::Result< State >(const State &, const InstanceIdentity &, const DefinitionHandle &)> RebuildFunction
const State & state() const noexcept
Borrow the current typed state.
State & state() noexcept
Mutate typed state on the owning domain thread.
eve::Result< void > restoreExact(const InstanceIdentity &identity, State state, bool active)
Restore an already validated snapshot of this exact instance.
const InstanceIdentity & identity() const noexcept
Borrow the complete immutable identity.
static eve::Result< RuntimeInstance > create(eve::PersistentId instanceId, eve::DefinitionRef definition, eve::Generation definitionGeneration, State state, bool active=true)
Construct a runtime instance after validating its identity.
void setActive(bool active) noexcept
Set active state; this does not reload or rebuild the instance.
eve::Result< void > checkDefinition(const DefinitionHandle &handle) const
Verify that a caller's definition handle is the exact current incarnation.
RuntimeInstance(RuntimeInstance &&) noexcept=default
Move a typed runtime instance.
RuntimeInstance(const RuntimeInstance &)=default
Copy a typed runtime instance.
RuntimeInstance & operator=(const RuntimeInstance &)=default
Copy-assign a typed runtime instance.
Id128 public API.
Definition Identity.h:112
constexpr bool isNil() const noexcept
Returns whether this value is the all-zero nil ID.
Definition Identity.h:176
constexpr bool isZero() const noexcept
Returns whether this value is zero.
void destroyRuntimeState(void *state) noexcept
Payload destruction for one state type.
ReloadPolicy
Policy used when a live definition is replaced.
@ RejectWhileActive
Reject the reload while the instance is active; inactive instances keep values.
@ ReapplyDefaults
Replace the typed state with defaults produced from the new definition.
@ RebuildInstance
Let the consumer atomically construct a new state from the new definition.
@ KeepInstanceValues
Replace the definition generation and preserve all instance values.
void * cloneRuntimeState(const void *state)
Payload operations for one state type; used by RuntimeInstance<State>.
ReloadDisposition
Observable result of a successful or no-op definition reload.
void assignRuntimeState(void *destination, const void *source)
Payload assignment for one state type.
const char * reloadPolicyName(ReloadPolicy policy) noexcept
Stable spelling for a runtime definition reload policy.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
A generation-qualified reference to one definition incarnation.
bool isValid() const noexcept
Whether both the logical reference and generation are valid.
friend bool operator==(const DefinitionHandle &, const DefinitionHandle &) noexcept=default
Canonical identity carried by every typed runtime instance.
bool isValid() const noexcept
Whether this identity has all required non-zero fields.
DefinitionHandle definitionHandle() const noexcept
Convert the logical reference and generation into a checked handle.
friend bool operator==(const InstanceIdentity &, const InstanceIdentity &) noexcept=default
Operator ==.
static eve::Result< InstanceIdentity > create(eve::PersistentId instanceId, eve::DefinitionRef definition, eve::Generation definitionGeneration)
Construct and validate one complete instance identity.
Identity transition emitted by a typed runtime reload operation.
bool changed() const noexcept
Whether the reload changed the definition generation.