载入中...
搜索中...
未找到
Module.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3#include "common/Result.h"
4
5#include <memory>
6#include <string>
7
8namespace ssq {
9class VM;
10class Table;
11class Class;
12} // namespace ssq
13
14#define SSQ_REG \
15 static void expose(ssq::Table& table); \
16 static void expose(ssq::Class& vm);
17
18#define Module_REG(ModuleName) \
19 SSQ_REG \
20 virtual std::string getName() const override { return name; } \
21
\
27 [[nodiscard("module instance ownership must be retained or explicitly handled")]] static ModuleName* create(); \
28 \
29 static auto nativeModuleFactory() { return &ModuleName::create; } \
30 static const char* name
31
32#define Module_IMPL(ModuleName, newExpr) \
33 ModuleRegister ModuleName##_register(ModuleName::name, (ModuleManager::creator_t)(ModuleName::create), \
34 ModuleName::expose); \
35 ModuleName* ModuleName::create() { \
36 auto factory = reinterpret_cast<ModuleManager::creator_t>(&ModuleName::create); \
37 auto* p = ModuleManager::find(name, factory); \
38 if (p) return static_cast<ModuleName*>(p); \
39 ModuleName* n = newExpr; \
40 ModuleManager::insert(name, n, factory); \
41 return n; \
42 } \
43 const char* ModuleName::name = #ModuleName
44
45#define getModInst(N,T) eve::ModuleManager::getInstance<N::T>(#T)
46#define requireModInst(N,T) eve::ModuleManager::requireInstance<N::T>(#T)
47
48namespace eve {
49
50class Runtime;
54public:
56 virtual ~Module() {}
58 virtual std::string getName() const = 0;
59};
63public:
64 typedef Module* (*creator_t)();
65 typedef void (*exposer_t)(ssq::Table&);
66
68 static ModuleManager& inst();
69
79 [[nodiscard("module lookup result must be checked before use")]] static Module* find(const char* name);
86 [[nodiscard]] static Module* find(const char* name, creator_t factory);
88 static void insert(const char* name, Module* inst);
95 static void insert(const char* name, Module* instance, creator_t factory);
96
98 static void register_module(const char* name, creator_t c, exposer_t e);
100 [[nodiscard("plugin registration outcome must be checked")]] static eve::Result<void> beginPluginRegistration();
107 [[nodiscard]] static eve::Result<void> beginPluginRegistration(std::string owner);
113 [[nodiscard("plugin registration outcome must be checked")]] static eve::Result<void> finishPluginRegistration(
114 bool commit);
124 static void expose(Runtime& runtime);
125 // Compatibility for embedders that still own their ssq::VM directly.
127 static void expose(ssq::VM& vm);
128 // Expose modules registered after the initial expose() (e.g. plugins).
130 static int expose_pending();
139 static Runtime* runtime();
148 static ssq::VM* vm();
150 static void detach(Runtime* runtime);
151
160 template <typename T>
161 [[nodiscard("module lookup result must be checked before use")]] static T* getInstance(const char* name) {
162 return static_cast<T*>(getInstanceRaw(name));
163 }
164
173 template <typename T>
174 [[nodiscard("module instance ownership must be retained or explicitly handled")]] static T* requireInstance(
175 const char* name) {
176 return static_cast<T*>(requireInstanceRaw(name));
177 }
178
185 static void requireAll();
186
187protected:
189 static void exposeVM(ssq::VM& vm);
190
191private:
192 static eve::Result<void> publishPluginBindings();
193 static int ensureScriptEcs();
194 static int exposeAllForCompatibility();
195
196 // Keep unordered_map lookup and lazy-creation code out of the two public
197 // templates. Every module instantiates only the pointer cast; the container
198 // implementation is emitted once in Module.cpp.
204 static Module* getInstanceRaw(const char* name);
210 static Module* requireInstanceRaw(const char* name);
211
212private:
213 // The singleton owns its registry; only its implementation sees container types.
214 struct State;
215 std::unique_ptr<State> state_;
216
219 ModuleManager(const ModuleManager&) = delete;
220 ModuleManager& operator=(const ModuleManager&) = delete;
221 ModuleManager(ModuleManager&&) = delete;
222 ModuleManager& operator=(ModuleManager&&) = delete;
224 friend struct ModuleBindAccess;
225};
226
230 ModuleRegister(const char* name, ModuleManager::creator_t c, ModuleManager::exposer_t e);
231};
232
233
234} // namespace eve
HSQUIRRELVM vm
Definition ECS.cpp:38
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::int32_t c
std::shared_ptr< OceanGpuSpectrum > owner
Move-only, checked operation results for the common layer.
EVENGINE_API_FOUNDATION public API.
Definition Module.h:57
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
EVENGINE_API_FOUNDATION public API.
Definition Runtime.h:189
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
enum EVENGINE_API_FOUNDATION Table
OT_TABLE (editable via Runtime table APIs / ReflectedPropertyModel).
Definition Runtime.h:121
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:21
EVENGINE_API_FOUNDATION public API.
Definition Module.h:223