https://github.com/nglsg/rttm
Blazing-fast C++ reflection powered by pure standard C++ - Zero macros, compiler-automated type introspection
https://github.com/nglsg/rttm
c-plus-plus c-plus-plus-17 cmake ecs game-development high-performance reflection serialization serializations template-library zero-overhead
Last synced: 11 months ago
JSON representation
Blazing-fast C++ reflection powered by pure standard C++ - Zero macros, compiler-automated type introspection
- Host: GitHub
- URL: https://github.com/nglsg/rttm
- Owner: NGLSG
- License: mit
- Created: 2025-02-21T09:47:23.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-06-03T09:41:29.000Z (about 1 year ago)
- Last Synced: 2025-06-03T20:37:08.919Z (about 1 year ago)
- Topics: c-plus-plus, c-plus-plus-17, cmake, ecs, game-development, high-performance, reflection, serialization, serializations, template-library, zero-overhead
- Language: C++
- Homepage:
- Size: 158 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RTTM
**Runtime Turbo Mirror**
高性能、轻量级的C++17动态反射库
[](https://en.cppreference.com/w/cpp/17)
[](LICENSE)
[]()
[]()
---
## 🎯 概述
RTTM是一个专为**游戏引擎**和**性能敏感应用**设计的现代C++反射库。基于C++17标准,零外部依赖,提供运行时类型信息、动态对象创建和方法调用等核心功能。
## ✨ 核心特性
### 🚀 高性能
- 比主流库快 **51%**
- 内存占用减少 **50%**
- 多线程优化设计
### 🔧 零依赖
- 仅需 C++17 标准库
- 跨平台兼容
- 支持 MSVC/GCC/Clang
### 💡 易用性
- 直观的 API 设计
- 链式调用支持
- 自动注册机制
**支持反射**:枚举 • 类/结构体 • 模板类 • 全局变量 • 全局函数
## 📊 性能基准
🏆 与主流库性能对比
| 测试维度 | RTTM | Boost.Hana | RTTR |
|---------|---------------------------------------------------------|--------------------------------------------------|---------------------------------------------------|
| **序列化时间** | **2946ms** | 3343ms (+13%) | 4450ms (+51%) |
| **属性访问** | **1.5ns** | 1.5ns | 13.7ns (+813%) |
| **多线程吞吐** | **1354 ops/ms** | 896 ops/ms | 459 ops/ms |
| **内存效率** | **4 KB/千对象** | 8 KB/千对象 | 8 KB/千对象 |
|**对象创建**| 271us/千对象 (+3387.5%) | **8us/千对象** | **7us/千对象** |
> 🔬 **测试环境**:MSVC Release模式,基于100w对象操作场景
## 🚀 快速开始
### 1️⃣ 引入头文件
```cpp
#include "RTTM/RTTM.hpp"
using namespace RTTM;
```
### 2️⃣ 注册类型
```cpp
class Person {
public:
std::string name;
int age = 0;
Person() = default;
Person(const std::string& n, int a) : name(n), age(a) {}
std::string greeting() { return "Hello, I'm " + name; }
};
// 注册反射信息
RTTM_REGISTRATION {
Registry_()
.property("name", &Person::name)
.property("age", &Person::age)
.method("greeting", &Person::greeting)
.constructor<>()
.constructor();
}
```
### 3️⃣ 动态操作
```cpp
// 获取类型并创建实例
auto personType = RType::Get();
auto result = personType->Create("Alice", 30);
// 属性操作
personType->GetProperty("name") = "Bob";
int age = personType->GetProperty("age");
// 方法调用
std::string greeting = personType->Invoke("greeting");
```
## 🎮 ECS系统示例
💡 查看完整的实体组件系统实现
```cpp
#include "RTTM/Entity.hpp"
// 健康组件
class Health : public RTTM::Component {
public:
int hp = 100;
Health(int h = 100) : hp(h) {}
std::string GetTypeName() const override { return "Health"; }
std::type_index GetTypeIndex() const override { return std::type_index(typeid(Health)); }
};
// 武器系统(抽象组件)
class WeaponSystem : public RTTM::SingletonComponent {
public:
COMPONENT_DEPENDENCIES(Health) // 声明依赖
int damage = 10;
virtual void Attack() = 0;
std::string GetTypeName() const override { return "WeaponSystem"; }
std::type_index GetTypeIndex() const override { return std::type_index(typeid(WeaponSystem)); }
};
// 具体武器实现
class Sword : public WeaponSystem {
public:
Sword() { damage = 30; }
void Attack() override { std::cout << "剑击!伤害:" << damage << std::endl; }
std::string GetTypeName() const override { return "Sword"; }
std::type_index GetTypeIndex() const override { return std::type_index(typeid(Sword)); }
};
// 战士实体
class Fighter : REQUIRE_COMPONENTS(WeaponSystem) {
public:
void Attack() {
GetComponentDynamic().Attack();
}
template
void ChangeWeapon() {
SwapComponent();
}
};
// 使用示例
int main() {
Fighter player;
player.AddComponent(80);
player.AddComponent();
player.Attack(); // 剑击!伤害:30
player.ChangeWeapon(); // 动态切换武器
player.Attack(); // 射击!伤害:20
}
```
## 🔄 序列化支持
📝 JSON序列化示例
```cpp
#include
using json = nlohmann::json;
// 通用序列化函数
json ToJson(const RType& type) {
json j;
for (const auto& name : type.GetPropertyNames()) {
auto prop = type.GetProperty(name);
if (prop->Is()) j[name] = prop->As();
else if (prop->Is()) j[name] = prop->As();
else if (prop->IsClass()) j[name] = ToJson(*prop);
}
return j;
}
// 通用反序列化函数
void FromJson(const RType& type, const json& j) {
for (const auto& name : type.GetPropertyNames()) {
if (j.contains(name)) {
auto prop = type.GetProperty(name);
if (prop->Is()) prop->SetValue(j[name].get());
else if (prop->IsClass()) FromJson(*prop, j[name]);
}
}
}
```
## ⚙️ 构建集成
### 系统要求
- **C++17** 或更高版本
- **编译器**:MSVC 2019+ / GCC 7+ / Clang 5+
- **平台**:Windows / Linux / macOS
### CMake集成
```cmake
# 添加RTTM
add_executable(MyProject main.cpp)
target_link_libraries(MyProject PRIVATE RTTM)
# 启用自动反射生成
include(/cmake/reflection.cmake)
rttm_add_reflection(MyProject)
```
### 手动集成
```bash
# 1. 克隆仓库
git clone https://github.com/NGLSG/RTTM.git
# 2. 添加到项目
# 将RTTM文件夹复制到项目中
# 3. 编译选项
# GCC/Clang: -std=c++17
# MSVC: /std:c++17
```
## 📚 高级特性
| 特性 | 说明 | 示例 |
|------|------|------|
| **枚举反射** | 支持枚举值的动态访问 | `Enum::Get()` |
| **模板类** | 支持模板类型反射 | `Registry_>()` |
| **全局函数** | 注册和调用全局函数 | `Global::RegisterMethod()` |
| **继承支持** | 支持类继承关系反射 | `base()` 链式调用 |
| **自动依赖** | ECS组件自动依赖管理 | `COMPONENT_DEPENDENCIES()` |
## 🤝 贡献指南
我们欢迎所有形式的贡献!
1. 🍴 **Fork** 本仓库
2. 🌿 创建特性分支:`git checkout -b feature/amazing-feature`
3. 💾 提交更改:`git commit -m 'Add amazing feature'`
4. 📤 推送分支:`git push origin feature/amazing-feature`
5. 🔄 创建 **Pull Request**
## 📄 许可证
本项目采用 [MIT许可证](LICENSE) - 查看文件了解详情
---
🌟 为高性能应用打造的现代C++反射解决方案
**Made with ❤️ by [NGLSG](https://github.com/NGLSG)**
[](https://github.com/NGLSG/RTTM)
[](https://github.com/NGLSG/RTTM/fork)
[](https://github.com/NGLSG/RTTM)