载入中...
搜索中...
未找到
Module.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3#include "common/Result.h"
4
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9namespace ssq {
10class VM;
11class Table;
12class Class;
13} // namespace ssq
14
15#define SSQ_REG \
16 static void expose(ssq::Table& table); \
17 static void expose(ssq::Class& vm);
18
19#define Module_REG(ModuleName) \
20 SSQ_REG \
21 virtual std::string getName() const override { return name; } \
22
\
28 [[nodiscard("module instance ownership must be retained or explicitly handled")]] static ModuleName* create(); \
29 static const char* name
30
31#define Module_IMPL(ModuleName, newExpr) \
32 ModuleRegister ModuleName##_register(ModuleName::name, \
33 (ModuleManager::creator_t)(ModuleName::create), ModuleName::expose); \
34 ModuleName* ModuleName::create() { \
35 auto* p = ModuleManager::find(name); \
36 if (p) return static_cast<ModuleName*>(p); \
37 ModuleName* n = newExpr; \
38 ModuleManager::insert(name, n); \
39 return n; \
40 } \
41 const char* ModuleName::name = #ModuleName
42
43#define getModInst(N,T) eve::ModuleManager::getInstance<N::T>(#T)
44#define requireModInst(N,T) eve::ModuleManager::requireInstance<N::T>(#T)
45
46namespace eve {
47
48class Runtime;
52public:
54 virtual ~Module() {}
56 virtual std::string getName() const = 0;
57};
61public:
62 typedef Module* (*creator_t)();
63 typedef void (*exposer_t)(ssq::Table&);
64
66 static ModuleManager& inst();
67
77 [[nodiscard("module lookup result must be checked before use")]] static Module* find(const char* name);
79 static void insert(const char* name, Module* inst);
80
82 static void register_module(const char* name, creator_t c, exposer_t e);
84 [[nodiscard("plugin registration outcome must be checked")]] static eve::Result<void> beginPluginRegistration();
90 [[nodiscard("plugin registration outcome must be checked")]] static eve::Result<void> finishPluginRegistration(
91 bool commit);
101 static void expose(Runtime& runtime);
102 // Compatibility for embedders that still own their ssq::VM directly.
104 static void expose(ssq::VM& vm);
105 // Expose modules registered after the initial expose() (e.g. plugins).
107 static int expose_pending();
116 static Runtime* runtime();
125 static ssq::VM* vm();
127 static void detach(Runtime* runtime);
128
137 template <typename T>
138 [[nodiscard("module lookup result must be checked before use")]] static T* getInstance(const char* name) {
139 return static_cast<T*>(getInstanceRaw(name));
140 }
141
150 template <typename T>
151 [[nodiscard("module instance ownership must be retained or explicitly handled")]] static T* requireInstance(
152 const char* name) {
153 return static_cast<T*>(requireInstanceRaw(name));
154 }
155
162 static void requireAll();
163
164protected:
166 static void exposeVM(ssq::VM& vm);
167
168private:
169 static int ensureScriptEcs();
170 static int exposeAllForCompatibility();
171
172 // Keep unordered_map lookup and lazy-creation code out of the two public
173 // templates. Every module instantiates only the pointer cast; the container
174 // implementation is emitted once in Module.cpp.
180 static Module* getInstanceRaw(const char* name);
186 static Module* requireInstanceRaw(const char* name);
188protected:
190 struct ModuleInfo {
191 creator_t creator = nullptr;
192 exposer_t exposer = nullptr;
193 Module* instance = nullptr;
194 bool exposed = false;
195 };
197 std::unordered_map<std::string, ModuleInfo> registered_modules;
198 bool plugin_registration_active_ = false;
199 std::vector<std::string> plugin_registration_added_;
200 std::string plugin_registration_error_;
201 Runtime* active_runtime_ = nullptr;
203 friend struct ModuleBindAccess;
204};
205
209 ModuleRegister(const char* name, ModuleManager::creator_t c, ModuleManager::exposer_t e);
210};
211
212
213} // namespace eve
HSQUIRRELVM vm
Definition ECS.cpp:20
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::int32_t c
std::string name
Move-only, checked operation results for the common layer.
EVENGINE_API_FOUNDATION public API.
Definition Module.h:55
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
Move-only operation result carrying either a value or Status.
Definition Result.h:155
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:14
ModuleInfo public API.
Definition Module.h:185
EVENGINE_API_FOUNDATION public API.
Definition Module.h:202