(SWIG / LUA) SWIG_LUA_CLASS에서 기본 / 부모 클래스 목록에 액세스하는 방법

StackOverflow https://stackoverflow.com/questions/911632

문제

주어진 클래스 세트의 생성 된 SWIG 래퍼에서 SWIG는 해당 클래스가 상속되는 모든 부모 클래스의 C- 스트링 표현 목록을 유지합니다. (char ** base_names). 기능이 있다는 것을 알고 있습니다

swig_type(some_variable)

주어진 변수의 데이터 유형의 문자열 표현을 반환합니다. 부모 클래스의 테이블을 문자열로 반환하는 함수도 있습니까? 그렇지 않다면이 기능을 작성하는 쉬운 방법이 있습니까? 나는 SWIG의 내부 작업에 전혀 익숙하지 않습니다.

감사!

도움이 되었습니까?

해결책

이와 같은 것이 작동해야합니다 (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);
%}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top