#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 %>); lua_pushfstring(L, "<%= @class.id_name %>: %p", *userdata); 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 %> void luaopen_<%= @class.lib_name %>(lua_State *L) { // Create the metatable which will contain all the member methods luaL_newmetatable(L, <%= @class.id_name.inspect %>); // "dub.Matrix" // 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 table like "dub" 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 %> }