zoukankan      html  css  js  c++  java
  • C++ 调用 Lua

    直接上代码:

    1:c++代码

      • #include <lua.hpp>
        #include <LuaBridge/LuaBridge.h>
        
        #include <iostream>
        #include <string>
        
        class A
        {
        public:
                void action()
                {
                        std::cout<<"Hello I am A
        ";
                }
        
                virtual void doPrint(int a,int b)
                {
                        std::cout<<"in A a:"<<a<<"b:"<<b<<std::endl;
                }
        
                std::string goodMan() const
                {
                        return "goodman";
                }
        };
        
        class B : public A
        {
        public:
                void hello(const std::string& info) const
                {
                        std::cout<<"hello:"<<info<<std::endl;
                }
        
                virtual void doPrint(int a, int b) override
                {
                        std::cout<<"in B just"<<(a + b) <<std::endl;
                }
        };
        
        void globalFunction()
        {
                std::cout<<"hello this is a global func
        ";
        }
        
        bool reloadLuaScript(lua_State* L, const std::string& luafile)
        {
                int state = luaL_dofile(L, luafile.c_str());
                if(state != LUA_OK)
                {
                        return false;
                }
                return true;
        }
        
        void registerClassAndFunctions(lua_State* L)
        {
                using namespace luabridge;
                //注册全局函数和类函数
                getGlobalNamespace(L).addFunction("gloabalFunction",globalFunction); 
                getGlobalNamespace(L)
                        .beginClass<A>("A")
                        .addFunction("action",&A::action)
                        .addFunction("doPrint", &A::doPrint)
                        .addFunction("goodMan", &A::goodMan)
                        .endClass()
                        .deriveClass<B,A>("B")
                        .addFunction("hello", &B::hello)
                        .endClass();
        }
        
        void testCallLua(lua_State* L)
        {
                A a;
                lua_getglobal(L,"testA");
                luabridge::push(L, &a);
                lua_pcall(L,1,0,0);
        }
        
        int main(int argc, char** argv)
        {
                lua_State* L = luaL_newstate();
        
                luaL_openlibs(L);
                std::cout<<"try load file "<<argv[1]<<std::endl;
        
                auto ok = reloadLuaScript(L, argv[1]);
                if(!ok)
                {
                        std::cout<<"load lua file failed
        ";
                }
                else
                {
                        registerClassAndFunctions(L);
                        testCallLua(L);
                }
                lua_close(L);
                L = nullptr;
        }
        

    2:Lua代码

      • --print("hello");
        --[[
                this is note
                这个是多行注释
        --]]
        print("This is myWorld!
        ");
        function testA(a)
                a:action();
                a:doPrint(1,2);
        end
        ~                                                                                                                                                                                                                 
        ~       
        

    3:编译运行

      • [root@10-120-10-106 NewChart]# g++ -std=c++11 -o testlua testLua.cpp -llua -ldl
        [root@10-120-10-106 NewChart]# ./testlua abc.lua 
        try load file abc.lua
        This is myWorld!
        
        Hello I am A
        in A a:1b:2
        

          

  • 相关阅读:
    HTTP状态码的详细解释,供参考
    js中实现页面跳转(返回前一页、后一页)
    CRUD工程师——基础容器LinkedList
    CRUD工程师——基础容器ArrayList
    Servlet入门
    Linux
    flex弹性布局
    div布局
    递归函数
    运算符
  • 原文地址:https://www.cnblogs.com/zhaohu/p/9407552.html
Copyright © 2011-2022 走看看