Created
June 29, 2011 20:47
-
-
Save gaspard/1054915 to your computer and use it in GitHub Desktop.
wrapping C++ classes with Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ============ Create new class | |
SpecialEdit = {} | |
setmetatable(SpecialEdit, mimas.LineEdit_mt) | |
-- calling SpecialEdit() should do the following things | |
-- 1. create userdata | |
-- 2. set new table as uservale ---> self | |
-- 3. create new lua thread and install "self" on the stack | |
-- 4. set access to userdata from self ---> self.super = userdata | |
-- 5. set "SpecialEdit" as metatable for "self" ---> setmetatable(self, SpecialEdit) | |
-- 6. return "self" | |
-- This is a callback called from the C side. The call goes as follows | |
-- 1. the object's lua_thread's stack contains "self", ready to be used. | |
-- 2. call self.textChanged(self, text) | |
function SpecialEdit:textChanged(text) | |
-- just to show that self is accessed by the callback with custom values ("owner"). | |
self.owner:doSomething() | |
-- call a native method | |
-- "hide" is found in mimas.LineEdit_mt ---> LineEdit_mt.hide(self) | |
-- the userdata is found in self ---> LineEdit *c_obj = self.super // C pseudo-code | |
-- call the class method ---> c_obj->hide() | |
self:hide(nil) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment