2009-05-26 13 views
1

SWIGは、与えられたクラスのセットのSWIGラッパーで、すべての親クラスのCストリング表現のリストを保持しますそのクラスは継承します。 (char ** base_names)。私はある関数があることを知っています。与えられた変数のデータ型の文字列表現を返す関数があります。親クラスのテーブルを文字列として返す関数もありますか?そうでない場合、この関数を書く簡単な方法はありますか?私はSWIGの内部の仕組みに精通していません。(SWIG/Lua)swig_lua_classのベース/親クラスのリストにアクセスする方法

ありがとうございます!このような

答えて

3

何か(私はLuaのとSWIGを使用しないので、テストしていません)動作するはずです:

// insert into runtime section 
// this is the C function that iterates over the base_names array 
// (I'm assuming that the array is terminated with a NULL) 
%runtime %{ 
    /* lua callable function to get the userdata's type */ 
    SWIGRUNTIME int SWIG_Lua_basenames(lua_State* L) 
    { 
     swig_lua_userdata* usr; 
     swig_lua_class* clss = NULL; 
     int i = 0; 
     if (lua_isuserdata(L,1)) 
     { 
     usr=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */ 
     if (usr && usr->type && usr->type->clientdata) { 
      // fetch the swig_lua_class struct, it contains the base_names 
      clss = (swig_lua_class*)usr->type->clientdata; 
     } 
     } 
     /* create a new table with all class base names in it 
     note that I create it even if clss is NULL, that way 
     an empty table -should- be returned 
     */   
     lua_newtable(L); 
     while(clss && clss->base_names[i]) { 
     lua_pushnumber(L, i+1); /* lua tables are 1-indexed */ 
     lua_pushstring(L, clss->base_names[i]); 
     lua_rawset(L, -3); 
     i++; 
     } 
     return 1; 
    } 
%} 
%init %{ 
    /* this goes into the user init function, register our new function with 
    Lua runtime 
    */ 
    SWIG_Lua_add_function(L,"swig_base_names",SWIG_Lua_basenames); 
%} 
+0

初期テストは、あなたのコードはチャンピオンのように動作することを示しています! ありがとうございます、それは大きな助けです! – zslayton

関連する問題