• Stars
    star
    646
  • Rank 69,672 (Top 2 %)
  • Language
    C++
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Header-only, tiny (99 lines) and powerful C++20 static reflection library.

 __    __       _______..______       _______  _______  __      
|  |  |  |     /       ||   _  \     |   ____||   ____||  |     
|  |  |  |    |   (----`|  |_)  |    |  |__   |  |__   |  |     
|  |  |  |     \   \    |      /     |   __|  |   __|  |  |     
|  `--'  | .----)   |   |  |\  \----.|  |____ |  |     |  `----.
 \______/  |_______/    | _| `._____||_______||__|     |_______|
                                                                

repo-size tag license compiler explorer

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

Integration

You can choose one of the following two methods

  • method 0: add the required file
  • method 1: cmake install, use find package(USRefl REQUIRED) to get imported target Ubpa::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.

More Repositories

1

RenderLab

App of RTR, PTR and Editor
C++
762
star
2

Utopia

Utopia Game Engine 无境游戏引擎
C++
487
star
3

USTC_CG

00106501: Computer Graphics (Spring-Summer 2020)
C++
419
star
4

ToyRTX

CPU & GPU RTX based on OpenGL
C++
389
star
5

UECS

Ubpa Entity-Component-System (U ECS) in Unity3D-style
C++
369
star
6

UDRefl

Ubpa Dynamic Reflection
C++
353
star
7

UGM

Ubpa Graphics Mathematics
C++
320
star
8

GAMES102

GAMES 102
C++
273
star
9

CMU_15_462

notes and assignments of CG (CMU 15-462/662)
C++
91
star
10

UHEMesh

an elegant, high-performance and user-friendly halfedge data structure
C++
79
star
11

UFG

Ubpa Frame Graph
C++
44
star
12

UGraphviz

Ubpa Graphviz C++ wrapper
C++
38
star
13

ULuaPP

Ubpa Lua++ (Lua & C++)
C++
32
star
14

ExponentialHeightFog

[ Unreal to Unity ] Exponential Height Fog
HLSL
28
star
15

UDP

Ubpa Design Pattern
C++
27
star
16

USmallFlat

Ubpa small flat containers based on C++20
C++
27
star
17

UCMake

Ubpa CMake
CMake
23
star
18

UTemplate

C++
23
star
19

LearnOpenGL

OpenGL Exercise
C++
19
star
20

CG_LGLiu

exercises of CG class teached by Prof LiGang Liu in 2018
C++
19
star
21

UDXRenderer

Ubpa DirectX 12 Renderer
C++
18
star
22

RayTracingInOneWeekend

rewrite Ray Tracing in a Weekend
C++
16
star
23

UDLua

Ubpa Lua base on UDRefl
C++
16
star
24

Project-GAMES101

Project-GAMES101
15
star
25

UDX12

Ubpa DirectX 12 C++ Wrapper
C++
10
star
26

MasterCourses

Master Courses
C++
4
star
27

best-cpp-reflection

best cpp reflection
3
star
28

DoubleConeRain

Double Cone Rain
C#
3
star
29

UScene

Ubpa Scene
C++
3
star
30

LearnVulkan

Learn Vulkan
2
star
31

ULua

Ubpa Lua
CMake
2
star
32

LearnRust

Rust
2
star
33

UData

Ubpa Data
C
2
star
34

GAMES102_Data

GAMES_102_Data
2
star
35

RenderLab_bk_20200303

C++
2
star
36

UANTLR

Ubpa ANTLR4
ANTLR
1
star
37

UCommon

Ubpa Common
C++
1
star
38

USTL

Ubpa stl extension
C++
1
star
39

UGit

CMake
1
star
40

OSG_Exercise

C++
1
star
41

UContainer

Ubpa Container
C++
1
star
42

UEngine

Ubpa Engine
C++
1
star
43

UThreadPool

Ubpa Thread Pool
C++
1
star
44

LearnCompiler

Learn Compiler
ANTLR
1
star
45

SecretCode

Encrypt / Decode codes by DES and Base64
C++
1
star
46

UVisitor

Visitor
C++
1
star
47

LearnQt

C++
1
star
48

UANN

ANN (Approximate Nearest Neighbors) library, ported to CMake
C++
1
star