ShitEngine
1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
Macros.h
浏览该文件的文档.
1
#pragma once
2
29
30
// 反射标记宏(annotate 方案)
31
//
32
// SHIT_REFLECT(Mode)
33
// 放在 class/struct 关键字之后、类名之前。展开为
34
// __attribute__((annotate("shit-reflection:Mode"))),
35
// ReflectionScanner 通过 AST 的 CXCursor_AnnotateAttr 读取。
36
// Mode: WhiteList | BlackList
37
// WhiteList — 仅反射 SHIT_META(Enable) 标记的字段(opt-in)
38
// BlackList — 反射全部字段,SHIT_META(Disable) 标记的除外(opt-out)
39
// (向后兼容:Fields ≈ BlackList,WhiteListFields ≈ WhiteList)
40
//
41
// 用法:
42
// class SHIT_API SHIT_REFLECT(BlackList) MyComponent : Base { ... };
43
// struct SHIT_REFLECT(WhiteList) MyStruct { ... };
44
//
45
// 展开示例(libClang 下):
46
// class __declspec(dllexport) __attribute__((annotate("shit-reflection:BlackList")))
47
// struct __attribute__((annotate("shit-reflection:WhiteList")))
48
//
49
// SHIT_REFLECT_BODY(Type)
50
// 放在类/结构体体内。展开为 friend 声明,授予生成的 Register_Type()
51
// 访问 private/protected 成员的权限(供成员指针取 offset,ABI 安全)。
52
// 注意:annotate 无法替代 friend 的运行期授权作用,必须保留。
53
//
54
// SHIT_ENUM(Type)
55
// 标记 enum / enum class。放在 enum/class 关键字之后、枚举名之前。
56
// 展开为 __attribute__((annotate("shit-enum:Type")))。
57
//
58
// 用法:
59
// enum class SHIT_ENUM(MyEnum) MyEnum { A, B, C };
60
//
61
// Scanner 遍历枚举项(CXCursor_EnumConstantDecl),提取名称与数值。
62
// 生成的注册代码调用 .Value("A", 0) 登记每个枚举常量。
63
//
64
// SHIT_META(...)
65
// 放在字段上方,为字段添加结构化元数据(供编辑器属性面板显示用)。
66
// 使用双括号语法:SHIT_META(({...}))
67
// 多条 SHIT_META 可叠加(每个生成独立的 annotate 属性)。
68
//
69
// 示例:
70
// SHIT_META(({.displayName = "HP", .range = {0, 100}}))
71
// SHIT_META(Enable) ← WhiteListFields 模式的简写标记
72
73
// libClang(ReflectionScanner 使用的解析器)定义 __clang__,GCC 不定义。
74
// 仅在 __clang__ 下启用 annotate,避免 GCC 编译时产生
75
// -Wattributes "annotate attribute directive ignored" 告警。
76
#if defined(__clang__)
77
#define SHIT_DETAIL_ANNOTATE(str) __attribute__((annotate(str)))
78
#else
79
// 非 clang 编译器(GCC/MSVC):annotate 退化为空。
80
// 不影响 ReflectionScanner——libClang 自带解析器,即使目标编译器是 GCC
81
// 也能识别 __attribute__ 语法(libClang 定义 __clang__)。
82
#define SHIT_DETAIL_ANNOTATE(str)
83
#endif
84
85
// ── 类/结构体级反射 ────────────────────────────────
86
#define SHIT_REFLECT(Mode) SHIT_DETAIL_ANNOTATE("shit-reflection:" #Mode)
87
88
// ── 类内 friend 授权(必须保留) ───────────────────
89
#define SHIT_REFLECT_BODY(Type) friend bool Register_##Type();
90
91
// ── 枚举反射 ──────────────────────────────────────
92
#define SHIT_ENUM(Type) SHIT_DETAIL_ANNOTATE("shit-enum:" #Type)
93
94
// ── 字段级元数据 ──────────────────────────────────
95
#define SHIT_META(...) SHIT_DETAIL_ANNOTATE("shit-meta:" #__VA_ARGS__)
96
97
// ── 方法反射 ──────────────────────────────────────
98
// 放在方法声明上方,标记该方法可被反射系统发现。
99
// Scanner 遍历 CXCursor_CXXMethod 时检查此注解。
100
//
101
// 用法:
102
// SHIT_METHOD
103
// float getSourceRectX() const;
104
//
105
#define SHIT_METHOD SHIT_DETAIL_ANNOTATE("shit-method")
106
107
// ── 属性反射(getter/setter 对)────────────────────
108
// 放在 getter 方法声明上方,标记一对 getter/setter 为一个属性。
109
// Scanner 检测到此注解后,会查找同名 setter 方法。
110
//
111
// 用法:
112
// SHIT_PROPERTY(getSourceRectX, setSourceRectX)
113
// float getSourceRectX() const;
114
// void setSourceRectX(float v);
115
//
116
// 属性在编辑器中渲染为可编辑字段,通过 getter/setter 读写。
117
// setter 为空字符串时表示只读属性。
118
//
119
#define SHIT_PROPERTY(Getter, Setter) SHIT_DETAIL_ANNOTATE("shit-property:" #Getter ":" #Setter)
include
ShitEngine
Reflection
Macros.h
制作者
1.17.0