Luabridge绑定重载运算符

编程入门 行业动态 更新时间:2024-10-22 16:21:30
本文介绍了Luabridge绑定重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编写了一个简单的vec3类,该类实现了*/+-运算符:

I've written a simple vec3 class, which implements the */+- operators:

class vec3 { public: vec3(): x(0.0f),y(0.0f),z(0.0f) {} vec3(float ix, float iy, float iz): x(ix),y(iy),z(iz) {} vec3 operator+(const vec3& v){ vec3 vec(x+v.x,y+v.y,z+v.z); return vec; } vec3 operator-(const vec3& v){ vec3 vec(x-v.x,y-v.y,z-v.z); return vec; } vec3 operator*(const float& s){ vec3 vec(x*s,y*s,z*s); return vec; } vec3 operator/(const float& d){ flot div = 1.0f/d; vec3 vec(x*div,y*div,z*div); return vec; } float x, y, z; };

我希望通过luabridge绑定这些运算符,类似于以下方式:

I wish to bind these operators via luabridge, in some way similar to:

getGlobalNamespace(L) .beginClass<vec3>("vec3"); .addConstructor<void (*) (const float&, const float& const float&)>() .addData ("x", &vec3::x) .addData ("z", &vec3::z) .addData ("z", &vec3::y) .addFunction("__add",(vec3*(vec3::*)(vec3)) &vec3::operator+); .addFunction("__sub",(vec3*(vec3::*)(vec3)) &vec3::operator-); .addFunction("__mul",(vec3*(vec3::*)(float)) &vec3::operator*); .addFunction("__div",(vec3*(vec3::*)(float)) &vec3::operator/); .endClass();

这样我就可以像下面这样在lua中调用这些函数了:

So that I can then call the functions in lua like so:

onUpdate = function (e, dt) e.position = e.position + e.velocity * dt; end

当我在vec3类的* operator中放置一个断点时,它确实命中了,但是rhs float没有定义.

When I place a breakpoint in the *operator for the vec3 class, it does hit it, but the rhs float is undefined.

如果我将lua代码更改为:

If I change the lua code to:

onUpdate = function (e, dt) e.position = e.position + e.velocity end

然后,rhs vec3也未定义.因此,看来该参数未正确传递.

Then the rhs vec3 is also undefined. So it looks like the argument is not being passed correctly.

然后我将注册更改为:

getGlobalNamespace(L) .beginClass<vec3>("vec3"); .addConstructor<void (*) (const float&, const float& const float&)>() .addData ("x", &vec3::x) .addData ("z", &vec3::z) .addData ("z", &vec3::y) .addFunction("__add",(vec3*(vec3::*)(const vec3&)) &vec3::operator+); .addFunction("__sub",(vec3*(vec3::*)(const vec3&)) &vec3::operator-); .addFunction("__mul",(vec3*(vec3::*)(const float&)) &vec3::operator*); .addFunction("__div",(vec3*(vec3::*)(const float&)) &vec3::operator/); .endClass();

但是现在vec3的成员数据或float参数未定义.如果我越过此断点,luabdridge会引发以下异常:

But now the member data of the vec3, or the float argument is undefined. If I move past this breakpoint, luabdridge throws the following exception:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

我唯一能想到的就是我以某种方式未正确注册该功能.

The only thing I can think of is that I have somehow not registered the function correctly.

所以我的问题是这样的: 如何正确绑定运算符,并确保参数类型正确,以及如何在lua中正确调用它?

So my question is this: How do I bind the operator correctly, and ensure that the argument type is correct, and how do I correctly call it in lua?

推荐答案

因此,正如我在问题中提到的那样,我怀疑注册未正确完成.我仍然不完全了解具体细节,但是我怀疑函数指针的转换会以某种方式更改调用约定,因此我将代码更改为:

So as I mentioned in my question, I suspected that the registration was not being done correctly. I still don;t fully understand the specifics, but I suspect that the function pointer cast is somehow changing the calling convention, so I changed the code to:

.addFunction("__mul",&vec3::operator*);

效果很好.

更多推荐

Luabridge绑定重载运算符

本文发布于:2023-06-03 12:17:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473766.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   运算符   Luabridge

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!