lua-users home
lua-l archive

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


Hi List,
Here's a fairly simple idea that seems fraught with gotchas.  I have a
GUI module that binds some C++ classes and presents them as a Lua
module.  It's quite spare and I want to keep it that way.  I've
designed the bindings to allow for heavy augmentation within Lua
scripts and I now want to design a module that codifies some of these
augmentations.  To make things concrete, here's the module layout:


require 'glv' -- widget library (contains a View widget)

I now want to make a 'glvutils' module that augments glv.View by
extending one of it's methods.  If I call View:add(widget) the widget
will become a child of View.  I'd like to have the function View:add
perform additional tasks.  Currently, I'm doing:

------------------------------- glvutils module code:

local glv = require "glv"
local View = glv.View

-- initializer functions that programmatically modify the glv module
local
function nameadd(f)
	return
	function(self, v)
		local name = v.name
		if(name) then
			print("nameadd", name)
			local children = self.children or {}
			children[name] = v
			self.children = children
		end
		f(self, v)
	end
end

local
function adapt_glv(m)
	-- widgets to modify
	local parent_widgets = {
		View = View,
	}
	
	for name, w in pairs(parent_widgets) do
		-- add new functionality to widget:add function
		w.add = nameadd(w.add)	
		
		-- put the modified widgets in the module table
		m[name] = w
	end	
end

module('glvutils', adapt_glv)
--------------------------------------------------------------------

An issue I have with the above is that if I require "glv" _after_ I
require "glvutils", the View:add function will be the one created by
the glvutils module, not the original glv module one.  Is there a way
around this?  Do I have to duplicate the table?  This could pose some
problems if View itself has a metatable or an environment.  Thanks for
any ideas about this.

wes