载入中...
搜索中...
未找到
SquirrelOwnership.h
浏览该文件的文档.
1#pragma once
2
8#include "common/Export.h"
9#include "common/Result.h"
12#include "common/Value.h"
13
14#include <squirrel.h>
15#include <simplesquirrel/simplesquirrel.hpp>
16
17#include <atomic>
18#include <cstdint>
19#include <exception>
20#include <functional>
21#include <limits>
22#include <memory>
23#include <optional>
24#include <string>
25#include <type_traits>
26#include <typeinfo>
27#include <utility>
28#include <vector>
29
30namespace eve::script {
31
41
52[[nodiscard]] inline const char* objectSemanticName(ObjectSemantic semantic) noexcept {
53 switch (semantic) {
54 case ObjectSemantic::Value: return "value";
55 case ObjectSemantic::Owned: return "owned";
56 case ObjectSemantic::Borrowed: return "borrowed";
57 }
58 return "unknown";
59}
60
61
64
72public:
74 constexpr BorrowedSquirrelObject() noexcept = default;
76 explicit constexpr BorrowedSquirrelObject(const ssq::Object* object) noexcept : object_(object) {}
77
89 [[nodiscard]] constexpr const ssq::Object* get() const noexcept { return object_; }
91 [[nodiscard]] bool isBound() const noexcept { return object_ != nullptr && !object_->isEmpty(); }
92
93private:
94 const ssq::Object* object_ = nullptr;
95};
96
98[[nodiscard]] inline BorrowedSquirrelObject borrowSquirrelObject(const ssq::Object& object) noexcept {
99 return BorrowedSquirrelObject(&object);
100}
101
109public:
110 static constexpr const char* capabilityName = "eve.script.ISquirrelRootReleaser";
112 virtual ~ISquirrelRootReleaser() = default;
118 virtual void releaseSquirrelRoots() noexcept = 0;
119};
120
129public:
133 explicit OwnedSquirrelObject(ssq::Object object) noexcept : object_(std::move(object)) {}
134
140 OwnedSquirrelObject& operator=(OwnedSquirrelObject&&) noexcept = default;
143
145 [[nodiscard]] const ssq::Object& get() const noexcept { return object_; }
147 [[nodiscard]] bool isBound() const noexcept { return !object_.isEmpty(); }
149 [[nodiscard]] ssq::Object release() && noexcept { return std::move(object_); }
150
151private:
152 ssq::Object object_;
153};
154
161 if (!borrowed.isBound()) {
166 "cannot own an empty or unbound Squirrel object", {}, {}, "squirrel.ownership"));
167 }
170}
171
172namespace detail {
173
174
175template <class T>
177[[nodiscard]] inline std::size_t squirrelTypeHash() {
178 return ssq::detail::stableTypeHash<T>();
179}
180
181template <class T>
183SQInteger ownedInstanceReleaseHook(SQUserPointer pointer, SQInteger) noexcept {
184 delete static_cast<T*>(pointer);
185 return 0;
186}
187
188} // namespace detail
189
190
191namespace detail {
192
193// squirrelTypeHash, ownedInstanceReleaseHook and ownedInstanceDestroy are
194// declared earlier in this header, before RuntimeObjectRegistry, because the
195// registry's constructor needs ownedInstanceDestroy<T>.
196
198using SquirrelReleaseHook = SQInteger (*)(SQUserPointer, SQInteger);
199
202
222 SquirrelReleaseHook releaseHook,
223 OwnedInstanceDestroy destroy,
224 std::size_t typeHash);
225
226} // namespace detail
227
238template <class T>
241 if (!vm || !object) {
243 return eve::Result<ssq::Object>::failure(detail::ownedInstanceArgumentDiagnostic());
244 }
246 return detail::makeOwnedSquirrelInstanceRaw(vm, static_cast<void*>(object.release()),
247 &detail::ownedInstanceReleaseHook<T>, &detail::ownedInstanceDestroy<T>,
248 detail::squirrelTypeHash<T*>());
249}
250
251} // namespace eve::script
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:31
void(* destroy)(void *)
const RuntimeTensor * borrowed
std::string pointer
int detail
Move-only, checked operation results for the common layer.
uint32_t semantic
Strong generation-qualified handles for non-ECS runtime registries.
Tag-free slot registry: handles, borrows, unique ownership and keep-alive pins.
Owning, renderer-independent dynamic values.
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Borrowed view of a rooted Squirrel object for one synchronous call.
constexpr const ssq::Object * get() const noexcept
Returns the borrowed object, or nullptr when unbound.
bool isBound() const noexcept
Returns whether the view is bound to a non-empty object.
constexpr BorrowedSquirrelObject() noexcept=default
Borrowed squirrel object.
Non-owning observation of a registry-owned object.
Releases C++-owned Squirrel roots before Runtime destroys its VM.
virtual void releaseSquirrelRoots() noexcept=0
Drops every C++-owned Squirrel root this listener still holds.
virtual ~ISquirrelRootReleaser()=default
I squirrel root releaser.
A rooted Squirrel reference owned by the C++ holder.
ssq::Object release() &&noexcept
Transfers the rooted reference to the caller.
bool isBound() const noexcept
Returns whether this owner contains a rooted non-empty object.
OwnedSquirrelObject(const OwnedSquirrelObject &)=delete
OwnedSquirrelObject & operator=(const OwnedSquirrelObject &)=delete
OwnedSquirrelObject()=default
Owned squirrel object.
OwnedSquirrelObject(ssq::Object object) noexcept
Owned squirrel object.
OwnedSquirrelObject(OwnedSquirrelObject &&) noexcept=default
Owned squirrel object.
eve::Diagnostic Diagnostic
Result< ssq::Object > makeOwnedSquirrelInstanceRaw(HSQUIRRELVM vm, void *object, SquirrelReleaseHook releaseHook, OwnedInstanceDestroy destroy, std::size_t typeHash)
Non-template core of makeOwnedSquirrelInstance.
Diagnostic ownedInstanceArgumentDiagnostic()
Diagnostic for a null VM or null object, built once instead of per T.
eve::Result< OwnedSquirrelObject > ownSquirrelObject(BorrowedSquirrelObject borrowed)
Promotes a borrowed VM object to an explicit rooted owner.
ObjectSemantic
The only three meanings a script-facing object may have.
BorrowedSquirrelObject borrowSquirrelObject(const ssq::Object &object) noexcept
Creates a one-call Borrowed view without retaining a VM reference.
std::unique_ptr< T > Owned
C++ owning storage used by an object factory.
const char * objectSemanticName(ObjectSemantic semantic) noexcept
Returns the stable name used by diagnostics and documentation.
eve::Result< ssq::Object > makeOwnedSquirrelInstance(HSQUIRRELVM vm, Owned< T > object)
Creates a Squirrel instance that owns a C++ wrapper through a release hook.
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:21