lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Ok, here's my test code with virtual methods tolua++:

-------------------------- header ---------------------------------------

#pragma once

#include <string>
#include <vector>

// lua
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

typedef std::string String;

class MyClass
{
public:
    MyClass(String name);
    String getName();
    virtual void overrideMe() {}
protected:
    String name_;
};

void passMe(MyClass* myClass);

-------------------------- pkg---------------------------------------
$cfile "luatest.h"

$#include "tolua_base.h"

typedef std::string String;

class MyClass
{
public:
    MyClass(String name);
    String getName();
    virtual void overrideMe();
protected:
    String name_;
};

void passMe(MyClass* myClass);

-------------------------- source main ---------------------------------------
#include "stdafx.h"

#include " luatest.h"
#include "VSLua_Debugger.h"
#include <vector>

int tolua_luatest_open (lua_State* tolua_S);
lua_State* state_;

static int report (lua_State *L, int status) {
    if (status && !lua_isnil(L, -1)) {
        const char *msg = lua_tostring(L, -1);
        if (msg == NULL) msg = "(error object is not a string)";
        printf("%s\n", msg);
        lua_pop(L, 1);
    }
    return status;
}

int _tmain(int argc, _TCHAR* argv[])
{
    state_ = lua_open();     // initialize mLuaState for LUA
    luaL_openlibs(state_);

    tolua_luatest_open(state_);

    int status = luaL_dofile(state_, " myFirst.lua");
    if (status != 0)
    {
        report(state_, status);
        assert(false);
    }

    return 0;
}

MyClass::MyClass(std::string name)
{
    name_ = name;
}

String
MyClass::getName()
{
    return name_;
}

void passMe(MyClass* myClass)
{
    myClass->overrideMe();
}

-------------------------- lua ---------------------------------------
local MyDerivedClass = {}
MyDerivedClass.__index = MyDerivedClass

function MyDerivedClass:overrideMe()
    print("overrideMe called in lua")
    print(self:getName())
end

function MyDerivedClass:new(name)

   -- create a table and make it an 'instance' of CustomWidget
   local t = {};
   setmetatable(t, MyDerivedClass);

   -- create a Lua__MyClass object, and make the table inherit from it
   local w = Lua__MyClass:new(name);
   tolua.setpeer(w, t) -- use 'tolua.inherit' on lua 5.0

   -- 'w' will be the lua object where the virtual methods will be looked up
   w:tolua__set_instance(w);

   return w; -- return 't' on lua 5.0 with tolua.inherit
end

instance = MyDerivedClass:new("nacho")
passMe(instance);

------------------------------------------------------------------------------------
output:

override me called in lua
nacho


Hope this helps :)

On 1/27/07, Matthew Armstrong <turkeypotpie@gmail.com> wrote:
This does work, but not so easily right out of the box.

I can't access my computer at work right now, so I'll post my test code on Monday.
As I remember, to get namespaces to work properly, I had to add a hack to the virtual method hooks file.

Though after all we decided to go with luabind, which can also do virtual methods quite easily.  luabind has some great features, but, of course, a few disadvantages too, such as extra compilation time.  If you want to call a lua function, or interpret a lua object, from c++ code, then tolua++ won't help you out much there, afaik.  You have to interface directly with the stack, which works, but isn't so pretty, or use another library in conjunction with tolua++, such as LuaPlus.


On 1/27/07, Megiddo < megiddo@meg-tech.com> wrote:
Original post: http://lua-users.org/lists/lua-l/2006-10/msg00282.html

Sorry to bring up an old thread, but I'm having this same issue. Does
anyone have any ideas what the problem could be? (Basically the
lua-users wiki page "Implementing Virtual Methods With Toluapp" does not
work as advertised)

This would be extremely helpful, hope there's a way to get it working!

   -Brett "Megiddo" Smith