[Swig-user] how to handle std::auto_ptr?

189 views
Skip to first unread message

Kyunghoon Lee

unread,
Jul 9, 2011, 10:30:10 PM7/9/11
to Swig...@lists.sourceforge.net
Dear swig-users,

I'm trying to wrap a C++ library that uses std::auto_ptr.  For instance, I got the following error:

g++ -c -I/usr/include/python2.6  -I../../rb_online -I../../libMesh_linalg rb_online_wrap.cxx
rb_online_wrap.cxx: In function ‘PyObject* _wrap_RBReferenceComponent_build_rb_evaluation(PyObject*, PyObject*)’:
rb_online_wrap.cxx:15615: error: no matching function for call to ‘std::auto_ptr<RBOnline::RBEvaluation>::auto_ptr(std::auto_ptr<RBOnline::RBEvaluation>)’

I guess maybe I need to refer to "C++ Smart Pointers" in the swig document (http://www.swig.org/Doc2.0/SWIGDocumentation.html#Library_std_shared_ptr), but I'm not sure.  One thing that worries me is that std::auto_ptr will be depreciated; thus, I'm thinking of asking the person who wrote the C++ library not to use std::auto_ptr.

I wonder if there is a way I can handle std::auto_ptr in SWIG or it's better to replace std::auto_ptr with something else and try to wrap the new C++ library.  Looking forward to your advice. 

Regards,
K. Lee.

Russell E. Owen

unread,
Jul 11, 2011, 6:37:11 PM7/11/11
to swig...@lists.sourceforge.net
In article
<CA+ZVpt8eW1GZ2BDWoeLxzvcn...@mail.gmail.com>,
Kyunghoon Lee <aeronova...@gmail.com> wrote:

> ---------------------------------------------------------------------
> ------------------------------------------------------------------------------
> All of the data generated in your IT infrastructure is seriously valuable.
> Why? It contains a definitive record of application performance, security
> threats, fraudulent activity, and more. Splunk takes this data and makes
> sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-d2d-c2---------------------------------------------
> ------------------------
> _______________________________________________
> Swig-user mailing list
> Swig...@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/swig-user

I suggest you avoid std::auto_ptr if possible. It is deprecated because
copying transfers ownership, which is rarely useful.

If you have a really recent C++ compiler you can use std::shared_ptr
(allows multiple owners) or std::unique_ptr (cannot be copied). Older
compilers will support tr1::shared_ptr or boost::shared_ptr, but I don't
know of a good non-copyable smart pointer.

-- Russell


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Swig-user mailing list
Swig...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Kyunghoon Lee

unread,
Jul 12, 2011, 1:21:05 AM7/12/11
to Russell E. Owen, swig...@lists.sourceforge.net
Hi Russell,

Thank you for your advice.  I'll ask my colleague to avoid auto_ptr.  However, I wonder whether SWIG still can deal with auto_ptr or not.  I hope someone could answer it.

Regards,
K. Lee.
 

William S Fulton

unread,
Aug 4, 2011, 2:56:49 PM8/4/11
to Kyunghoon Lee, Russell E. Owen, swig...@lists.sourceforge.net
On 12/07/11 06:21, Kyunghoon Lee wrote:
>
>
> On Tue, Jul 12, 2011 at 6:37 AM, Russell E. Owen <ro...@uw.edu
> <mailto:ro...@uw.edu>> wrote:
>
> In article
> <CA+ZVpt8eW1GZ2BDWoeLxzvcn...@mail.gmail.com
> <mailto:CA%2BZVpt8eW1GZ2BDWoeLxzvcnOyXtgPtGLicGyXWaPWt%2Bhm...@mail.gmail.com>>,
> Kyunghoon Lee <aeronova...@gmail.com
> <mailto:Swig...@lists.sourceforge.net>

> > https://lists.sourceforge.net/lists/listinfo/swig-user
>
> I suggest you avoid std::auto_ptr if possible. It is deprecated because
> copying transfers ownership, which is rarely useful.
>
> If you have a really recent C++ compiler you can use std::shared_ptr
> (allows multiple owners) or std::unique_ptr (cannot be copied). Older
> compilers will support tr1::shared_ptr or boost::shared_ptr, but I don't
> know of a good non-copyable smart pointer.
>
> -- Russell
>
>
> ------------------------------------------------------------------------------
> All of the data generated in your IT infrastructure is seriously
> valuable.
> Why? It contains a definitive record of application performance,
> security
> threats, fraudulent activity, and more. Splunk takes this data and makes
> sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-d2d-c2
> _______________________________________________
> Swig-user mailing list
> Swig...@lists.sourceforge.net <mailto:Swig...@lists.sourceforge.net>

> https://lists.sourceforge.net/lists/listinfo/swig-user
>
>
> Hi Russell,
>
> Thank you for your advice. I'll ask my colleague to avoid auto_ptr.
> However, I wonder whether SWIG still can deal with auto_ptr or not. I
> hope someone could answer it.

Here is my macro for auto_ptr and an example using them:

%define AUTO_PTR_TYPEMAPS(TYPE...)
#if defined(SWIGJAVA)
%typemap (jni) std::auto_ptr<TYPE > "jlong"
%typemap (jtype) std::auto_ptr<TYPE > "long"
%typemap (jstype) std::auto_ptr<TYPE > "$typemap(jstype, TYPE)"

%typemap (out) std::auto_ptr<TYPE > %{
jlong lpp = 0;
*(TYPE**) &lpp = $1.release();
$result = lpp;
%}
%typemap(javaout) std::auto_ptr<TYPE > {
long cPtr = $jnicall;
return (cPtr == 0) ? null : new $typemap(jstype, TYPE)(cPtr, true);
}
#elif defined(SWIGCSHARP)
%typemap (ctype) std::auto_ptr<TYPE > "void *"
%typemap (imtype, out="IntPtr") std::auto_ptr<TYPE > "HandleRef"
%typemap (cstype) std::auto_ptr<TYPE > "$typemap(cstype, TYPE)"
%typemap (out) std::auto_ptr<TYPE > %{
$result = (void *)$1.release();
%}
%typemap(csout, excode=SWIGEXCODE) std::auto_ptr<TYPE > {
IntPtr cPtr = $imcall;
$typemap(cstype, TYPE) ret = (cPtr == IntPtr.Zero) ? null : new
$typemap(cstype, TYPE)(cPtr, true);$excode
return ret;
}
#elif defined(SWIGPYTHON)
%typemap (out) std::auto_ptr<TYPE > %{
%set_output(SWIG_NewPointerObj(%as_voidptr($1.release()),
$descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
#endif
%template() std::auto_ptr<TYPE >;
%enddef

namespace std {
template <class T> class auto_ptr {};
}

AUTO_PTR_TYPEMAPS(MyStruct)

%inline %{
#include <memory>
struct MyStruct {};
std::auto_ptr<MyStruct> factory_function() {
return std::auto_ptr<MyStruct>(new MyStruct());
}
%}

It only provides return values from functions. If you have functions
taking auto_ptr as an input, you'll have to think of something else.

SWIG provides support for shared_ptr for Python, so that is a good option.

William

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1

Reply all
Reply to author
Forward
0 new messages