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

    1. <small id='eL7xx'></small><noframes id='eL7xx'>

        <bdo id='eL7xx'></bdo><ul id='eL7xx'></ul>
    2. <tfoot id='eL7xx'></tfoot>
    3. <legend id='eL7xx'><style id='eL7xx'><dir id='eL7xx'><q id='eL7xx'></q></dir></style></legend>
    4. 为嵌入式 Lua 重定向/重新定义 print()

      Redirecting/redefining print() for embedded Lua(为嵌入式 Lua 重定向/重新定义 print())

      <tfoot id='DRHRx'></tfoot>

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

        <tbody id='DRHRx'></tbody>
            1. <legend id='DRHRx'><style id='DRHRx'><dir id='DRHRx'><q id='DRHRx'></q></dir></style></legend>
                <bdo id='DRHRx'></bdo><ul id='DRHRx'></ul>
                <i id='DRHRx'><tr id='DRHRx'><dt id='DRHRx'><q id='DRHRx'><span id='DRHRx'><b id='DRHRx'><form id='DRHRx'><ins id='DRHRx'></ins><ul id='DRHRx'></ul><sub id='DRHRx'></sub></form><legend id='DRHRx'></legend><bdo id='DRHRx'><pre id='DRHRx'><center id='DRHRx'></center></pre></bdo></b><th id='DRHRx'></th></span></q></dt></tr></i><div id='DRHRx'><tfoot id='DRHRx'></tfoot><dl id='DRHRx'><fieldset id='DRHRx'></fieldset></dl></div>
                本文介绍了为嵌入式 Lua 重定向/重新定义 print()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在我的 C++ 应用程序中嵌入了 Lua.我想重定向打印语句(或者可能只是重新定义打印函数?),以便我可以在其他地方显示计算的表达式.

                I have embedded Lua in my C++ application. I want to redirect print statements (or maybe simply redefine the print function?), so that I can display the evaluated expression somewhere else.

                执行此操作的最佳方法是什么:重定向或重新定义 print() 函数?

                What is the best way to do this: redirect or redefining the print() function?

                任何显示如何执行此操作的代码段/指向代码段的指针都将不胜感激.

                Any snippets/pointers to snippets that show how to do this would be much appreciated.

                推荐答案

                你可以在C中重新定义print语句:

                You can redefine the print statement in C:

                static int l_my_print(lua_State* L) {
                    int nargs = lua_gettop(L);
                
                    for (int i=1; i <= nargs; i++) {
                        if (lua_isstring(L, i)) {
                            /* Pop the next arg using lua_tostring(L, i) and do your print */
                        }
                        else {
                        /* Do something with non-strings if you like */
                        }
                    }
                
                    return 0;
                }
                

                然后在全局表中注册:

                static const struct luaL_Reg printlib [] = {
                  {"print", l_my_print},
                  {NULL, NULL} /* end of array */
                };
                
                extern int luaopen_luamylib(lua_State *L)
                {
                  lua_getglobal(L, "_G");
                  // luaL_register(L, NULL, printlib); // for Lua versions < 5.2
                  luaL_setfuncs(L, printlib, 0);  // for Lua versions 5.2 or greater
                  lua_pop(L, 1);
                }
                

                由于您使用的是 C++,因此您需要使用extern "C"" 来包含您的文件

                Since you are using C++ you'll need to include your file using 'extern "C"'

                这篇关于为嵌入式 Lua 重定向/重新定义 print()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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(使用非平凡的构造函数初始化联合)

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

                    <tbody id='B68gi'></tbody>

                    <legend id='B68gi'><style id='B68gi'><dir id='B68gi'><q id='B68gi'></q></dir></style></legend>
                    <tfoot id='B68gi'></tfoot>
                      <bdo id='B68gi'></bdo><ul id='B68gi'></ul>

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