[PyQt] sip: how to disable copy constructor, assignment operator for mapped types

Phil Thompson phil at riverbankcomputing.com
Tue May 11 09:53:20 BST 2010


On Mon, 10 May 2010 15:53:50 -0700, Nathan Cournia <cournia at gmail.com>
wrote:
> Hi,
> 
> I'm currently trying to use %MappedType to allow my QT code to
> interact with C++ objects that have been wrapped using SWIG.  My .sip
> file currently looks like:
> 
> %MappedType Node /NoDefaultCtors/
> {
> 
> %TypeHeaderCode
> #include <foo/Node.h>
> using namespace foo;
> %End
> 
> %ConvertFromTypeCode
>      return sipCpp->getPyObject();
> %End
> 
> %ConvertToTypeCode
>     if (!sipIsErr) {
>         return foo::Node::isNode(sipPy);
>     }
> 
>     *sipCppPtr = foo::Node::asNode(sipPy);
> 
>     if (!*sipCppPtr) {
>         *sipIsErr = 1;
>         return 0;
>     } else {
>         return SIP_TEMPORARY;
>     }
> %End
> 
> };
> 
> Can %MappedType be used to tell SIP about Python objects that have
> been wrapped with SWIG?  Is this a good idea?
> 
> The following code is generated:
> 
> extern "C" {static void assign_Node(void *, SIP_SSIZE_T, const void *);}
> static void assign_Node(void *sipDst, SIP_SSIZE_T sipDstIdx, const void
> *sipSrc)
> {
>     reinterpret_cast<Node *>(sipDst)[sipDstIdx] =
> *reinterpret_cast<const Node *>(sipSrc);
> }
> 
> 
> extern "C" {static void *array_Node(SIP_SSIZE_T);}
> static void *array_Node(SIP_SSIZE_T sipNrElem)
> {
>     return new Node[sipNrElem];
> }
> 
> 
> extern "C" {static void *copy_Node(const void *, SIP_SSIZE_T);}
> static void *copy_Node(const void *sipSrc, SIP_SSIZE_T sipSrcIdx)
> {
>     return new Node(reinterpret_cast<const Node *>(sipSrc)[sipSrcIdx]);
> }
> 
> 
> /* Call the mapped type's destructor. */
> extern "C" {static void release_Node(void *, int);}
> static void release_Node(void *ptr, int)
> {
>     Py_BEGIN_ALLOW_THREADS
>     delete reinterpret_cast<Node *>(ptr);
>     Py_END_ALLOW_THREADS
> }
> #endif
> 
> 
> This poses several problems.  First, foo::Node's assignment and copy
> constructors have been hidden/disabled.  This means assign_Node and
> copy_Node do not work.  Is there a way to disable the generation of
> assign_Node and copy_Node?
> 
> Second, I do not want SIP to call delete on the Node in release_Node.
> foo::Node is internally reference counted, The SWIG bindings are aware
> of this and should decrement foo::Node's reference count when the
> PyObject is released.  Can I disabled the generation of release_Node?

/NoDefaultCtors/ is a class annotation, not a mapped type annotation.
Replace it with /NoRelease/ and all those functions will be suppressed.

Phil


More information about the PyQt mailing list