Quantcast

Wrapping a custom smart pointer

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Wrapping a custom smart pointer

Jeremy Cowles
Hi swig-users,

I'm new to SWIG and want to wrap a fairly complex custom smart pointer (to C# and, perhaps later, to Go), so I started reading the typemap docs and looking at the standard smart_ptr library source and now I have some questions:

1) What triggers the auto-magic smart pointer internals? From other threads, I realize that the logic is baked into the SWIG executable and from the docs I understand that SWIG is attempting to detect smart pointers, but curious exactly how it's being triggered.

2) Perhaps related, what is this line doing in lib/shared_ptr.h?

%feature("smartptr", noblock=1) TYPE { SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > }

3) The smart pointer I need to wrap doesn't match the <NAMESPACE>::<SUBNAMESPACE>::shared_ptr naming pattern and has slightly different semantics and API, so I think I'm going to write something very similar. The pointers typically take this form: TfRefPtr<Foo> which gets typedef'd to FooRefPtr (similarly FooConstRefPtr) as well as TfWeakPtr<Foo> / FooWeakPtr. I would love to avoid re-writing that logic, any suggestions?

4) I would like TfWeakPtr to be wrapped out as a weak reference in C#. I'm not sure if it should literally be NET's WeakReference<T>, but I want to correctly surface the weak-pointer-ness. Any guidance there?

5) The C++ class hierarchy is kind of... intricate (involving CRTP and several layers of base classes/templates) -- how much of that do I need to expose to SWIG? Can I get away with making a proxy class that has some limited set of public members and no base classes?


And most importantly: thanks for making and maintaining such an awesome tool! :D

--
Jeremy


------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapping a custom smart pointer

William S Fulton
On 14 September 2016 at 17:22, Jeremy Cowles <[hidden email]> wrote:

> Hi swig-users,
>
> I'm new to SWIG and want to wrap a fairly complex custom smart pointer (to
> C# and, perhaps later, to Go), so I started reading the typemap docs and
> looking at the standard smart_ptr library source and now I have some
> questions:
>
> 1) What triggers the auto-magic smart pointer internals? From other threads,
> I realize that the logic is baked into the SWIG executable and from the docs
> I understand that SWIG is attempting to detect smart pointers, but curious
> exactly how it's being triggered.
>
%feature("smartptr") is the key feature.

> 2) Perhaps related, what is this line doing in lib/shared_ptr.h?
>
> %feature("smartptr", noblock=1) TYPE {
> SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > }
>
It basically marks the TYPE as having a smartptr to it (the
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE >) type. This is then
used to support smart pointers and inheritance. Say D derives from
base B, then shared_ptr<D> is marked with a fake inheritance from
shared_ptr<B> in the type system if the "smartptr" feature is used on
both B and D. This is to emulate the conversion of shared_ptr<D> to
shared_ptr<B> in the target language.

> 3) The smart pointer I need to wrap doesn't match the
> <NAMESPACE>::<SUBNAMESPACE>::shared_ptr naming pattern and has slightly
> different semantics and API, so I think I'm going to write something very
> similar. The pointers typically take this form: TfRefPtr<Foo> which gets
> typedef'd to FooRefPtr (similarly FooConstRefPtr) as well as TfWeakPtr<Foo>
> / FooWeakPtr. I would love to avoid re-writing that logic, any suggestions?
>
There are so many subtle differences between different smart pointers
that it would be hard to support all the variations in one place.
Copying the shared_ptr typemaps and modifying for your own would be
the way forward.

> 4) I would like TfWeakPtr to be wrapped out as a weak reference in C#. I'm
> not sure if it should literally be NET's WeakReference<T>, but I want to
> correctly surface the weak-pointer-ness. Any guidance there?
>
> 5) The C++ class hierarchy is kind of... intricate (involving CRTP and
> several layers of base classes/templates) -- how much of that do I need to
> expose to SWIG? Can I get away with making a proxy class that has some
> limited set of public members and no base classes?
>
Generally I recommend exposing as little as you can get away with.
Providing a carefully chosen set of C++ classes for exposure is key to
a successful API. Definitely don't expose all your headers and as you
have a smart pointer, I'd recommend using smart pointers only and no
raw pointers or c++ references to solve a pile of memory problems with
target languages that use garbage collectors.

William

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...