[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: table and userdata pattern (attributes on userdata)
- From: Gaspard Bucher <gaspard@...>
- Date: Tue, 5 Apr 2011 11:21:44 +0200
Hi list !
I have C++ classes that I would like to extend in Lua (for example a FileObserver class that should store the observed files in Lua). Until now, I have been using composition:
==== SOLUTION A
instance = { super = mimas.FileObserver() }
function instance.super.pathChanged(path)
instance:pathChanged(path)
end
--- But this is not great because I have to define C++ to Lua accessors like this (since "self" is not the same).
==== SOLUTION B
instance = mimas.FileObserver()
-- set any attribute (I could manage this with __newindex, __index in C++ but it seems slow).
instance.watched_files = {}
function instance:pathChanged(path)
-- self == instance
-- I can access watched_files
end
=====
The cleanest solution is "B", but there is a pretty big overhead on each member access (Lua --> mimas.FileObserver meta table --> C++ binding --> __index --> userdata env table). Is there another solution ?
Thanks for any hints,
Gaspard