MappedType for unique_ptr

Phil Thompson phil at riverbankcomputing.com
Wed Nov 20 10:40:52 GMT 2024


On 20/11/2024 04:57, Nyall Dawson wrote:
> Hi list,
> 
> I'm wondering if any one has had any luck in creating a MappedType to
> handle unique_ptr objects.
> 
> I've been experimenting with the following implementation, which seems 
> to
> work well for converting from unique_ptrs to Python owned objects. But 
> I
> can't find any way to implement the ConvertToTypeCode so that I can 
> wrap
> functions which have unique_ptr arguments.
> 
> I'm really hoping to modernize some API and move away from raw 
> pointers,
> but this is a stumbling block...
> 
> template <TYPE>
> %MappedType std::unique_ptr< TYPE >
> /NoRelease,AllowNone,TypeHint="Optional[TYPE]",TypeHintValue="TYPE"/
> {
> %TypeHeaderCode
> #include <memory>
> %End
> 
> %ConvertFromTypeCode
>   const sipTypeDef *sip_type = sipFindType("TYPE*");
>   return sipConvertFromNewType(sipCpp->release(), sip_type, NULL);
> %End

Obviously I don't know the detail of your use-case but you are 
hard-coding a particular ownership protocol by using 
sipConvertFromNewType() and release() rather than sipConvertFromType() 
and get().

> %ConvertToTypeCode
>   // only one way for now...
>   return 0;
> %End
> };

What's the problem with using the unique_ptr<> ctor?

Phil


More information about the PyQt mailing list