Skip to content

Commit

Permalink
Merge branch 'tamuratak-fix_ruby_director_and_shared_ptr'
Browse files Browse the repository at this point in the history
* tamuratak-fix_ruby_director_and_shared_ptr:
  Rename shared_ptr testcase
  [ruby] use boost/shared_ptr and boost_shared_ptr.i. not use auto.
  [ruby] delete unnecessary changes.
  [ruby] add %typemap(directorin) for shared_ptr.
  [ruby] enable a test, cpp11_shared_ptr_director.
  [ruby] add %typemap(directorin) and %typemap(directorout) for shared_ptr.
  [ruby] add %typemap(directorout) for shared_ptr.
  add a test for shared_ptr with director

Conflicts:
	CHANGES.current
  • Loading branch information
wsfulton committed Apr 21, 2017
2 parents ab1e9f5 + 093cf8d commit 71e5ff4
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.current
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 3.0.13 (in progress)
============================

2017-04-21: tamuratak
[Ruby] #964 - Add shared_ptr director typemaps.

2017-04-20: wsfulton
[Ruby] #586, #935 Add assert for invalid NULL type parameter when calling SWIG_Ruby_NewPointerObj.

Expand Down
90 changes: 90 additions & 0 deletions Examples/test-suite/li_boost_shared_ptr_director.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
%module(directors="1") "li_boost_shared_ptr_director"

%{
#include <boost/shared_ptr.hpp>
%}

%include "boost_shared_ptr.i";
%shared_ptr(C);
%feature("director") Base;

%inline %{
struct C {
C() : m(1) {};
C(int n) : m(n) {};
int get_m() { return m; };
int m;
};

struct Base {
Base() {};
virtual boost::shared_ptr<C> ret_c_shared_ptr() = 0;
virtual C ret_c_by_value() = 0;
virtual int take_c_by_value(C c) = 0;
virtual int take_c_shared_ptr_by_value(boost::shared_ptr<C> c) = 0;
virtual int take_c_shared_ptr_by_ref(boost::shared_ptr<C>& c) = 0;
virtual int take_c_shared_ptr_by_pointer(boost::shared_ptr<C>* c) = 0;
virtual int take_c_shared_ptr_by_pointer_ref(boost::shared_ptr<C>*const&c) = 0;
virtual ~Base() {}
};

int call_ret_c_shared_ptr(Base* b) {
boost::shared_ptr<C> ptr = b->ret_c_shared_ptr();
if (ptr) {
return ptr->get_m();
} else {
return -1;
}
}

int call_ret_c_by_value(Base* b) {
C c = b->ret_c_by_value();
return c.get_m();
}

int call_take_c_by_value(Base* b) {
C c(5);
return b->take_c_by_value(c);
}

int call_take_c_shared_ptr_by_value(Base* b) {
boost::shared_ptr<C> ptr(new C(6));
return b->take_c_shared_ptr_by_value(ptr);
}

int call_take_c_shared_ptr_by_value_with_null(Base* b) {
boost::shared_ptr<C> ptr;
return b->take_c_shared_ptr_by_value(ptr);
}

int call_take_c_shared_ptr_by_ref(Base* b) {
boost::shared_ptr<C> ptr(new C(7));
return b->take_c_shared_ptr_by_ref(ptr);
}

int call_take_c_shared_ptr_by_ref_with_null(Base* b) {
boost::shared_ptr<C> ptr;
return b->take_c_shared_ptr_by_ref(ptr);
}

int call_take_c_shared_ptr_by_pointer(Base* b) {
boost::shared_ptr<C> ptr(new C(8));
return b->take_c_shared_ptr_by_pointer(&ptr);
}

int call_take_c_shared_ptr_by_pointer_with_null(Base* b) {
boost::shared_ptr<C> ptr;
return b->take_c_shared_ptr_by_pointer(&ptr);
}

int call_take_c_shared_ptr_by_pointer_ref(Base* b) {
boost::shared_ptr<C> *ptr = new boost::shared_ptr<C>(new C(9));
return b->take_c_shared_ptr_by_pointer_ref(ptr);
}

int call_take_c_shared_ptr_by_pointer_ref_with_null(Base* b) {
boost::shared_ptr<C> *ptr = new boost::shared_ptr<C>();
return b->take_c_shared_ptr_by_pointer_ref(ptr);
}

%}
3 changes: 2 additions & 1 deletion Examples/test-suite/ruby/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ top_srcdir = @top_srcdir@
top_builddir = @top_builddir@

CPP_TEST_CASES = \
li_boost_shared_ptr_director \
li_cstring \
li_factory \
li_std_functors \
Expand All @@ -33,7 +34,7 @@ CPP_TEST_CASES = \
CPP11_TEST_CASES = \
cpp11_hash_tables \
cpp11_shared_ptr_upcast \
cpp11_shared_ptr_const
cpp11_shared_ptr_const \

C_TEST_CASES += \
li_cstring \
Expand Down
79 changes: 79 additions & 0 deletions Examples/test-suite/ruby/li_boost_shared_ptr_director_runme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'li_boost_shared_ptr_director'

include Li_boost_shared_ptr_director

class Derived < Base

def initialize(flag)
@return_none = flag
super()
end

def ret_c_shared_ptr
if @return_none
nil
else
C.new
end
end

def ret_c_by_value
C.new
end

def take_c_by_value(c)
c.get_m
end

def take_c_shared_ptr_by_value(c)
if c
c.get_m
else
-2
end
end

def take_c_shared_ptr_by_ref(c)
if c
c.get_m
else
-3
end
end

