[PyQt] Any plans for std::unique/shared ptrs?

Shaheed Haque srhaque at theiet.org
Fri Sep 1 10:34:48 BST 2017


On 1 September 2017 at 01:08, Nyall Dawson <nyall.dawson at gmail.com> wrote:
> Hi list,
>
> I'm wondering if there's been any discussion yet for supporting
> unique_ptr and shared_ptrs within sip bindings, or whether anyone has
> any code available they've developed for handling these types.
>
> I'd love to start using these within a project I'm involved in (such
> as returning unique_ptrs from factory functions), but given that all
> these functions also need to be exposed to our python bindings I'm yet
> to find a clean way to handle this.
>
> Is this on the roadmap at all?

I'm not sure just how much use this answer will be to you, but...

The code here contains nascent support for boost::shared_ptr and std::auto_ptr:

https://github.com/ShaheedHaque/extra-cmake-modules/blob/shaheed_master/find-modules/module_generation/templates/std_n_boost.py

The support is nascent in the sense that it generates code which runs
through the SIP and C++ compilers, but has not actually been run yet.
(Of course, the point of the generator is that fixing any issues that
are found should be simple at that point). In a similar vein, things
like the way errors, object ownership are handled might not be right
yet.

The code may seem complex from the SIP template support point of view,
but the complexity essentially boils down to one thing: the generated
code is type-sensitive as compared to the SIP way of needing separate
templates for objects, integers, floats and the like. Some of the
complexity is also down to the limitations in what Clang exposes which
force the use of heuristics more than I'd like, but at least those are
being stress tested against a reasonably large codebase.

At any rate, here is an example of the output (for an "OBJECT"):

=========
%MappedType boost::shared_ptr<_gpgme_key>
{
// Generated for PARM_DECL on line 309
'GpgME::UserID::Signature::Signature::key' (by
std_n_boost.py:parameter_rules[1],pointer_parameter):
['_gpgme_key(OBJECT)']

%TypeHeaderCode
#include <boost/shared_ptr.hpp>
%End
%ConvertFromTypeCode
    typedef _gpgme_key CxxvalueT;
    const sipTypeDef *genvalueT = sipType__gpgme_key;

    // Convert the value from C++.
    PyObject *value = sipConvertFromType((void *)sipCpp->get(),
genvalueT, sipTransferObj);
    if (value == NULL) {
        PyErr_Format(PyExc_TypeError, "cannot convert value");
        return 0;
    }
    return value;
%End
%ConvertToTypeCode
    const char *cxxvalueS = "_gpgme_key";
    typedef _gpgme_key CxxvalueT;
    const sipTypeDef *genvalueT = sipType__gpgme_key;
    PyObject *value;

    // Convert the value to C++.
    value = sipPy;
        int valueState;
        CxxvalueT *cxxvalue = reinterpret_cast<CxxvalueT
*>(sipForceConvertToType(value, genvalueT, sipTransferObj,
SIP_NOT_NONE, &valueState, sipIsErr));

    if (*sipIsErr) {
        if (cxxvalue == NULL) {
            PyErr_Format(PyExc_TypeError, "value has type '%s' but
'%s' is expected",
                         Py_TYPE(value)->tp_name, cxxvalueS);        }
        sipReleaseType((void *)cxxvalue, genvalueT, valueState);
        return 0;
    }
        sipReleaseType((void *)cxxvalue, genvalueT, valueState);
    *sipCppPtr = new boost::shared_ptr<CxxvalueT>(cxxvalue);
    return sipGetState(sipTransferObj);
%End
};
=========

(Any corrections to the above are welcome!).

Thanks, Shaheed

> Cheers,
> Nyall
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list