#include "<%= @class.header %>" #include "lua_cpp_helper.h" <% if @class.prefix %> using namespace <%= @class.prefix %>; <% end %> /* ============================ Constructors ====================== */ <%= @class.constructor %> /* ============================ Destructor ====================== */ static int <%= @class.destructor_name %>(lua_State *L) { <%= @class.name %> **userdata = (<%= @class.name %>**)luaL_checkudata(L, 1, <%= @class.id_name.inspect %>); if (*userdata) delete *userdata; *userdata = NULL; return 0; } /* ============================ tostring ====================== */ static int <%= @class.tostring_name %>(lua_State *L) { <%= @class.name %> **userdata = (<%= @class.name %>**)luaL_checkudata(L, 1, <%= @class.id_name.inspect %>); <% if @class.string_format %> lua_pushfstring(L, "<<%= @class.id_name %>: %p <%= @class.string_format %>>", *userdata, <%= @class.string_args %>); <% else %> lua_pushfstring(L, "<<%= @class.id_name %>: %p>", *userdata); <% end %> return 1; } /* ============================ Member Methods ====================== */ <% if @class.members; @class.members.each do |function| %> <%= function %> <% end; end %> /* ============================ Lua Registration ====================== */ static const struct luaL_Reg <%= @class.name %>_member_methods[] = { <%= indent(method_registration, 2) %>, {NULL, NULL}, }; static const struct luaL_Reg <%= @class.name %>_namespace_methods[] = { <%= indent(namespace_methods_registration, 2) %>, {NULL, NULL}, }; <% if @class.has_constants? %> static const struct lua_constants_Reg <%= @class.name %>_namespace_constants[] = { <%= indent(constants_registration, 2) %>, {NULL, NULL}, }; <% end %> #ifdef DUB_LUA_NO_OPEN int luaload_<%= @class.lib_name %>(lua_State *L) { #else extern "C" int luaopen_<%= @class.lib_name %>(lua_State *L) { #endif // Create the metatable which will contain all the member methods luaL_newmetatable(L, <%= @class.id_name.inspect %>); // metatable.__index = metatable (find methods in the table itself) lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); // register member methods luaL_register(L, NULL, <%= @class.name %>_member_methods); // register class methods in a global namespace table luaL_register(L, <%= @class.prefix.inspect %>, <%= @class.name %>_namespace_methods); <% if @class.has_constants? %> // register class enums register_constants(L, <%= (@class.id_name + '_const').inspect %>, <%= @class.name %>_namespace_constants); <% end %> }