lua-users home
lua-l archive

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


> 
> I guess my question really comes down to this: if the app needs to
> create arbitrary entities based on a configuration file (and thus
> doesn't know the names of the entities to query), is it better to just
> iterate over all tables and inspect them and act appropriately, or is it
> more common/sane to have the script explicitly create entities by
> calling into the app?  I don't even need to keep identifiers around,
> it's purely a way to make the app data driven.
> 


Well, you can still use the constructor syntax, to put things in
appropriate tables.  For example:

  function Window(t)
    tinsert( WindowTable, t)
  end

so that all your windows would already be in the same table.


I haven't had the time to go into the metatables of 5.0 yet, but in
the old tag method mechanism (4.0) you could the window function could
just add a "window" tag to the table, and set the setglobal method
associated to this tag to insert the new window in WindowTable, this
way you'd have access to the name... :

  WindowTag = newtag()

  function Window(t)
    return settag(t, WindowTag)
  end

  function setglobal_Window(varname, value)
    WindowTable[varname] = value
  end

  settagmethod( WindowTag, "setglobal", setglobal_Window)

Later,  when you do

  mynewwindow = Window{bla, bla, bla}

you will have an entry "mynewwindow" in WindowTable with {bla, bla, bla}.


If your problem is to let the lua interpreter to know what classes to
recognize, you cand create (from C/C++) a table with the names you
know how to deal, and then have a function (in lua) to create all
these functions and bindings automatically.

In our application here, we dynamically create bindings for several
c++ classes.  We let a factory mechanism do it, so we can actually
load a dynamic module, and it will -- dynamically -- add new bindings.
(well,  it is not so simple as I made it sound ;-)

Siome

-- 
----------------------------
Siome Klein Goldenstein
siome@cis.upenn.edu
http://www.cis.upenn.edu/~siome