SWIG_NewPointerObj 的最后一个参数是什么意思? [英] what does the last argument to SWIG_NewPointerObj mean?

查看:41
本文介绍了SWIG_NewPointerObj 的最后一个参数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 SWIG 访问 C++ 库的兼容性库.我会发现能够在该层内创建一个 SWIG 包装的 Python 对象(而不是接受 C++ 对象作为参数或返回一个)很有用.IE.我想要指向 SWIG 包装的 C++ 对象的 PyObject*.

I have a compatibility library that uses SWIG to access a C++ library. I would find it useful to be able to create a SWIG-wrapped Python object inside this layer (as opposed to accepting the C++ object as an argument or returning one). I.e. I want the PyObject* that points to the SWIG-wrapped C++ object.

我发现 SWIG_NewPointerObj 函数正是这样做的.SWIG 生成的 xx_wrap.cpp 文件使用此函数,但它也在 swig -python -external-runtime swigpyrun.h

I discovered that the SWIG_NewPointerObj function does exactly this. The SWIG-generated xx_wrap.cpp file uses this function, but it's also made available in the header emitted by swig -python -external-runtime swigpyrun.h

但是,我找不到任何对该函数的最后一个参数的引用.似乎它指定了对象的所有权,但没有说明每个选项的含义(甚至它们都是什么)的文档.以下似乎是可接受的值:

HOWEVER, I cannot find any reference to what the last argument to this function is. It appears that it specifies the ownership of the object, but there is no documentation that says what each of the options mean (or even what they all are). It appears that the following are acceptable values:

  • 0
  • SWIG_POINTER_OWN
  • SWIG_POINTER_NOSHADOW
  • SWIG_POINTER_NEW = OWN + NOSHADOW
  • SWIG_POINTER_DISOWN(我不确定 SWIG_NewPointerObj 是否接受)
  • SWIG_POINTER_IMPLICIT_CONV(我不确定 SWIG_NewPointerObj 是否接受)

我想创建一个仅在我的包装层中使用的对象.我想用我自己的指向 C++ 对象的指针来创建它(这样我就可以更改 C++ 对象的值并将其反映在 Python 对象中.我需要它以便可以将它传递给 Python 回调函数.我想在程序的整个生命周期中保持这个实例,这样我就不会浪费时间为每个回调创建/销毁相同的对象.哪个选项是合适的,我该怎么做Py_INCREF?

I want to create an object that is used only in my wrapping layer. I want to create it out of my own pointer to the C++ object (so I can change the C++ object's value and have it be reflected in the Python object. I need it so it can be passed to a Python callback function. I want to keep this one instance throughout the life of the program so that I don't waste time creating/destroying identical objects for each callback. Which option is appropriate, and what do I Py_INCREF?

推荐答案

当您使用 SWIG_NewPointerObj 创建新的指针对象时,您可以传递以下标志:

When you create new pointer objects with SWIG_NewPointerObj, you may pass the following flags:

SWIG_POINTER_OWN
SWIG_POINTER_NOSHADOW

如果设置了 SWIG_POINTER_OWN,则在 Python 指针最终确定时将调用底层 C++ 类的析构函数.默认情况下,不会调用析构函数.请参阅内存管理

If SWIG_POINTER_OWN is set, the destructor of the underlying C++ class will be called when the Python pointer is finalized. By default, the destructor will not be called. See Memory Management

对于您的用例,您根本不需要设置任何标志.

For your use case, you don't need to set any flags at all.

从我在源代码中看到的,如果设置了 SWIG_POINTER_NOSHADOW,则返回一个基本的包装指针.您将无法在 Python 中访问成员变量.你所拥有的只是一个不透明的指针.

From what I can see in the sources, if SWIG_POINTER_NOSHADOW is set, then a basic wrapped pointer is returned. You will not be able to access member variables in Python. All you'll have is an opaque pointer.

参考:/usr/share/swig/2.0.7/python/pyrun.swg

Reference: /usr/share/swig/2.0.7/python/pyrun.swg

这篇关于SWIG_NewPointerObj 的最后一个参数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