UDLua icon indicating copy to clipboard operation
UDLua copied to clipboard

Ubpa Lua base on UDRefl

 __    __   _______   __       __    __       ___      
|  |  |  | |       \ |  |     |  |  |  |     /   \     
|  |  |  | |  .--.  ||  |     |  |  |  |    /  ^  \    
|  |  |  | |  |  |  ||  |     |  |  |  |   /  /_\  \   
|  `--'  | |  '--'  ||  `----.|  `--'  |  /  _____  \  
 \______/  |_______/ |_______| \______/  /__/     \__\ 

repo-size tag license

⭐ Star us on GitHub — it helps!

UDLua

Ubpa Lua base on UDRefl (the "best" C++20 dynamic reflection library), which make it easy to use C++ in Lua.

Documents

  • todo

How to use

the example code is here

Suppose you need to use struct vec

struct vec {
  float x;
  float y;
  float norm() const { return std::sqrt(x * x + y * y); }
};

Manual registration on Lua

if you want to do it on C++, check out this link ->.

local vec = UDRefl.RegisterType({
  type = "vec",
  fields = {
    { type = "float32", name = "x" },
    { type = "float32", name = "y" },
  },
  methods = {
    {
      name = "norm",
      result = "float32",
      body = function (p)
        return math.sqrt(UDRefl.unbox(p.x * p.x + p.y * p.y))
      end
    }
  }
})

Iterate over members

for iter in ObjectView.new(vec):GetFields():__range() do
  local name, info = iter:__indirection():__tuple_bind()
  print(name:GetView())
end

for iter in ObjectView.new(vec):GetMethods():__range() do
  local name, info = iter:__indirection():__tuple_bind()
  print(name:GetView())
end

Constructing types

v = SharedObject.new(vec)
print(v:GetType():GetName()) -- prints "vec"

Set/get variables

v.x = 3
v.y = 4
print("x: " .. v.x) -- prints "3"

Invoke Methods

print("norm: " .. v:norm()) -- prints "5"

Iterate over varables

for iter in v:GetTypeFieldVars():__range() do
  local name, var = iter:__indirection():__tuple_bind()
  print(name:GetView() .. ": ".. var)
end

Compiler compatibility

  • Clang/LLVM >= 10.0
  • GCC >= 10.0
  • MSVC >= 1926

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