LuaPlus學習(四)

來源:互聯網
上載者:User
<--!著作權foruok,轉載註明出處!-->    一個完整的例子

     實驗了一個完整的例子。
    在C++中產生一個類,註冊到lua。
    從lua調用C++類的成員函數。
    在Alternate.lua中寫了階乘函數factorial。
    從C++中調用factorial。
    Alternate.lua如下:

print("now in alternate.lua");
print("initial number of AxhSystem:"..AxhSystem:GetNumber()); 
AxhSystem:SetNumber(15);
print("Change the number from lua,new:"..AxhSystem:GetNumber());
print("Now Set message to 'Hello world'");
AxhSystem:SetMessage("Hello world");
print("AxhSystem:GetMessage return:"..AxhSystem:GetMessage());
print(" now, enter a number to change AxhSystem's number:");
num=io.read("*number");
print("you enter:"..num);
print("set to AxhSystem")
AxhSystem:SetNumber(num);
print("AxhSystem's new number="..AxhSystem:GetNumber());
function factorial(n)
    if( n==0 ) then
        return 1;
    else
        return n * factorial(n-1);
    end
end

    C++代碼如下:


class CAxhSystem
...{
public:
    CAxhSystem():m_nNumber(0)
    ...{
        memset(m_szMsg, 0, sizeof(m_szMsg));
    }
    ~CAxhSystem()
    ...{
    }
    void SetNumber(int num)
    ...{
        m_nNumber = num;
    }
    int GetNumber()
    ...{
        return m_nNumber;
    }
    void SetMessage(const char * szMsg)
    ...{
        if(szMsg)
            _tcsncpy(m_szMsg, szMsg, 256);
    }
    char * GetMessage()
    ...{
        return m_szMsg;
    }
protected:
    int m_nNumber;
    char m_szMsg[256];
} ;
CAxhSystem g_system;
LuaStateOwner g_state;
LuaObject g_globalObj;
int InitLua()
...{
    g_state->OpenLibs();
    g_globalObj = g_state->GetGlobals();
    return 0;
}
int RegisterObject()
...{
    LuaObject axhmetaobj = g_globalObj.CreateTable("AxhMetaTable");
    axhmetaobj.SetObject("__index", axhmetaobj);
    //register functor
    axhmetaobj.RegisterObjectDirect("SetNumber", (const CAxhSystem*)0, CAxhSystem::SetNumber);
    axhmetaobj.RegisterObjectDirect("GetNumber", (const CAxhSystem*)0, CAxhSystem::GetNumber);
     axhmetaobj.RegisterObjectDirect("SetMessage", (const CAxhSystem*)0, CAxhSystem::SetMessage);
     axhmetaobj.RegisterObjectDirect("GetMessage", (const CAxhSystem*)0, CAxhSystem::GetMessage);
    //get lua object
    LuaObject luasys =  g_state->BoxPointer(&g_system);
    luasys.SetMetaTable(axhmetaobj);
    g_globalObj.SetObject("AxhSystem", luasys);
    return 0;
}
int _tmain(int argc, _TCHAR* argv[])
...{
    InitLua();
    RegisterObject();
    g_state->DoFile("Alternate.lua");
    LuaFunction<int> factorial = g_state->GetGlobal("factorial");
    printf(" Now call 'factoral' in Alternate.lua, enter a number:");
    int number;
    scanf("%d", &number);
    printf("You ennter %d, it's factorial=%d ", number, factorial(number));
    return 0;
}

    OK,到這裡已經可以在lua和c++之間做一些互動了,可以根據需要實現我們的目標了。
    接下來我要好好學習lua了,不然沒辦法用它來寫複雜的邏輯。
<--!著作權foruok,轉載註明出處!-->

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.