Skip to content

Commit ebc324d

Browse files
committed
Merge branch 'ruby-autoptr'
* ruby-autoptr: Extend std::auto_ptr<> support to Ruby
2 parents 8c1c01f + 7cc9480 commit ebc324d

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
88
Version 4.0.1 (21 Aug 2019)
99
===========================
1010

11+
2019-09-09: vadz
12+
[Ruby] Add std::auto_ptr<> typemaps.
13+
1114
2019-08-20: TekuConcept
1215
[Javascript] #1535 Add %native support to Javascript.
1316

Examples/test-suite/li_std_auto_ptr.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#endif
1313
%}
1414

15-
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON)
15+
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY)
1616

1717
%include "std_auto_ptr.i"
1818

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'swig_assert'
4+
5+
require 'li_std_auto_ptr'
6+
7+
k1 = Li_std_auto_ptr::makeKlassAutoPtr("first")
8+
k2 = Li_std_auto_ptr::makeKlassAutoPtr("second")
9+
swig_assert_equal_simple(Li_std_auto_ptr::Klass::getTotal_count(), 2)
10+
11+
k1 = nil
12+
GC.start
13+
14+
# GC can need a few runs to actually collect the object.
15+
100.times do ||
16+
next if Li_std_auto_ptr::Klass::getTotal_count() == 2
17+
18+
swig_assert_equal_simple(Li_std_auto_ptr::Klass::getTotal_count(), 1)
19+
break
20+
end
21+
22+
swig_assert_equal_simple(k2.getLabel(), "second")
23+
24+
if Li_std_auto_ptr::Klass::getTotal_count() != 1
25+
STDERR.puts "GC failed to collect the first object, count still #{Li_std_auto_ptr::Klass::getTotal_count()}"
26+
27+
# Skip the rest of the test as it's not going to work correctly anyhow.
28+
exit
29+
end
30+
31+
k2 = nil
32+
GC.start
33+
34+
100.times do ||
35+
next if Li_std_auto_ptr::Klass::getTotal_count() == 1
36+
37+
swig_assert_equal_simple(Li_std_auto_ptr::Klass::getTotal_count(), 0)
38+
break
39+
end
40+
41+
if Li_std_auto_ptr::Klass::getTotal_count() != 0
42+
STDERR.puts "GC failed to collect the second object, count still #{Li_std_auto_ptr::Klass::getTotal_count()}"
43+
end

Lib/ruby/std_auto_ptr.i

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
The typemaps here allow to handle functions returning std::auto_ptr<>,
3+
which is the most common use of this type. If you have functions taking it
4+
as parameter, these typemaps can't be used for them and you need to do
5+
something else (e.g. use shared_ptr<> which SWIG supports fully).
6+
*/
7+
8+
%define %auto_ptr(TYPE)
9+
%typemap (out) std::auto_ptr<TYPE > %{
10+
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
11+
%}
12+
%template() std::auto_ptr<TYPE >;
13+
%enddef
14+
15+
namespace std {
16+
template <class T> class auto_ptr {};
17+
}

0 commit comments

Comments
 (0)