For example, there have a class written in C++:
//Say.h
#pragma once
#include <iostream>
class Say
{
public:
Say() {}
virtual ~Say() {}
virtual void SaySomething() { std::cout << "It should not be show..\n"; };
};
inline void CallCppFun(Say& intf) {
intf.SaySomething();
}
and I write the Say.i:
//Say.i
%module Test
%{
#include "Say.h"
%}
%include "Say.h"
%inline %{
inline void CallCppFun(Say& intf);
%}
and main.cpp:
//main.cpp
#include <iostream>
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
/* the SWIG wrappered library */
extern "C" int luaopen_Test(lua_State*L);
using namespace std;
int main()
{
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
printf("[C] now loading the SWIG wrapped library\n");
luaopen_Test(L);
if (luaL_loadfile(L, "Test.lua") || lua_pcall(L, 0, 0, 0)) {
printf("[C] ERROR: cannot run lua file: %s", lua_tostring(L, -1));
exit(3);
}
return 0;
}
then run the command:
swig -c++ -lua say.i
I compile the auto-genearated file example_wrap.cxx and other cpp file and link successfully.
What I want to do in Test.lua is to inherit from C++ Say class in lua:
-- Test.lua
Test.Say.SaySomething = function(self)
print("Inherit from C++ in Lua")
end
my = Test.Say()
my:SaySomething() -- doesn't appear to inherit successfully in lua call
Test.CallCppFun(my) -- doesn't appear to inherit successfully in c++ call
The result of print was not appear to inherit successfully both in lua call and c++ call:
[C] now loading the SWIG wrapped library
It should not be show..
It should not be show..
I know it is support in inherit from C++ in Java:generating-java-interface-with-swig
I know there have a similar question in here, but doesn't give answer of the concrete problem I face to:implementing-and-inheriting-from-c-classes-in-lua-using-swig
Does Lua support inherit from C++ class in lua using SWIG or even just use pure lua? Please show some code example. If SWIG can't do this job, does it have some third-party-library support to do it easily?