diff --git a/Examples/test-suite/cpp11_std_unique_ptr.i b/Examples/test-suite/cpp11_std_unique_ptr.i index 804a4a27d..ec7505974 100644 --- a/Examples/test-suite/cpp11_std_unique_ptr.i +++ b/Examples/test-suite/cpp11_std_unique_ptr.i @@ -53,11 +53,9 @@ struct KlassInheritance : virtual Klass { } }; -#if defined(SWIGJAVA) || defined (SWIGCSHARP) || defined(SWIGPYTHON) std::string takeKlassUniquePtr(std::unique_ptr k) { return std::string(k->getLabel()); } -#endif bool is_nullptr(Klass *p) { return p == nullptr; diff --git a/Examples/test-suite/ruby/cpp11_std_unique_ptr_runme.rb b/Examples/test-suite/ruby/cpp11_std_unique_ptr_runme.rb index cfc03fe2a..ed9138bcd 100644 --- a/Examples/test-suite/ruby/cpp11_std_unique_ptr_runme.rb +++ b/Examples/test-suite/ruby/cpp11_std_unique_ptr_runme.rb @@ -11,6 +11,97 @@ def gc_check(expected_count) # swig_assert_equal_simple(expected_count, Cpp11_std_unique_ptr::Klass::getTotal_count()) end +def checkCount(expected_count) + actual_count = Cpp11_std_unique_ptr::Klass.getTotal_count() + if (actual_count != expected_count) + raise RuntimeError, "Counts incorrect, expected:" + expected_count + " actual:" + actual_count + end +end + + +# unique_ptr as input +kin = Cpp11_std_unique_ptr::Klass.new("KlassInput") +checkCount(1) +s = Cpp11_std_unique_ptr.takeKlassUniquePtr(kin) +checkCount(0) +if (s != "KlassInput") + raise RuntimeError, "Incorrect string: " + s +end +exception_thrown = false +begin + Cpp11_std_unique_ptr.is_nullptr(kin) +rescue ObjectPreviouslyDeleted + exception_thrown = true +end +if (!exception_thrown) + raise RuntimeError, "is_nullptr failed to throw" +end +kin = nil +checkCount(0) + +kin = Cpp11_std_unique_ptr::Klass.new("KlassInput") +checkCount(1) +s = Cpp11_std_unique_ptr.takeKlassUniquePtr(kin) +checkCount(0) +if (s != "KlassInput") + raise RuntimeError, "Incorrect string: " + s +end +exception_thrown = false +begin + Cpp11_std_unique_ptr.is_nullptr(kin) +rescue ObjectPreviouslyDeleted + exception_thrown = true +end +if (!exception_thrown) + raise RuntimeError, "is_nullptr failed to throw" +end +exception_thrown = false +begin + Cpp11_std_unique_ptr.takeKlassUniquePtr(kin) +rescue RuntimeError => e + # puts e.message + exception_thrown = true +end +if (!exception_thrown) + raise RuntimeError, "double usage of takeKlassUniquePtr should have been an error" +end +kin = nil +checkCount(0) + +kin = Cpp11_std_unique_ptr::Klass.new("KlassInput") +exception_thrown = false +begin + notowned = Cpp11_std_unique_ptr::get_not_owned_ptr(kin) + Cpp11_std_unique_ptr::takeKlassUniquePtr(notowned) +rescue RuntimeError + exception_thrown = true +end +if (!exception_thrown) + raise RuntimeError, "Should have thrown 'Cannot release ownership as memory is not owned' error" +end +Cpp11_std_unique_ptr.takeKlassUniquePtr(kin) # Ensure object is deleted (can't rely on GC) +checkCount(0) + +kini = Cpp11_std_unique_ptr::KlassInheritance.new("KlassInheritanceInput") +checkCount(1) +s = Cpp11_std_unique_ptr.takeKlassUniquePtr(kini) +checkCount(0) +if (s != "KlassInheritanceInput") + raise RuntimeError, "Incorrect string: " + s +end +exception_thrown = false +begin + Cpp11_std_unique_ptr.is_nullptr(kini) +rescue ObjectPreviouslyDeleted + exception_thrown = true +end +if (!exception_thrown) + raise RuntimeError, "is_nullptr failed to throw" +end +kini = nil +checkCount(0) + +# unique_ptr as output k1 = Cpp11_std_unique_ptr::makeKlassUniquePtr("first") k2 = Cpp11_std_unique_ptr::makeKlassUniquePtr("second") swig_assert_equal_simple(2, Cpp11_std_unique_ptr::Klass::getTotal_count()) diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg index e4c23bde5..6cac4626a 100644 --- a/Lib/ruby/rubyrun.swg +++ b/Lib/ruby/rubyrun.swg @@ -281,6 +281,11 @@ SWIG_Ruby_ConvertPtrAndOwn(VALUE obj, void **ptr, swig_type_info *ty, int flags, own->own = 0; } + if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE)) { + if (!RDATA(obj)->dfree) + return SWIG_ERROR_RELEASE_NOT_OWNED; + } + /* Check to see if the input object is giving up ownership of the underlying C struct or C++ object. If so then we need to reset the destructor since the Ruby object no @@ -292,7 +297,7 @@ SWIG_Ruby_ConvertPtrAndOwn(VALUE obj, void **ptr, swig_type_info *ty, int flags, swig_class *sklass = (swig_class *) ty->clientdata; track = sklass->trackObjects; } - + if (track) { /* We are tracking objects for this class. Thus we change the destructor * to SWIG_RubyRemoveTracking. This allows us to @@ -306,6 +311,10 @@ SWIG_Ruby_ConvertPtrAndOwn(VALUE obj, void **ptr, swig_type_info *ty, int flags, } } + if (flags & SWIG_POINTER_CLEAR) { + DATA_PTR(obj) = 0; + } + /* Do type-checking if type info was provided */ if (ty) { if (ty->clientdata) { diff --git a/Lib/ruby/std_unique_ptr.i b/Lib/ruby/std_unique_ptr.i index 163c7c2d1..a0128ba78 100644 --- a/Lib/ruby/std_unique_ptr.i +++ b/Lib/ruby/std_unique_ptr.i @@ -8,9 +8,22 @@ * ----------------------------------------------------------------------------- */ %define %unique_ptr(TYPE) +%typemap(in, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) { + res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags); + if (!SWIG_IsOK(res)) { + if (res == SWIG_ERROR_RELEASE_NOT_OWNED) { + %releasenotowned_fail(res, "TYPE *", $symname, $argnum); + } else { + %argument_fail(res, "TYPE *", $symname, $argnum); + } + } + $1.reset((TYPE *)argp); +} + %typemap (out) std::unique_ptr< TYPE > %{ %set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags)); %} + %template() std::unique_ptr< TYPE >; %enddef