在SWIG和freearg类型映射之间传递信息

时间:2016-07-14 20:58:13

标签: c++ swig

我有一个针对Python的类型映射,它接受已经包装的指针对象或者另外允许传递Python序列。在包装指针的情况下,我不想删除内存,因为SWIG拥有它。但是,在处理序列时,我正在分配需要删除的临时对象。所以我在我的'in'字体图中添加了一个标志来标记我是否分配了指针目标。如何在相应的“freearg”类型映射中访问此标志?

字体图如下所示:

%typemap(in) name* (void* argp = 0, int res = 0, bool needsDelete = false) {
  res = SWIG_ConvertPtr($input, &argp, $descriptor, $disown | 0);
  if (SWIG_IsOK(res)) {
    $1 = ($ltype)(argp); // already a wrapped pointer, accept
  } else {
    if (!PySequence_Check($input)) {
      SWIG_exception(SWIG_ArgError(res), "Expecting a sequence.");
    } else if (PyObject_Length($input) != size) {
      SWIG_exception(SWIG_ArgError(res), "Expecting a sequence of length " #size);
    } else {
      needsDelete = true;
      $1 = new name;
      for (int i = 0; i < size; ++i) {
        PyObject* o = PySequence_GetItem($input, i);
        (*$1)[i] = swig::as<type>(o);
        Py_DECREF(o);
      }
    }
  }
}

%typemap(freearg) name* {
  if ($1 /* && needsDelete */) delete $1;
}

这会导致生成的代码如下所示:

  {
    res2 = SWIG_ConvertPtr(obj1, &argp2, SWIGTYPE_p_MyName_t, 0 | 0);
    if (SWIG_IsOK(res2)) {
      arg2 = (MyName *)(argp2); // already a wrapper pointer, accept
    } else {
      if (!PySequence_Check(obj1)) {
        SWIG_exception(SWIG_ArgError(res2), "Expecting a sequence.");
      } else if (PyObject_Length(obj1) != 3) {
        SWIG_exception(SWIG_ArgError(res2), "Expecting a sequence of length ""3");
      } else {
        needsDelete2 = true;
        arg2 = new MyName;
        for (int i = 0; i < 3; ++i) {
          PyObject* o = PySequence_GetItem(obj1, i);
          (*arg2)[i] = swig::as<double>(o);
          Py_DECREF(o);
        }
      }
    }
  }
  if (arg1) (arg1)->someMember = *arg2;
  resultobj = SWIG_Py_Void();
  {
    if (arg2 /* && needsDelete */) delete arg2;
  }

1 个答案:

答案 0 :(得分:2)

根据SWIG手册中的11.15 Passing data between typemaps

您只需要在freearg typemap中将变量用作needsDelete$argnum