载入中...
搜索中...
未找到
SquirrelBindContext.h
浏览该文件的文档.
1#pragma once
2
12#include "common/Export.h"
13#include "common/Result.h"
15
16#include <simplesquirrel/simplesquirrel.hpp>
17
18#include <string>
19#include <type_traits>
20#include <utility>
21
22namespace eve::script {
23
33public:
40 constexpr BindContext(HSQUIRRELVM vm, const char* source) noexcept : vm_(vm), source_(source) {}
41
49 [[nodiscard]] constexpr HSQUIRRELVM vm() const noexcept { return vm_; }
50
58 [[nodiscard]] constexpr const char* source() const noexcept { return source_; }
59
67 [[nodiscard]] ssq::Table fail(DiagnosticCode code, std::string message, std::string path = {}) const {
68 return projectStatusResult(vm_, Status::failure(Diagnostic::error(code, std::move(message), std::move(path), {},
69 source_ ? source_ : "")));
70 }
71
78 [[nodiscard]] ssq::Table failInvalid(std::string message, std::string path = {}) const {
79 return fail(DiagnosticCode::InvalidArgument, std::move(message), std::move(path));
80 }
81
90 template <class Fn>
92 [[nodiscard]] ssq::Table checked(bool ready, std::string nullMessage, Fn&& fn, std::string path = {}) const {
93 if (!ready) return failInvalid(std::move(nullMessage), std::move(path));
95 return projectResult(vm_, std::forward<Fn>(fn)());
96 }
97
107 template <class T, class Fn>
109 [[nodiscard]] ssq::Table checked(T* self, std::string nullMessage, Fn&& fn, std::string path = {}) const {
111 return checked(self != nullptr, std::move(nullMessage), std::forward<Fn>(fn), std::move(path));
112 }
113
114private:
115 HSQUIRRELVM vm_ = nullptr;
116 const char* source_ = nullptr;
117};
118
128template <class F>
130inline void bindMethod(ssq::Class& cls, const char* name, F&& fn) {
131 cls.addFunc(name, std::forward<F>(fn));
132}
133
143template <class ScriptT, class Getter>
145inline void bindNullSafe(ssq::Class& cls, const char* name, Getter getter,
146 std::invoke_result_t<Getter, ScriptT&> whenNull) {
147 cls.addFunc(name, [getter = std::move(getter), whenNull = std::move(whenNull)](ScriptT* self) {
148 return self ? getter(*self) : whenNull;
149 });
150}
151
152} // namespace eve::script
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
HSQOBJECT cls
Definition ECS.cpp:21
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
std::string message
DiagnosticCode code
std::string name
std::string path
Definition PlayHost.cpp:110
Move-only, checked operation results for the common layer.
The single Squirrel projection for common Result, Status and Value.
const UnitySourceAsset & source
Pins the active VM and a stable diagnostic source for one expose() TU.
ssq::Table failInvalid(std::string message, std::string path={}) const
Convenience InvalidArgument failure with this binder's source tag.
constexpr BindContext(HSQUIRRELVM vm, const char *source) noexcept
Construct a binder for one expose() translation unit.
ssq::Table fail(DiagnosticCode code, std::string message, std::string path={}) const
Project a binding-layer argument failure.
constexpr const char * source() const noexcept
Diagnostic source tag for this binder.
constexpr HSQUIRRELVM vm() const noexcept
Active Squirrel VM handle.
ssq::Table checked(T *self, std::string nullMessage, Fn &&fn, std::string path={}) const
Null-self guard then project a void Result-producing call.
ssq::Table checked(bool ready, std::string nullMessage, Fn &&fn, std::string path={}) const
Run fn and project its void Result when ready; otherwise fail.
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
void bindNullSafe(ssq::Class &cls, const char *name, Getter getter, std::invoke_result_t< Getter, ScriptT & > whenNull)
Register a null-safe getter that returns whenNull when self is null.
void bindMethod(ssq::Class &cls, const char *name, F &&fn)
Register one script method while keeping a literal name for Binding Contracts.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
SettlementPipeline::Stage fn