Ubpa / UDRefl

Ubpa Dynamic Reflection
MIT License
353 stars 59 forks source link
cpp cpp20 reflection

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

repo-size tag license

⭐ Star us on GitHub — it helps!

UDRefl

Ubpa Dynamic Reflection

Extremely fast and powerful C++20 dynamic reflection library

Documentaion ->

How to Use

the example code is here, other examples is here

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

Mngr.RegisterType<Vec>();
Mngr.AddField<&Vec::x>("x");
Mngr.AddField<&Vec::y>("y");
Mngr.AddMethod<&Vec::norm>("norm");

Iterate over members

for (auto&& [name, info] : FieldRange_of<Vec>)
  std::cout << name.GetView() << std::endl;

for (auto&& [name, info] : MethodRange_of<Vec>)
  std::cout << name.GetView() << std::endl;

Constructing types

SharedObject v = Mngr.MakeShared(Type_of<Vec>);
std::cout << v.GetType().GetName() << std::endl; // prints "Vec"

Set/get variables

v.Var("x") = 3;
v.Var("y") = 4;
std::cout << "x: " << v.Var("x") << std::endl;

Invoke Methods

std::cout << "norm: " << v.Invoke("norm") << std::endl;

Iterate over variables

for (auto&& [name, var] : v.GetVars())
  std::cout << name.GetView() << ": " << var << std::endl;

other example

Features

Compiler compatibility

Tested platforms:

  • Windows 10: VS2019 16.8.5

  • Ubuntu 20: GCC 10.2, Clang 11.0

  • MacOS 11.0 : GCC 10.2

    AppleClang 12 and Clang 11 is not supported

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.