Re: Factories and other functions returning pointers
Burlen Loring <[email protected]> Fri, 3 Dec 2021 11:24:07 -0800
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CABjnq17WqmK=FMnm5PqTwjoUyVm4xp+ozyAfXSMVihksnRS9MA@mail.gmail.com> |
Hi Kris, Could your factory function simply return a std::shared_ptr? I pasted a code example showing specifics in the github issue you raised. I tested it with your example code and all worked well. It has a couple of benefits since you'd no longer need to use the %factory and %newobject directives and using std::make_shared is more efficient than other methods since allocation of the smart pointer control block can occur with the allocation of the object. Burlen On Fri, Dec 3, 2021 at 1:20 AM <[email protected]> wrote: > Hi all > > > > Seems I mailed to soon. This morning my strategy does seem to work if I > add 2 factory declarations: > > > > %factory(A *, A1, A2); > > %factory(A const*, A1, A2); > > > > I also mentioned worries about ownership, but I now think that this is > independent, and I just have to use %newobject for the appropriate cases. > > > > So I think I have resolved my problems for bare pointers. Still struggling > with shared_ptr, but I'll document that in the GitHub issue %factory > fails with %shared_ptr · Issue #2110 · swig/swig (github.com) > <https://github.com/swig/swig/issues/2110> > > > > Kris > > > > *From:* Kris Thielemans> > *Sent:* 02 December 2021 22:21 > > > > Hi all > > > > > > > > I'm trying to make our Python interface to STIR friendlier w.r.t. derived > classes. Many of our C++ functions return a bare pointer to the base class. > > > > > > > > class A; > > > > class A1: public A; > > > > class A2: public A; > > > > > > > > class B > > > > { > > > > A const * some_function() const; > > > > }; > > > > > > > > It'd be great if in Python, the result of B::some_function() would be > actually of the appropriate type (e.g. A1), as there is no way to downcast > in Python. > > > > > > > > I looked in the use of %factory. This is intended for "factory" functions > that create a new object. It then occurred to me that nothing in the > %factory mechanism seems to require that the result is a new object. > Indeed, using %factory for some_function() seems to work perfectly fine > (although I'm wondering about ownership). > > > > > > > > %factory(A const* some_function, A1, A2); > > > > > > > > This generates > > > > > > > > typemap(out) A const* some_function{ … } > > > > > > > > which handles the downcasting. Great! So then I hoped to just do this for > all functions that return an A* (or A const*). > > > > > > > > %factory(A const*, A1, A2); > > > > > > > > Generating > > > > > > > > typemap(out) A const* { … } > > > > > > > > However, this typemap doesn't match the signature of the > B::some_function(). Am I doing something stupid? (This is my first foray > into typemaps, so the answer is probably "yes"). > > > > > > > > Any help appreciated. > > > > > > > > Thanks! > > > > > > > > Kris > > > > > > > > PS: in fact, most of our functions return a shared_ptr<const A> or even > unique_ptr, but I have to work around those as SWIG doesn't handle > unique_ptr yet unfortunately. See %factory fails with %shared_ptr · Issue > #2110 · swig/swig (github.com) for more information on my struggles with > shared_ptr. > > > > > > > > > _______________________________________________ > Swig-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/swig-user > _______________________________________________ Swig-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/swig-user