<i id='xMjoe'><tr id='xMjoe'><dt id='xMjoe'><q id='xMjoe'><span id='xMjoe'><b id='xMjoe'><form id='xMjoe'><ins id='xMjoe'></ins><ul id='xMjoe'></ul><sub id='xMjoe'></sub></form><legend id='xMjoe'></legend><bdo id='xMjoe'><pre id='xMjoe'><center id='xMjoe'></center></pre></bdo></b><th id='xMjoe'></th></span></q></dt></tr></i><div id='xMjoe'><tfoot id='xMjoe'></tfoot><dl id='xMjoe'><fieldset id='xMjoe'></fieldset></dl></div>

        <bdo id='xMjoe'></bdo><ul id='xMjoe'></ul>

      <small id='xMjoe'></small><noframes id='xMjoe'>

      <legend id='xMjoe'><style id='xMjoe'><dir id='xMjoe'><q id='xMjoe'></q></dir></style></legend>
    1. <tfoot id='xMjoe'></tfoot>

      1. Luabridge 绑定重载运算符

        Luabridge binding overloaded operators(Luabridge 绑定重载运算符)

        1. <tfoot id='B1p8K'></tfoot>
              <bdo id='B1p8K'></bdo><ul id='B1p8K'></ul>
              • <i id='B1p8K'><tr id='B1p8K'><dt id='B1p8K'><q id='B1p8K'><span id='B1p8K'><b id='B1p8K'><form id='B1p8K'><ins id='B1p8K'></ins><ul id='B1p8K'></ul><sub id='B1p8K'></sub></form><legend id='B1p8K'></legend><bdo id='B1p8K'><pre id='B1p8K'><center id='B1p8K'></center></pre></bdo></b><th id='B1p8K'></th></span></q></dt></tr></i><div id='B1p8K'><tfoot id='B1p8K'></tfoot><dl id='B1p8K'><fieldset id='B1p8K'></fieldset></dl></div>

                <small id='B1p8K'></small><noframes id='B1p8K'>

                <legend id='B1p8K'><style id='B1p8K'><dir id='B1p8K'><q id='B1p8K'></q></dir></style></legend>
                    <tbody id='B1p8K'></tbody>

                  本文介绍了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 浮点数未定义.

                  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 绑定重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                      <tbody id='qwobl'></tbody>

                    <i id='qwobl'><tr id='qwobl'><dt id='qwobl'><q id='qwobl'><span id='qwobl'><b id='qwobl'><form id='qwobl'><ins id='qwobl'></ins><ul id='qwobl'></ul><sub id='qwobl'></sub></form><legend id='qwobl'></legend><bdo id='qwobl'><pre id='qwobl'><center id='qwobl'></center></pre></bdo></b><th id='qwobl'></th></span></q></dt></tr></i><div id='qwobl'><tfoot id='qwobl'></tfoot><dl id='qwobl'><fieldset id='qwobl'></fieldset></dl></div>

                          <small id='qwobl'></small><noframes id='qwobl'>

                          <legend id='qwobl'><style id='qwobl'><dir id='qwobl'><q id='qwobl'></q></dir></style></legend>

                          <tfoot id='qwobl'></tfoot>
                            <bdo id='qwobl'></bdo><ul id='qwobl'></ul>