载入中...
搜索中...
未找到
Module.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4#include <cstdint>
5#include <string>
6#include <unordered_map>
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 { return name; } \
21 static ModuleName* create(); \
22 static const char* name
23
24#define Module_IMPL(ModuleName, newExpr) \
25 ModuleRegister ModuleName##_register(ModuleName::name, \
26 (ModuleManager::creator_t)(ModuleName::create), ModuleName::expose); \
27 ModuleName* ModuleName::create() { \
28 auto* p = ModuleManager::find(name); \
29 if (p) return static_cast<ModuleName*>(p); \
30 ModuleName* n = newExpr; \
31 ModuleManager::insert(name, n); \
32 return n; \
33 } \
34 const char* ModuleName::name = #ModuleName
35
36#define getModInst(N,T) eve::ModuleManager::getInstance<N::T>(#T)
37#define requireModInst(N,T) eve::ModuleManager::requireInstance<N::T>(#T)
38
39namespace eve {
40
41class Runtime;
42
44public:
45 virtual ~Module() {}
46 virtual std::string getName() const = 0;
47};
48
50public:
51 typedef Module* (*creator_t)();
52 typedef void (*exposer_t)(ssq::Table&);
53
55 static ModuleManager& inst();
56
58 static Module* find(const char* name);
60 static void insert(const char* name, Module* inst);
61
63 static void register_module(const char* name, creator_t c, exposer_t e);
65 static void expose(Runtime& runtime);
66 // Compatibility for embedders that still own their ssq::VM directly.
67 static void expose(ssq::VM& vm);
68 // Expose modules registered after the initial expose() (e.g. plugins).
69 static int expose_pending();
71 static Runtime* runtime();
73 static ssq::VM* vm();
75 static void detach(Runtime* runtime);
76
78 template <typename T>
79 static T* getInstance(const char* name) {
80 return static_cast<T*>(inst().registered_modules[name].instance);
81 }
82
84 template <typename T>
85 static T* requireInstance(const char* name) {
86 auto& D = inst().registered_modules[name];
87 if (D.instance) return static_cast<T*>(D.instance);
88 return static_cast<T*>(D.creator());
89 }
90
91protected:
92 static void exposeVM(ssq::VM& vm);
93
94 struct ModuleInfo {
95 creator_t creator = nullptr;
96 exposer_t exposer = nullptr;
97 Module* instance = nullptr;
98 bool exposed = false;
99 };
100
101 std::unordered_map<std::string, ModuleInfo> registered_modules;
102 Runtime* active_runtime_ = nullptr;
103};
104
106 ModuleRegister(const char* name, ModuleManager::creator_t c, ModuleManager::exposer_t e);
107};
108
109
110
111} // namespace eve
HSQUIRRELVM vm
Definition ECS.cpp:20
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:16
uint32_t c
const char * name
Definition RockMesh.cpp:21
std::unordered_map< std::string, ModuleInfo > registered_modules
Definition Module.h:101
static T * getInstance(const char *name)
Casts the registered instance of a module to T (may be null).
Definition Module.h:79
static T * requireInstance(const char *name)
Returns the live instance, creating it lazily through the registered factory.
Definition Module.h:85
virtual ~Module()
Definition Module.h:45
virtual std::string getName() const =0
Definition Build.cpp:11
@ Table
OT_TABLE (not yet editable).
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:12