2009-05-26 34 views
1

我注意到,在給定的一組類的SWIG包裝中,SWIG保留了所有父類的C字符串表示該類繼承的。 (char ** base_names)。我知道有一個功能(SWIG/Lua)如何訪問swig_lua_class中的基類/父類的列表

swig_type(some_variable) 

這將返回給定變量的數據類型的字符串表示形式。是否還有一個函數會返回一個父類的表作爲字符串?如果不是,有沒有簡單的方法來寫這個函數?我完全不瞭解SWIG的內部運作。

謝謝!

回答

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 2009-05-26 17:23:31