载入中...
搜索中...
未找到
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 static const std::size_t value = std::hash<std::string>{}(typeid(T).name());
179 return value;
180}
181
182template <class T>
184SQInteger ownedInstanceReleaseHook(SQUserPointer pointer, SQInteger) noexcept {
185 delete static_cast<T*>(pointer);
186 return 0;
187}
188
189} // namespace detail
190
191
192namespace detail {
193
194// squirrelTypeHash, ownedInstanceReleaseHook and ownedInstanceDestroy are
195// declared earlier in this header, before RuntimeObjectRegistry, because the
196// registry's constructor needs ownedInstanceDestroy<T>.
197
199using SquirrelReleaseHook = SQInteger (*)(SQUserPointer, SQInteger);
200
203
223 SquirrelReleaseHook releaseHook,
224 OwnedInstanceDestroy destroy,
225 std::size_t typeHash);
226
227} // namespace detail
228
239template <class T>
242 if (!vm || !object) {
244 return eve::Result<ssq::Object>::failure(detail::ownedInstanceArgumentDiagnostic());
245 }
247 return detail::makeOwnedSquirrelInstanceRaw(vm, static_cast<void*>(object.release()),
248 &detail::ownedInstanceReleaseHook<T>, &detail::ownedInstanceDestroy<T>,
249 detail::squirrelTypeHash<T*>());
250}
251
252} // namespace eve::script
double value
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:31
void(* destroy)(void *)
std::string name
const RuntimeTensor * borrowed
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
int detail
Move-only, checked operation results for the common layer.
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.
uint32_t semantic
Definition WfcSimple.cpp:15
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
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
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 C++ observation with no implicit lifetime extension.
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:14