Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
注册C++;用于Lua 5.2的类 我正在阅读如何创建和登记一个C++类,用于LuaVist._C++_Class_Lua - Fatal编程技术网

注册C++;用于Lua 5.2的类 我正在阅读如何创建和登记一个C++类,用于LuaVist.

注册C++;用于Lua 5.2的类 我正在阅读如何创建和登记一个C++类,用于LuaVist.,c++,class,lua,C++,Class,Lua,但是,尽管它简单、信息丰富、易读,但它似乎适用于Lua的旧版本 因此,某些函数/宏要么缺失,要么只是具有不同的签名 以下代码在LuaC 5.2版中是什么样子的? #include <lua.h> #include <lualib.h> #include <lauxlib.h> #include <stringstream> #include <string> using namespace std; // Just a useles

但是,尽管它简单、信息丰富、易读,但它似乎适用于Lua的旧版本

因此,某些函数/宏要么缺失,要么只是具有不同的签名

以下代码在LuaC 5.2版中是什么样子的?

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stringstream>
#include <string>

using namespace std;

// Just a useless test class
class MyClass
{
private:
    int _X;

public:
    MyClass(int x) : _X(x) {};
    ~MyClass() { Release() };

    // Displays your number in a welcoming message box
    void Hello()
    {
        stringstream ss;
        ss << "Hello!" << endl << "Your number is: " << _X;
        string s(ss.str());
        MessageBoxA(NULL, s.c_str(), "MyClass", MB_ICONINFORMATION);
    }

    void Release() {
        //release code goes here
    }
};

// Functions that will create/destroy MyClass instances
static int newMyClass(lua_State* L)
{
    int n = lua_gettop(L); // Number of arguments
    if (n != 2)
        return luaL_error(L, "Got %d arguments, expected 2 (class, x)", n);
    // First argument is now a table that represent the class to instantiate
    luaL_checktype(L, 1, LUA_TTABLE);

    lua_newtable(L); // Create table to represent instance

    // Set first argument of new to metatable of instance
    lua_pushvalue(L, 1);
    lua_setmetatable(L, -2);

    // Do function lookups in metatable
    lua_pushvalue(L, 1);
    lua_setfield(L, 1, "__index");

    // Allocate memory for a pointer to to object
    MyClass** s = (MyClass**)lua_newuserdata(L, sizeof(MyClass*));

    int x = luaL_checknumber(L, 2);

    *s = new MyClass(x);

    luaL_getmetatable(L, "Lua.MyClass");
    lua_setmetatable(L, -2);
    lua_setfield(L, -2, "__self");

    return 1;
}

static int doSomethingMyClass(lua_State* L)
{
    MyClass* c = nullptr;
    checkUserData(L, "Lua.MyClass", c);
    c->Hello();
    return 0;
}

static int destroyMyClass(lua_State* L)
{
    MyClass* c = nullptr;
    checkUserData(L, "Lua.MyClass", c);
    c->Release();
    return 0;
}

// Functions that will show up in our Lua environment
static const luaL_Reg gMyClassFuncs[] = {
    // Creation
    { "new", newMyClass) },
    { "hello", helloMyClass },
    { NULL, NULL }
};

static const luaL_Reg gDestroyMyClassFuncs[] = {
    {"__gc", destroyMyClass},
    {NULL, NULL}
};

// Registers the class for use in Lua
void registerMyClass(lua_State *L)
{  
    // Register metatable for user data in registry
    luaL_newmetatable(L, "Lua.MyClass");
    luaL_register(L, 0, gDestroyMyClassFuncs);      
    luaL_register(L, 0, gMyClassFuncs);      
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");  

    // Register the base class for instances of Sprite
    luaL_register(L, "MyClass", gSpriteFuncs);  
}

我需要更改什么才能使5.2正常工作?

该代码中唯一不属于Lua5.2的功能是
Lualu寄存器
。您应该改用
luaL_setfuncs

您还应该手动设置全局
MyClass
,或者在Lua代码中使用
local MyClass=require“MyClass”
,因为
require
不再设置全局


如果要嵌入Lua,只需使用
-DLUA\u COMPAT\u MODULE
编译它和代码,即可获得5.1函数。但是如果你打算使用这个版本,现在是把你的代码移动到Lua 5.2的好时机。但看起来最新版本是在2009年初发布的,这意味着它最多只支持Lua的5.1版本,这让我回到了我的原始问题,因为我的项目使用5.2,但我还没有研究如何让它与5.2一起工作,我想是时候进行vm克隆了。
-- Create a new MyClass instance
local c = MyClass:new(5)
-- Show message
c:Hello() -- Should say something like "Hello! Your number is: 5"