Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support std::unique_ptr #60

Open
troopa81 opened this issue Dec 18, 2024 · 2 comments
Open

Support std::unique_ptr #60

troopa81 opened this issue Dec 18, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@troopa81
Copy link
Contributor

It's not possible to use std::unique_ptr as arguments. It works as returned value only.

Here is an example unique_ptr.zip

The problem is that even if we write the appropriate MappedType, the generated code that call the method with a std::unique_ptr argument should std::move this one

extern "C" {static PyObject *meth_Test_setObject(PyObject *, PyObject *);}
static PyObject *meth_Test_setObject(PyObject *sipSelf, PyObject *sipArgs)
{
...
    {
        std::unique_ptr<::AnObject>* a0;
        ::Test *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "BJ3", &sipSelf, sipType_Test, &sipCpp, sipType_std_unique_ptr_0100AnObject, &a0, SIP_NULLPTR))
        {
          // sipCpp->setObject(*a0); // generated code, doesn't work, we need to move
	  sipCpp->setObject(std::move(*a0));

            Py_INCREF(Py_None);
            return Py_None;
        }
    }
...
}

Could it be supported by SIP ?

@philthompson10 philthompson10 added the enhancement New feature or request label Dec 18, 2024
@nyalldawson
Copy link

Ideally the conversion from python -> C++ would also check that the object IS owned by python first, and if not then raise some kind of Python exception (RuntimeError?) stating that it's not possible to call the method with an object which isn't owned by python.

(Otherwise we'll eventually get a double free crash somewhere)

@philthompson10
Copy link
Contributor

Adding a /Movable/ (or similar - /NonCopyable/?) mapped type annotation shouldn't be too much of a problem.

I don't see a problem with ownership so long as /Transfer/ is used properly. It may be possible that mapped type conversion code doesn't currently have enough context to handle transfers of ownership properly in this case.

Can I assume that the common use case is going to be when unique_ptrs are going to be passed and returned by value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants