Re: adding fields to lua class from C++
Tom McCubbin <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
It seems you want your luabind object to behave as if a lua table. In lua when fields are accessed that do not exist, the table metatable object is indexed to look for an ' __index' function in case of data accesses, or '__newindex' in case of data assignment. Your best bet (if luabind doesn't hijack these methods already) is to add lua or c++ code to these metatable entries from your objects constructor. Then when someone attempts to index into your object, that code will be called and the appropriate action can be taken. If you don't already own the Programming in Lua and the Reference Manual, I'd heartily recommend them. The previous version of Programming in Lua is free online. I'd take a look at the C-Api chapters, particularly userdata, and metatables. I hope that is enough to get you going. On Sat, Apr 11, 2009 at 7:40 AM, Domino <[email protected]> wrote: > Hi, > > > > Thanks, but unfortunately this didn’t answer my question. I did not ask how > to do this from the design point of view but how to ensure that the code: > > > > if (m_Self[field_name]) > > > > won’t fail. In other words how to force lua table to create entry > “field_name” from the C++ side? From the design point of view I’ve chosen > that it’s C++ duty as I don’t want to force script creator to first create > fields and then add them to dynamic fields list. It’s simpler to have only > dynamic fields and force lua to put the field into table from C++ side. > > > > Thanks > > > > *From:* yaohua xiao [mailto:[email protected]] > *Sent:* Friday, April 10, 2009 2:46 AM > *To:* [email protected] > *Subj**ect:* Re: [luabind] adding fields to lua class from C++ > > > > > > I think it's a design problem. When you decided what c++ or lua's duty in > your project, the problem is clear. > > In my opinion, i will use a std::map<string,string> to store the key-field > pairs > and exports a get/set method to lua. > > in your case: > > > function myClass:populateDynamicFields( > > ) > > addDynamicField('field1', int) > > addDynamicField('field2', string) > > end > > It seems that the fields will be different types, so I suggest to use the > string > type to store field value, and translate it when you use it. > > > > > 2009/4/9 <[email protected]> > > > > Message: 5 > Date: Mon, 6 Apr 2009 20:47:37 +0200 > From: "Domino" <[email protected]> > Subject: [luabind] adding fields to lua class from C++ > To: <[email protected]> > Message-ID: <000001c9b6e8$27f70da0$77e528e0$@fm> > Content-Type: text/plain; charset="us-ascii" > > Hi all, > > > > I'm working on pseudo-dynamic fields for the lua classes. What I want to > achieve is some sort of mapping of object fields available from C++. So my > script class will populate the fields map like: > > > > function myClass:populateDynamicFields() > > addDynamicField('field1', int) > > addDynamicField('field2', string) > > end > > > > then from GUI I want to display this list of dynamic fields and be able to > set their values via get/setValueInt (eventually I would like to add fields > from GUI as well). These methods will basically look like below: > > > > void myClassInterface::setValueInt(string name, int value) > > { > > // let's say that I have luabind::object that is the script class > representation in luabind and it's called m_Self > > if (m_Self[name]) > > { > > m_Self[name] = value; > > } > > } > > > > What I don't know is how to ensure that the m_Self[name] exists? Or how to > force its creation from C++ side? Like: > > > > // . > > if (!m_Self[name]) > > // do something to create field > > > > Is there some way to do this? Or maybe there is some more generic way to do > what I'm trying here? > > > > Thanks a lot. > > > > > ---------------------------------------------------------------------- > Przygotuj znajomym wirtualny Smigus Dyngus! ;) > http://link.interia.pl/f211a > > ------------------------------------------------------------------------------ > This SF.net email is sponsored by: > High Quality Requirements in a Collaborative Environment. > Download a free trial of Rational Requirements Composer Now! > http://p.sf.net/sfu/www-ibm-com > _______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > > ------------------------------------------------------------------------------ This SF.net email is sponsored by: High Quality Requirements in a Collaborative Environment. Download a free trial of Rational Requirements Composer Now! http://p.sf.net/sfu/www-ibm-com _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user