tolua++その3(動作)
とりあえず簡単なパッケージファイルからcppソースを出力させて動作を追ってみる。
使用するパッケージファイルsample.pkg
namespace sample { class Hoge{ void fuga(); }; }
toluaする
$ ./tolua sample.pkg
生成されたコード。"//"コメントはdoit関数内の関数呼び出しと出力の対応メモ
// preamble //////////////////////////////////////////////////////////// /* ** Lua binding: sample ** Generated automatically by tolua++-1.0.93(lua) on Sat Feb 19 10:37:51 2011. */ #ifndef __cplusplus #include "stdlib.h" #endif #include "string.h" #include "tolua++.h" /* Exported function */ TOLUA_API int tolua_sample_open (lua_State* tolua_S); /* function to register type */ static void tolua_reg_types (lua_State* tolua_S) { tolua_usertype(tolua_S,"sample::Hoge"); } // supcode //////////////////////////////////////////////////////////// /* method: fuga of class sample::Hoge */ #ifndef TOLUA_DISABLE_tolua_sample_sample_Hoge_fuga00 static int tolua_sample_sample_Hoge_fuga00(lua_State* tolua_S) { 〜省略〜 } #endif //#ifndef TOLUA_DISABLE // register //////////////////////////////////////////////////////////// /* Open function */ TOLUA_API int tolua_sample_open (lua_State* tolua_S) { tolua_open(tolua_S); tolua_reg_types(tolua_S); tolua_module(tolua_S,NULL,0); tolua_beginmodule(tolua_S,NULL); tolua_module(tolua_S,"sample",0); tolua_beginmodule(tolua_S,"sample"); tolua_cclass(tolua_S,"Hoge","sample::Hoge","",NULL); tolua_beginmodule(tolua_S,"Hoge"); tolua_function(tolua_S,"fuga",tolua_sample_sample_Hoge_fuga00); tolua_endmodule(tolua_S); tolua_endmodule(tolua_S); tolua_endmodule(tolua_S); return 1; } #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501 TOLUA_API int luaopen_sample (lua_State* tolua_S) { return tolua_sample_open(tolua_S); }; #endif
tolua++には-Pオプションというのがあるのでこれでパッケージファイルがどう解析されたのかを表示してみる。
$ ./tolua.lua sample.pkg -P Package: sample Namespace{ name = 'sample', Class{ name = 'sample::Hoge', base = ''; lname = 'Hoge', type = 'sample::Hoge', btype = '', ctype = 'const sample::Hoge', Function{ mod = '', type = 'void', ptr = '', name = 'fuga', lname = 'fuga', const = '', cname = 'tolua_sample_sample_Hoge_fuga00', lname = 'fuga', args = { Declaration{ mod = '', type = 'void', ptr = '', name = '', dim = '', def = '', ret = '', }, } }, }, }
作られた木構造からpreamble, supcode, registerと3つの部分にわけてc++コードが出力される。このコードを組み込むアプリからはtolua_sample_open関数を呼ぶことで、
usertype"sample::Hoge"のluaへの登録と、module"sample", module"Hoge", function"fuga"のluaへの公開をしている。