6/04/2013

lua c/c++ binding. LUNA


LUNA 

Author 

/*
 * Lenny Palozzi - lenny.palozzi@home.com
 */


Code 


/*
 * Lenny Palozzi - lenny.palozzi@home.com
 */
extern "C" {
#include
}

template
class Luna
{
 public:
   /* member function map */
   struct RegType { 
      const char* name; 
      const int(T::*mfunc)(lua_State*);
   };      
   /* register class T */
   static void Register(lua_State* L) {
      lua_pushcfunction(L, &Luna::constructor);
      lua_setglobal(L, T::className);
      
      if (otag == 0) {
otag = lua_newtag(L);
lua_pushcfunction(L, &Luna::gc_obj);
lua_settagmethod(L, otag, "gc"); /* tm to release objects */
      }
   }
 private:
   static int otag; /* object tag */
   
   /* member function dispatcher */
   static int thunk(lua_State* L) {
      /* stack = closure(-1), [args...], 'self' table(1) */
      int i = static_cast(lua_tonumber(L,-1));
      lua_pushnumber(L, 0); /* userdata object at index 0 */
      lua_gettable(L, 1);
      T* obj = static_cast(lua_touserdata(L,-1));
      lua_pop(L, 2); /* pop closure value and obj */
      return (obj->*(T::Register[i].mfunc))(L);
   }
   
   /* constructs T objects */
   static int constructor(lua_State* L) {
      T* obj= new T(L); /* new T */
      /* user is expected to remove any values from stack */
      
      lua_newtable(L); /* new table object */
      lua_pushnumber(L, 0); /* userdata obj at index 0 */
      lua_pushusertag(L, obj, otag); /* have gc call tm */
      lua_settable(L, -3);
      
      /* register the member functions */
      for (int i=0; T::Register[i].name; i++) {
lua_pushstring(L, T::Register[i].name);
lua_pushnumber(L, i);
lua_pushcclosure(L, &Luna::thunk, 1);
lua_settable(L, -3);
      }
      return 1; /* return the table object */
   }

   /* releases objects */
   static int gc_obj(lua_State* L) {
      T* obj = static_cast(lua_touserdata(L, -1));
      delete obj;
      return 0;
   }
 protected: 
   Luna(); /* hide default constructor */
};
template
int Luna::otag = 0;



LICENSE



Copyright (c) 2005 Leonardo Palozzi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.




댓글 없음: