如何使用重新定义的打印功能打印Lua表? [英] How to print Lua table using the redefined print function?

查看:91
本文介绍了如何使用重新定义的打印功能打印Lua表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这篇文章中学到了如何在C ++中重新定义Lua的print(). ( https://stackoverflow.com/a/4514193/5224286 )

I learned how to redefine the Lua's print() in C++ from this post. (https://stackoverflow.com/a/4514193/5224286)

这是重新定义的打印功能,可将变量打印到主机程序的控制台. (通过名为post..的函数)

Here's the redefined print function that prints variables to my host program's console. (through the functions named post..)

int l_my_print(lua_State *L)
{
    int nargs = lua_gettop(L);
    for (int i = 1; i <= nargs; ++i)
    {
        if (lua_isnil(L, i))
            poststring("nil");
        else if (lua_isboolean(L, i))
            lua_toboolean(L, i) ? poststring("true") : poststring("false");
        else if (lua_isnumber(L, i))
            postfloat(static_cast<t_float>(lua_tonumber(L, i)));
        else if (lua_isstring(L, i))
            poststring(lua_tostring(L, i));
        else if (lua_istable(L, i))
            poststring("table: "); //how to print like Lua's built-in print()?
    }
    endpost();
    return 0;
}

除了我打印表格时,这段代码可以正常工作. (现在仅打印table:)

This code works fine except when I print tables. (it just prints table: now)

我想知道如何像Lua的print()一样打印表格.

I wonder how to print tables just like how Lua's print() works.

例如,当我在Lua中运行以下代码时:(在重新定义之前)

For example, when I run the following code in Lua: (before the redefine)

print({1,2,3});

我得到以下结果:(它似乎不断变化)

I get this result: (which seems to change constantly)

table: 0x23b8660

这是指向Lua表的指针的十六进制表示吗?

Is this a hex representation of pointer to the Lua-table?

我应该如何使用l_my_print()函数,使其像Lua的print()一样工作?

What should I do with my l_my_print() function so it can work just like Lua's print()?

推荐答案

只需使用luaL_tolstring即可获取任何内容的字符串表示形式.这也符合__tostring元方法.下面的示例将C ++ 17中的std::string_view用作零复制只读字符串参数.

Just use luaL_tolstring to get the string representation of anything. This also respects the __tostring metamethod. The example below uses std::string_view from C++17 for zero-copy read-only string arguments.

#include <iostream>
#include <string_view>

#include <lua.hpp>

void poststring(std::string_view sv) { std::cout << sv << '\n'; }

void endpost() { std::cout << "---\n"; }

int l_my_print(lua_State *L) {
    int nargs = lua_gettop(L);
    for (int i = 1; i <= nargs; ++i) {
        poststring(luaL_tolstring(L, i, nullptr));
        lua_pop(L, 1); // remove the string
    }
    endpost();
    return 0;
}

int main() {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    lua_pushcfunction(L, l_my_print);
    lua_setglobal(L, "my_print");

    int i = 0;
    lua_pushlightuserdata(L, &i);
    lua_setglobal(L, "udata");

    luaL_dostring(L, "my_print(1, 3.14, \"Hello World\")\n"
                     "my_print(false, udata, {})\n");

    lua_close(L);
}

示例调用:

$ clang++ -Wall -Wextra -Wpedantic -std=c++17 -I/usr/include/lua5.3 test.cpp -llua5.3
$ ./a.out 
1
3.14
Hello World
---
false
userdata: 0x7fff4685993c
table: 0x883300
---

这篇关于如何使用重新定义的打印功能打印Lua表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