ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
Macros.h
浏览该文件的文档.
1
#pragma once
2
3
// 反射标记宏(annotate 方案)
4
//
5
// SHIT_REFLECT(Mode)
6
// 放在 class/struct 关键字之后、类名之前。展开为
7
// __attribute__((annotate("shit-reflection:Mode"))),
8
// ReflectionScanner 通过 AST 的 CXCursor_AnnotateAttr 读取。
9
// Mode: WhiteList | BlackList
10
// WhiteList — 仅反射 SHIT_META(Enable) 标记的字段(opt-in)
11
// BlackList — 反射全部字段,SHIT_META(Disable) 标记的除外(opt-out)
12
// (向后兼容:Fields ≈ BlackList,WhiteListFields ≈ WhiteList)
13
//
14
// 用法:
15
// class SHIT_API SHIT_REFLECT(BlackList) MyComponent : Base { ... };
16
// struct SHIT_REFLECT(WhiteList) MyStruct { ... };
17
//
18
// 展开示例(libClang 下):
19
// class __declspec(dllexport) __attribute__((annotate("shit-reflection:BlackList")))
20
// struct __attribute__((annotate("shit-reflection:WhiteList")))
21
//
22
// SHIT_REFLECT_BODY(Type)
23
// 放在类/结构体体内。展开为 friend 声明,授予生成的 Register_Type()
24
// 访问 private/protected 成员的权限(供成员指针取 offset,ABI 安全)。
25
// 注意:annotate 无法替代 friend 的运行期授权作用,必须保留。
26
//
27
// SHIT_ENUM(Type)
28
// 标记 enum / enum class。放在 enum/class 关键字之后、枚举名之前。
29
// 展开为 __attribute__((annotate("shit-enum:Type")))。
30
//
31
// 用法:
32
// enum class SHIT_ENUM(MyEnum) MyEnum { A, B, C };
33
//
34
// Scanner 遍历枚举项(CXCursor_EnumConstantDecl),提取名称与数值。
35
// 生成的注册代码调用 .Value("A", 0) 登记每个枚举常量。
36
//
37
// SHIT_META(...)
38
// 放在字段上方,为字段添加结构化元数据(供编辑器属性面板显示用)。
39
// 使用双括号语法:SHIT_META(({...}))
40
// 多条 SHIT_META 可叠加(每个生成独立的 annotate 属性)。
41
//
42
// 示例:
43
// SHIT_META(({.displayName = "HP", .range = {0, 100}}))
44
// SHIT_META(Enable) ← WhiteListFields 模式的简写标记
45
46
// libClang(ReflectionScanner 使用的解析器)定义 __clang__,GCC 不定义。
47
// 仅在 __clang__ 下启用 annotate,避免 GCC 编译时产生
48
// -Wattributes "annotate attribute directive ignored" 告警。
49
#if defined(__clang__)
50
#define SHIT_DETAIL_ANNOTATE(str) __attribute__((annotate(str)))
51
#else
52
// 非 clang 编译器(GCC/MSVC):annotate 退化为空。
53
// 不影响 ReflectionScanner——libClang 自带解析器,即使目标编译器是 GCC
54
// 也能识别 __attribute__ 语法(libClang 定义 __clang__)。
55
#define SHIT_DETAIL_ANNOTATE(str)
56
#endif
57
58
// ── 类/结构体级反射 ────────────────────────────────
59
#define SHIT_REFLECT(Mode) SHIT_DETAIL_ANNOTATE("shit-reflection:" #Mode)
60
61
// ── 类内 friend 授权(必须保留) ───────────────────
62
#define SHIT_REFLECT_BODY(Type) friend bool Register_##Type();
63
64
// ── 枚举反射 ──────────────────────────────────────
65
#define SHIT_ENUM(Type) SHIT_DETAIL_ANNOTATE("shit-enum:" #Type)
66
67
// ── 字段级元数据 ──────────────────────────────────
68
#define SHIT_META(...) SHIT_DETAIL_ANNOTATE("shit-meta:" #__VA_ARGS__)
include
ShitEngine
Reflection
Macros.h
制作者
1.17.0