项目
在 GitHub 查看
Ubpa/USRefl
关于
仅头文件、精简(99 行)且功能强大的 C++20 静态反射库。
开发工具实测星标增长
- 24小时
- +1
- 7天
- +1
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文
__ __ _______..______ _______ _______ __
| | | | / || _ \ | ____|| ____|| |
| | | | | (----`| |_) | | |__ | |__ | |
| | | | \ \ | / | __| | __| | |
| `--' | .----) | | |\ \----.| |____ | | | `----.
\______/ |_______/ | _| `._____||_______||__| |_______|
⭐ Star us on GitHub — it helps!
USRefl
Ubpa Static Reflection
Header-only, tiny (99 lines) and powerful C++20 static reflection library.
Feature
- header-only, tiny (99 lines) and powerful (USRefl_99.h (MSVC & GCC), USRefl_99_clang.h (Clang))
- noninvasive
- basic
- (non-static / static) member variable
- (non-static / static) member function
- attribute
- enum
- string <-> enumerator
- static dispatch
- template
- inheritance
- diamond inheritance
- iterate bases recursively
- virtual inheritance
- parser
How to use
run it online : compiler explorer
Suppose you need to reflect struct Vec
struct Vec {
float x;
float y;
float norm() const { return std::sqrt(x*x + y*y); }
};
Manual registration
template<>
struct Ubpa::USRefl::TypeInfo<Vec> :
TypeInfoBase<Vec>
{
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("x") , &Type::x },
Field {TSTR("y") , &Type::y },
Field {TSTR("norm"), &Type::norm},
};
};
if you use 99-lines version, you need to add a line
static constexpr std::string_view name = "...";
Iterate over members
TypeInfo<Vec>::fields.ForEach([](const auto& field) {
std::cout << field.name << std::endl;
});
Set/get variables
std::invoke(TypeInfo<Vec>::fields.Find(TSTR("y")).value, v) = 4.f;
std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v) = 3.f;
std::cout
<< "x: "
<< std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v)
<< std::endl;
Invoke Methods
std::cout
<< "norm: "
<< std::invoke(TypeInfo<Vec>::fields.Find(TSTR("norm")).value, v)
<< std::endl;
Iterate over variables
TypeInfo<Vec>::ForEachVarOf(v, [](const auto& field, const auto& var) {
std::cout << field.name << ": " << var << std::endl;
});
other example
- 99 line: USRefl_99.h, test, online (test all examples below)
- template
- static
- func
- enum
- inheritance
- virtual inheritance
- attribute name as type
- AutoRefl : app, example (Vec.h, Vec_AutoRefl.inl (generated by AutoRefl), main.cpp)
Integration
You can choose one of the following two methods
- ⭐ method 0: add the required file
- MSVC and GCC: USRefl_99.h
- Clang : USRefl_99_clang.h
- ⭐ method 1: cmake install, use
find package(USRefl REQUIRED)to get imported targetUbpa::USRefl_core
Compiler compatibility
- Clang/LLVM >= 10
- GCC >= 10
- MSVC >= 1926 (not fully support virtual inheritance because of a MSVC bug)
Licensing
You can copy and paste the license summary from below.
MIT License
Copyright (c) 2020 Ubpa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
探索相关项目
加载中…