Code Monkey home page Code Monkey logo

udlua's Introduction

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

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

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

udlua's People

Contributors

ubpa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

udlua's Issues

Does not compile (Does compile after some tweaking)

俺C++也就搬砖工水平,不太能确定这个是不是正常现象,如果这个issue显得比较蠢求轻拍(

OS: Linux Mint 20.1 x86_64
Compiler: gcc-10 (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
UDRefl: master(8e0c18a)
ULuaW: master(239b2fa)

> make
Scanning dependencies of target UDLua_core
[ 20%] Building CXX object src/core/CMakeFiles/UDLua_core.dir/UDLua.cpp.o
/home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp: In function ‘int f_meta(lua_State*)’:
/home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp:76:48: error: expected primary-expression before ‘>’ token
   76 |     else if (functor.type.Is<UDRefl::ObjectView>()) {
      |                                                ^
/home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp:76:50: error: expected primary-expression before ‘)’ token
   76 |     else if (functor.type.Is<UDRefl::ObjectView>()) {
      |                                                  ^
/home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp:80:50: error: expected primary-expression before ‘>’ token
   80 |     else if (functor.type.Is<UDRefl::SharedObject>()) {
      |                                                  ^
/home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp:80:52: error: expected primary-expression before ‘)’ token
   80 |     else if (functor.type.Is<UDRefl::SharedObject>()) {
      |                                                    ^
In file included from /usr/include/c++/10/memory:63,
                 from /usr/Ubpa/ULuaW_0_1_1/include/ULuaW/LuaState.h:5,
                 from /usr/Ubpa/ULuaW_0_1_1/include/ULuaW/ULuaW.h:3,
                 from /home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/Util.h:3,
                 from /home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp:3:
/usr/include/c++/10/bits/stl_algobase.h: In instantiation of ‘constexpr _Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = Ubpa::UDRefl::MethodRange::iterator; _Predicate = __gnu_cxx::__ops::_Iter_pred<f_ObjectView_index(lua_State*)::<lambda(Ubpa::UDRefl::ObjectView, Ubpa::Name)>::<lambda(const auto:42&)> >]’:
/usr/include/c++/10/bits/stl_algo.h:3934:28:   required from ‘constexpr _IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = Ubpa::UDRefl::MethodRange::iterator; _Predicate = f_ObjectView_index(lua_State*)::<lambda(Ubpa::UDRefl::ObjectView, Ubpa::Name)>::<lambda(const auto:42&)>]’
/home/ikigai/SoftData/libs/ubpa-multi/UDLua/src/core/UDLua.cpp:407:4:   required from here
/usr/include/c++/10/bits/stl_algobase.h:1974:23: error: no matching function for call to ‘__find_if(Ubpa::UDRefl::MethodRange::iterator&, Ubpa::UDRefl::MethodRange::iterator&, __gnu_cxx::__ops::_Iter_pred<f_ObjectView_index(lua_State*)::<lambda(Ubpa::UDRefl::ObjectView, Ubpa::Name)>::<lambda(const auto:42&)> >&, std::__iterator_traits<Ubpa::UDRefl::MethodRange::iterator, void>::iterator_category)’
 1974 |       return __find_if(__first, __last, __pred,
      |              ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
 1975 |          std::__iterator_category(__first));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/stl_algobase.h:1909:5: note: candidate: ‘template<class _InputIterator, class _Predicate> constexpr _InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag)’
 1909 |     __find_if(_InputIterator __first, _InputIterator __last,
      |     ^~~~~~~~~
/usr/include/c++/10/bits/stl_algobase.h:1909:5: note:   template argument deduction/substitution failed:
/usr/include/c++/10/bits/stl_algobase.h:1975:34: note:   cannot convert ‘std::__iterator_category<Ubpa::UDRefl::MethodRange::iterator>(__first)’ (type ‘std::__iterator_traits<Ubpa::UDRefl::MethodRange::iterator, void>::iterator_category’ {aka ‘std::output_iterator_tag’}) to type ‘std::input_iterator_tag’
 1975 |          std::__iterator_category(__first));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/10/bits/stl_algobase.h:1921:5: note: candidate: ‘template<class _RandomAccessIterator, class _Predicate> constexpr _RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag)’
 1921 |     __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~~~~~~
