c++ - 在 SWIG in 和 freearg 类型映射之间传递信息

标签 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;
  }

最佳答案

根据 11.15 Passing data between typemaps来自 SWIG 手册:

您只需要在 freearg 类型映射中将变量用作 needsDelete$argnum

关于c++ - 在 SWIG in 和 freearg 类型映射之间传递信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38384110/

相关文章:

c++ - 分配给 `char* x = new char[32]` 的内存过多

c++ - 奇怪的 VS2013 错误?还是程序员诱发的疾病?

c++ - 使用什么数据结构

java - JVM:如何管理 JNI 创建的堆外内存

python - 如何在 python 中使用枚举?

C++ undefined symbol 错误与 ForceFit

c++ - 为什么在创建具有关联指针的对象时使用 'new'?

python - 如何使用静态变量、自定义 getter 和 setter 扩展 SWIG 中的结构?

用于读取可变参数长度的 Python C 包装器

python - 用 swig 包装时避免 "unused typedef"警告