def take_c_shared_ptr_by_pointer(c)
if c
c.get_m
else
-4
end
end

def take_c_shared_ptr_by_pointer_ref(c)
if c
c.get_m
else
-5
end
end

end

a = Derived.new(false)
b = Derived.new(true)

raise unless call_ret_c_shared_ptr(a) == 1
raise unless call_ret_c_shared_ptr(b) == -1
raise unless call_ret_c_by_value(a) == 1

raise unless call_take_c_by_value(a) == 5
raise unless call_take_c_shared_ptr_by_value(a) == 6
raise unless call_take_c_shared_ptr_by_ref(a) == 7
raise unless call_take_c_shared_ptr_by_pointer(a) == 8
raise unless call_take_c_shared_ptr_by_pointer_ref(a) == 9

raise unless call_take_c_shared_ptr_by_value_with_null(a) == -2
raise unless call_take_c_shared_ptr_by_ref_with_null(a) == -3
raise unless call_take_c_shared_ptr_by_pointer_with_null(a) == -4
raise unless call_take_c_shared_ptr_by_pointer_ref_with_null(a) == -5

75 changes: 75 additions & 0 deletions Lib/ruby/boost_shared_ptr.i
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@
%set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN));
}

%typemap(directorout,noblock=1) CONST TYPE (void *argp, int res = 0) {
swig_ruby_owntype newmem = {0, 0};
res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem);
if (!SWIG_IsOK(res)) {
%dirout_fail(res, "$type");
}
if (!argp) {
%dirout_nullref("$type");
} else {
$result = *(%reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *)->get());
if (newmem.own & SWIG_CAST_NEW_MEMORY) delete %reinterpret_cast(argp, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *);
}
}
%typemap(directorin,noblock=1) CONST TYPE {
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >(new $1_ltype(($1_ltype &)$1));
$input = SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN | %newpointer_flags);
}

// plain pointer
// Note: $disown not implemented by default as it will lead to a memory leak of the shared_ptr instance
%typemap(in) CONST TYPE * (void *argp = 0, int res = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) {
Expand Down Expand Up @@ -108,6 +126,13 @@
%set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN));
}

%typemap(directorin,noblock=1) CONST TYPE * %{
#error "directorin typemap for plain pointer not implemented"
%}
%typemap(directorout,noblock=1) CONST TYPE * %{
#error "directorout typemap for plain pointer not implemented"
%}

// plain reference
%typemap(in) CONST TYPE & (void *argp = 0, int res = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared) {
swig_ruby_owntype newmem = {0, 0};
Expand Down Expand Up @@ -153,6 +178,13 @@
%set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN));
}

%typemap(directorin,noblock=1) CONST TYPE & %{
#error "directorin typemap for plain reference not implemented"
%}
%typemap(directorout,noblock=1) CONST TYPE & %{
#error "directorout typemap for plain reference not implemented"
%}

// plain pointer by reference
// Note: $disown not implemented by default as it will lead to a memory leak of the shared_ptr instance
%typemap(in) TYPE *CONST& (void *argp = 0, int res = 0, $*1_ltype temp = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared) {
Expand Down Expand Up @@ -182,6 +214,13 @@
#error "varout typemap not implemented"
%}

%typemap(directorin,noblock=1) TYPE *CONST& %{
#error "directorin typemap for plain pointer by reference not implemented"
%}
%typemap(directorout,noblock=1) TYPE *CONST& %{
#error "directorout typemap for plain pointer by reference not implemented"
%}

// shared_ptr by value
%typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > (void *argp, int res = 0) {
swig_ruby_owntype newmem = {0, 0};
Expand Down Expand Up @@ -212,6 +251,26 @@
%set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN));
}

%typemap(directorin,noblock=1) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > {
if ($1) {
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1);
$input = SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN | %newpointer_flags);
} else {
$input = Qnil;
}
}
%typemap(directorout,noblock=1) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > (void * swig_argp, int swig_res = 0) {
if (NIL_P($input)) {
$result = $ltype();
} else {
swig_res = SWIG_ConvertPtr($input, &swig_argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags);
if (!SWIG_IsOK(swig_res)) {
%dirout_fail(swig_res,"$type");
}
$result = *(%reinterpret_cast(swig_argp, $&ltype));
}
}

// shared_ptr by reference
%typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & (void *argp, int res = 0, $*1_ltype tempshared) {
swig_ruby_owntype newmem = {0, 0};
Expand Down Expand Up @@ -239,6 +298,14 @@
#error "varout typemap not implemented"
%}

%typemap(directorin,noblock=1) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > & {
if ($1) {
$input = SWIG_NewPointerObj(%as_voidptr(&$1), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %newpointer_flags);
} else {
$input = Qnil;
}
}

// shared_ptr by pointer
%typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > * (void *argp, int res = 0, $*1_ltype tempshared) {
swig_ruby_owntype newmem = {0, 0};
Expand Down Expand Up @@ -267,6 +334,14 @@
#error "varout typemap not implemented"
%}

%typemap(directorin,noblock=1) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *const& {
if ($1 && *$1) {
$input = SWIG_NewPointerObj(%as_voidptr($1), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %newpointer_flags);
} else {
$input = Qnil;
}
}

// shared_ptr by pointer reference
%typemap(in) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *& (void *argp, int res = 0, SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > tempshared, $*1_ltype temp = 0) {
swig_ruby_owntype newmem = {0, 0};
Expand Down

0 comments on commit 71e5ff4

Please sign in to comment.