/usr/include/c++/10/bits/stl_algobase.h:1921:5: note:   template argument deduction/substitution failed:
/usr/include/c++/10/bits/stl_algobase.h:1975:34: note:   cannot convert ‘std::__iterator_category<Ubpa::UDRefl::MethodRange::iterator>(__first)’ (type ‘std::__iterator_traits<Ubpa::UDRefl::MethodRange::iterator, void>::iterator_category’ {aka ‘std::output_iterator_tag’}) to type ‘std::random_access_iterator_tag’
 1975 |          std::__iterator_category(__first));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/10/bits/stl_algobase.h:1972:5: note: candidate: ‘template<class _Iterator, class _Predicate> constexpr _Iterator std::__find_if(_Iterator, _Iterator, _Predicate)’
 1972 |     __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
      |     ^~~~~~~~~
/usr/include/c++/10/bits/stl_algobase.h:1972:5: note:   template argument deduction/substitution failed:
/usr/include/c++/10/bits/stl_algobase.h:1974:23: note:   candidate expects 3 arguments, 4 provided
 1974 |       return __find_if(__first, __last, __pred,
      |              ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
 1975 |          std::__iterator_category(__first));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [src/core/CMakeFiles/UDLua_core.dir/build.make:63: src/core/CMakeFiles/UDLua_core.dir/UDLua.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:115: src/core/CMakeFiles/UDLua_core.dir/all] Error 2
make: *** [Makefile:152: all] Error 2

My tweaks:

UDLua.cpp 76:
- else if (functor.type.Is<UDRefl::ObjectView>()) {
+ else if (functor.type.template Is<UDRefl::ObjectView>()) {

UDLua.cpp 80:
- else if (functor.type.Is<UDRefl::SharedObject>()) {
+ else if (functor.type.template Is<UDRefl::SharedObject>()) {

UDLua.cpp 405-407:
- return std::find_if(methods.begin(), methods.end(), [name](const auto& name_methodinfo) {
- 	return std::get<const Name>(name_methodinfo) == name;
- }) != methods.end();
+ for (auto& method : methods){
+ 	if (std::get<const Name>(method) == name){
+ 		return true;
+ 	}
+ }
+ return false;

`RegisterType` strangely fails: failure running test#4 after test#1

Run only test#4:

> ./UDLua_test_00_basic
require "test_04_overload"
y
x
__add
__add
__ctor
vec& vec::operator+(float32)
4       5
vec& vec::operator+(&{const{vec}})
7       9

Run test#1 then test#4:

> ./UDLua_test_00_basic
require "test_01_exception"
./test_01_exception.lua:24: Ubpa::details::CallHandle::__call : Invoke exception.
Ubpa::UDRefl::SharedObject::new_MethodPtr::lambda: The result type is reference, so the return type must be a ObjectView/SharedObject
require "test_04_overload"
./test_04_overload.lua:2: UDRefl::RegisterType : Call Ubpa::UDRefl::ReflMngr::RegisterType failed.

After replacing all vec with vec2 in test#1:

> ./UDLua_test_00_basic
require "test_01_exception"
./test_01_exception.lua:24: Ubpa::details::CallHandle::__call : Invoke exception.
Ubpa::UDRefl::SharedObject::new_MethodPtr::lambda: The result type is reference, so the return type must be a ObjectView/SharedObject
require "test_04_overload"
y
x
__add
__add
__ctor
vec& vec::operator+(float32)
4       5
vec& vec::operator+(&{const{vec}})
7       9

Update: I just realized vec in test#1 was not really erased from info since it fails to register at the beginning and following-up lines were not actually executed, so I changed the title. However it is still strange vec in test#1 seems to own its place in info even when registration fails and throws an error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.