From 1c968b3695b234ec94cf7fbdb9902ecd519d2edf Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Mon, 10 Apr 2017 18:53:46 +0900 Subject: [PATCH 1/8] add a test for shared_ptr with director --- .../test-suite/cpp11_shared_ptr_director.i | 23 ++++++++++++++++++ .../ruby/cpp11_shared_ptr_director_runme.rb | 24 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Examples/test-suite/cpp11_shared_ptr_director.i create mode 100644 Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb diff --git a/Examples/test-suite/cpp11_shared_ptr_director.i b/Examples/test-suite/cpp11_shared_ptr_director.i new file mode 100644 index 00000000000..a8907de2cdc --- /dev/null +++ b/Examples/test-suite/cpp11_shared_ptr_director.i @@ -0,0 +1,23 @@ +%module(directors="1") "cpp11_shared_ptr_director" + +%{ +#include +%} + +%include "std_shared_ptr.i"; +%shared_ptr(Created); +%feature("director") Creator; + +%inline %{ +struct Created {}; + +struct Creator { + Creator() {}; + virtual std::shared_ptr create() = 0; + virtual ~Creator() {} +}; + +void crash(Creator* creator) { + std::shared_ptr ptr = creator->create(); +} +%} diff --git a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb new file mode 100644 index 00000000000..17e7512f38b --- /dev/null +++ b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb @@ -0,0 +1,24 @@ +require 'cpp11_shared_ptr_director' + +include Cpp11_shared_ptr_director + +class Derived < Creator + + def initialize(flag) + @return_none = flag + super() + end + + def create + if @return_none + nil + else + Created.new + end + end + +end + +crash(Derived.new(false)) +crash(Derived.new(true)) + From b3c2b1c51ca703177082479874d3ac533d529de0 Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Mon, 10 Apr 2017 23:26:18 +0900 Subject: [PATCH 2/8] [ruby] add %typemap(directorout) for shared_ptr. --- Examples/test-suite/cpp11_shared_ptr_director.i | 13 +++++++++++-- .../ruby/cpp11_shared_ptr_director_runme.rb | 4 ++-- Lib/ruby/boost_shared_ptr.i | 12 ++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Examples/test-suite/cpp11_shared_ptr_director.i b/Examples/test-suite/cpp11_shared_ptr_director.i index a8907de2cdc..23e649af36a 100644 --- a/Examples/test-suite/cpp11_shared_ptr_director.i +++ b/Examples/test-suite/cpp11_shared_ptr_director.i @@ -9,7 +9,10 @@ %feature("director") Creator; %inline %{ -struct Created {}; +struct Created { + Created() {}; + int get_m() { return 1; }; +}; struct Creator { Creator() {}; @@ -17,7 +20,13 @@ struct Creator { virtual ~Creator() {} }; -void crash(Creator* creator) { +int crash(Creator* creator) { std::shared_ptr ptr = creator->create(); + if (ptr) { + return ptr->get_m(); + } else { + return -1; + } } + %} diff --git a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb index 17e7512f38b..208e7260316 100644 --- a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb +++ b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb @@ -19,6 +19,6 @@ def create end -crash(Derived.new(false)) -crash(Derived.new(true)) +p crash(Derived.new(false)) +p crash(Derived.new(true)) diff --git a/Lib/ruby/boost_shared_ptr.i b/Lib/ruby/boost_shared_ptr.i index 938074d8181..356e87452c6 100644 --- a/Lib/ruby/boost_shared_ptr.i +++ b/Lib/ruby/boost_shared_ptr.i @@ -197,6 +197,18 @@ %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); } +%typemap(directorout) 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, %convertptr_flags); + if (!SWIG_IsOK(swig_res)) { + %dirout_fail(swig_res,"$type"); + } + $result = *(%reinterpret_cast(swig_argp, $<ype)); + } +} + %typemap(varin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > { swig_ruby_owntype newmem = {0, 0}; void *argp = 0; From 17b4e0c66ec262381832950cc381e1a22ed6ffaa Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Tue, 11 Apr 2017 18:41:14 +0900 Subject: [PATCH 3/8] [ruby] add %typemap(directorin) and %typemap(directorout) for shared_ptr. --- .../test-suite/cpp11_shared_ptr_director.i | 48 ++++++++--- .../ruby/cpp11_shared_ptr_director_runme.rb | 34 ++++++-- Lib/ruby/boost_shared_ptr.i | 80 ++++++++++++++++--- 3 files changed, 133 insertions(+), 29 deletions(-) diff --git a/Examples/test-suite/cpp11_shared_ptr_director.i b/Examples/test-suite/cpp11_shared_ptr_director.i index 23e649af36a..bb23a1de775 100644 --- a/Examples/test-suite/cpp11_shared_ptr_director.i +++ b/Examples/test-suite/cpp11_shared_ptr_director.i @@ -5,23 +5,29 @@ %} %include "std_shared_ptr.i"; -%shared_ptr(Created); -%feature("director") Creator; +%shared_ptr(C); +%feature("director") Base; %inline %{ -struct Created { - Created() {}; - int get_m() { return 1; }; +struct C { + C() : m(1) {}; + C(int n) : m(n) {}; + int get_m() { return m; }; + int m; }; -struct Creator { - Creator() {}; - virtual std::shared_ptr create() = 0; - virtual ~Creator() {} +struct Base { + Base() {}; + virtual std::shared_ptr 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(std::shared_ptr c) = 0; + virtual int take_c_shared_ptr_by_ref(std::shared_ptr& c) = 0; + virtual ~Base() {} }; -int crash(Creator* creator) { - std::shared_ptr ptr = creator->create(); +int call_ret_c_shared_ptr(Base* b) { + std::shared_ptr ptr = b->ret_c_shared_ptr(); if (ptr) { return ptr->get_m(); } else { @@ -29,4 +35,24 @@ int crash(Creator* creator) { } } +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) { + std::shared_ptr ptr(new C(6)); + return b->take_c_shared_ptr_by_value(ptr); +} + +int call_take_c_shared_ptr_by_ref(Base* b) { + std::shared_ptr ptr(new C(7)); + return b->take_c_shared_ptr_by_ref(ptr); +} + %} diff --git a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb index 208e7260316..c2aefaa3da4 100644 --- a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb +++ b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb @@ -2,23 +2,47 @@ include Cpp11_shared_ptr_director -class Derived < Creator +class Derived < Base def initialize(flag) @return_none = flag super() end - def create + def ret_c_shared_ptr if @return_none nil else - Created.new + 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) + c.get_m + end + + def take_c_shared_ptr_by_ref(c) + c.get_m + end + end -p crash(Derived.new(false)) -p crash(Derived.new(true)) +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 diff --git a/Lib/ruby/boost_shared_ptr.i b/Lib/ruby/boost_shared_ptr.i index 356e87452c6..ef65fcb258d 100644 --- a/Lib/ruby/boost_shared_ptr.i +++ b/Lib/ruby/boost_shared_ptr.i @@ -24,7 +24,9 @@ // Typemap customisations... +// // plain value +// %typemap(in) 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); @@ -62,8 +64,28 @@ %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) { swig_ruby_owntype newmem = {0, 0}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SHARED_PTR_DISOWN | %convertptr_flags, &newmem); @@ -79,7 +101,6 @@ $1 = %const_cast((smartarg ? smartarg->get() : 0), $1_ltype); } } - %typemap(out, fragment="SWIG_null_deleter_python") CONST TYPE * { SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner) : 0; %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), $owner | SWIG_POINTER_OWN)); @@ -108,7 +129,16 @@ %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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -153,8 +183,17 @@ %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) { swig_ruby_owntype newmem = {0, 0}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SHARED_PTR_DISOWN | %convertptr_flags, &newmem); @@ -182,7 +221,16 @@ #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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -197,18 +245,6 @@ %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); } -%typemap(directorout) 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, %convertptr_flags); - if (!SWIG_IsOK(swig_res)) { - %dirout_fail(swig_res,"$type"); - } - $result = *(%reinterpret_cast(swig_argp, $<ype)); - } -} - %typemap(varin) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > { swig_ruby_owntype newmem = {0, 0}; void *argp = 0; @@ -224,7 +260,21 @@ %set_varoutput(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SWIG_POINTER_OWN)); } +%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, %convertptr_flags); + if (!SWIG_IsOK(swig_res)) { + %dirout_fail(swig_res,"$type"); + } + $result = *(%reinterpret_cast(swig_argp, $<ype)); + } +} + +// // 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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -251,7 +301,9 @@ #error "varout typemap not implemented" %} +// // 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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -279,7 +331,9 @@ #error "varout typemap not implemented" %} +// // 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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); From 0f94b936e82201f56cc6d4a24cd3645640ddeaf9 Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Tue, 11 Apr 2017 18:51:32 +0900 Subject: [PATCH 4/8] [ruby] enable a test, cpp11_shared_ptr_director. --- Examples/test-suite/ruby/Makefile.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Examples/test-suite/ruby/Makefile.in b/Examples/test-suite/ruby/Makefile.in index 27e9f5e1800..33f42a7fc27 100644 --- a/Examples/test-suite/ruby/Makefile.in +++ b/Examples/test-suite/ruby/Makefile.in @@ -33,7 +33,8 @@ CPP_TEST_CASES = \ CPP11_TEST_CASES = \ cpp11_hash_tables \ cpp11_shared_ptr_upcast \ - cpp11_shared_ptr_const + cpp11_shared_ptr_const \ + cpp11_shared_ptr_director C_TEST_CASES += \ li_cstring \ From 0020fc97b02d6ec7d58e211d1a652f68d6bc7f51 Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Wed, 12 Apr 2017 12:20:05 +0900 Subject: [PATCH 5/8] [ruby] add %typemap(directorin) for shared_ptr. --- .../test-suite/cpp11_shared_ptr_director.i | 32 +++++++++++++++ .../ruby/cpp11_shared_ptr_director_runme.rb | 41 ++++++++++++++++--- Lib/ruby/boost_shared_ptr.i | 26 +++++++++++- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/Examples/test-suite/cpp11_shared_ptr_director.i b/Examples/test-suite/cpp11_shared_ptr_director.i index bb23a1de775..0f05541349f 100644 --- a/Examples/test-suite/cpp11_shared_ptr_director.i +++ b/Examples/test-suite/cpp11_shared_ptr_director.i @@ -23,6 +23,8 @@ struct Base { virtual int take_c_by_value(C c) = 0; virtual int take_c_shared_ptr_by_value(std::shared_ptr c) = 0; virtual int take_c_shared_ptr_by_ref(std::shared_ptr& c) = 0; + virtual int take_c_shared_ptr_by_pointer(std::shared_ptr* c) = 0; + virtual int take_c_shared_ptr_by_pointer_ref(std::shared_ptr*const&c) = 0; virtual ~Base() {} }; @@ -50,9 +52,39 @@ int call_take_c_shared_ptr_by_value(Base* b) { return b->take_c_shared_ptr_by_value(ptr); } +int call_take_c_shared_ptr_by_value_with_null(Base* b) { + std::shared_ptr ptr; + return b->take_c_shared_ptr_by_value(ptr); +} + int call_take_c_shared_ptr_by_ref(Base* b) { std::shared_ptr 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) { + std::shared_ptr ptr; + return b->take_c_shared_ptr_by_ref(ptr); +} + +int call_take_c_shared_ptr_by_pointer(Base* b) { + std::shared_ptr 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) { + std::shared_ptr ptr; + return b->take_c_shared_ptr_by_pointer(&ptr); +} + +int call_take_c_shared_ptr_by_pointer_ref(Base* b) { + auto ptr = new std::shared_ptr(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) { + auto ptr = new std::shared_ptr(); + return b->take_c_shared_ptr_by_pointer_ref(ptr); +} + %} diff --git a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb index c2aefaa3da4..51b338aef81 100644 --- a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb +++ b/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb @@ -26,11 +26,35 @@ def take_c_by_value(c) end def take_c_shared_ptr_by_value(c) - c.get_m + if c + c.get_m + else + -2 + end end def take_c_shared_ptr_by_ref(c) - c.get_m + 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 @@ -42,7 +66,14 @@ def take_c_shared_ptr_by_ref(c) 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_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 diff --git a/Lib/ruby/boost_shared_ptr.i b/Lib/ruby/boost_shared_ptr.i index ef65fcb258d..0350f41d9ff 100644 --- a/Lib/ruby/boost_shared_ptr.i +++ b/Lib/ruby/boost_shared_ptr.i @@ -260,11 +260,19 @@ %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, %convertptr_flags); + 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"); } @@ -301,6 +309,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 // @@ -331,6 +347,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 // From 6672338cc0d74c68c6738c53ba7de06a83b1efaa Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Fri, 21 Apr 2017 22:44:38 +0900 Subject: [PATCH 6/8] [ruby] delete unnecessary changes. --- Lib/ruby/boost_shared_ptr.i | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/Lib/ruby/boost_shared_ptr.i b/Lib/ruby/boost_shared_ptr.i index 0350f41d9ff..02e2f999f84 100644 --- a/Lib/ruby/boost_shared_ptr.i +++ b/Lib/ruby/boost_shared_ptr.i @@ -24,9 +24,7 @@ // Typemap customisations... -// // plain value -// %typemap(in) 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); @@ -82,10 +80,8 @@ $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) { swig_ruby_owntype newmem = {0, 0}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SHARED_PTR_DISOWN | %convertptr_flags, &newmem); @@ -101,6 +97,7 @@ $1 = %const_cast((smartarg ? smartarg->get() : 0), $1_ltype); } } + %typemap(out, fragment="SWIG_null_deleter_python") CONST TYPE * { SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartresult = $1 ? new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner) : 0; %set_output(SWIG_NewPointerObj(%as_voidptr(smartresult), $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), $owner | SWIG_POINTER_OWN)); @@ -136,9 +133,7 @@ #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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -190,10 +185,8 @@ #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) { swig_ruby_owntype newmem = {0, 0}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), SHARED_PTR_DISOWN | %convertptr_flags, &newmem); @@ -228,9 +221,7 @@ #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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -280,9 +271,7 @@ } } -// // 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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -317,9 +306,7 @@ } } -// // 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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); @@ -355,9 +342,7 @@ } } -// // 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}; res = SWIG_ConvertPtrAndOwn($input, &argp, $descriptor(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *), %convertptr_flags, &newmem); From 31459d0cc070c294e6f7465ef68b262c634bbdbd Mon Sep 17 00:00:00 2001 From: Takashi Tamura Date: Fri, 21 Apr 2017 22:56:13 +0900 Subject: [PATCH 7/8] [ruby] use boost/shared_ptr and boost_shared_ptr.i. not use auto. --- .../test-suite/cpp11_shared_ptr_director.i | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Examples/test-suite/cpp11_shared_ptr_director.i b/Examples/test-suite/cpp11_shared_ptr_director.i index 0f05541349f..0a8a2494fe4 100644 --- a/Examples/test-suite/cpp11_shared_ptr_director.i +++ b/Examples/test-suite/cpp11_shared_ptr_director.i @@ -1,10 +1,10 @@ %module(directors="1") "cpp11_shared_ptr_director" %{ -#include +#include %} -%include "std_shared_ptr.i"; +%include "boost_shared_ptr.i"; %shared_ptr(C); %feature("director") Base; @@ -18,18 +18,18 @@ struct C { struct Base { Base() {}; - virtual std::shared_ptr ret_c_shared_ptr() = 0; + virtual boost::shared_ptr 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(std::shared_ptr c) = 0; - virtual int take_c_shared_ptr_by_ref(std::shared_ptr& c) = 0; - virtual int take_c_shared_ptr_by_pointer(std::shared_ptr* c) = 0; - virtual int take_c_shared_ptr_by_pointer_ref(std::shared_ptr*const&c) = 0; + virtual int take_c_shared_ptr_by_value(boost::shared_ptr c) = 0; + virtual int take_c_shared_ptr_by_ref(boost::shared_ptr& c) = 0; + virtual int take_c_shared_ptr_by_pointer(boost::shared_ptr* c) = 0; + virtual int take_c_shared_ptr_by_pointer_ref(boost::shared_ptr*const&c) = 0; virtual ~Base() {} }; int call_ret_c_shared_ptr(Base* b) { - std::shared_ptr ptr = b->ret_c_shared_ptr(); + boost::shared_ptr ptr = b->ret_c_shared_ptr(); if (ptr) { return ptr->get_m(); } else { @@ -48,42 +48,42 @@ int call_take_c_by_value(Base* b) { } int call_take_c_shared_ptr_by_value(Base* b) { - std::shared_ptr ptr(new C(6)); + boost::shared_ptr 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) { - std::shared_ptr ptr; + boost::shared_ptr ptr; return b->take_c_shared_ptr_by_value(ptr); } int call_take_c_shared_ptr_by_ref(Base* b) { - std::shared_ptr ptr(new C(7)); + boost::shared_ptr 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) { - std::shared_ptr ptr; + boost::shared_ptr ptr; return b->take_c_shared_ptr_by_ref(ptr); } int call_take_c_shared_ptr_by_pointer(Base* b) { - std::shared_ptr ptr(new C(8)); + boost::shared_ptr 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) { - std::shared_ptr ptr; + boost::shared_ptr ptr; return b->take_c_shared_ptr_by_pointer(&ptr); } int call_take_c_shared_ptr_by_pointer_ref(Base* b) { - auto ptr = new std::shared_ptr(new C(9)); + boost::shared_ptr *ptr = new boost::shared_ptr(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) { - auto ptr = new std::shared_ptr(); + boost::shared_ptr *ptr = new boost::shared_ptr(); return b->take_c_shared_ptr_by_pointer_ref(ptr); } From 093cf8d60f0622f66bfbddca75167d9160801502 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 21 Apr 2017 19:28:08 +0100 Subject: [PATCH 8/8] Rename shared_ptr testcase --- CHANGES.current | 3 +++ ...1_shared_ptr_director.i => li_boost_shared_ptr_director.i} | 2 +- Examples/test-suite/ruby/Makefile.in | 2 +- ...irector_runme.rb => li_boost_shared_ptr_director_runme.rb} | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) rename Examples/test-suite/{cpp11_shared_ptr_director.i => li_boost_shared_ptr_director.i} (97%) rename Examples/test-suite/ruby/{cpp11_shared_ptr_director_runme.rb => li_boost_shared_ptr_director_runme.rb} (94%) diff --git a/CHANGES.current b/CHANGES.current index 9ea6662cc96..e859072bd99 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -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: tamuratak [Ruby] #930, #937 - Fix containers of std::shared_ptr. Upcasting, const types (eg vector>) and NULL/nullptr support added. diff --git a/Examples/test-suite/cpp11_shared_ptr_director.i b/Examples/test-suite/li_boost_shared_ptr_director.i similarity index 97% rename from Examples/test-suite/cpp11_shared_ptr_director.i rename to Examples/test-suite/li_boost_shared_ptr_director.i index 0a8a2494fe4..4acfa1a5d0e 100644 --- a/Examples/test-suite/cpp11_shared_ptr_director.i +++ b/Examples/test-suite/li_boost_shared_ptr_director.i @@ -1,4 +1,4 @@ -%module(directors="1") "cpp11_shared_ptr_director" +%module(directors="1") "li_boost_shared_ptr_director" %{ #include diff --git a/Examples/test-suite/ruby/Makefile.in b/Examples/test-suite/ruby/Makefile.in index 33f42a7fc27..cc2ba1ed27b 100644 --- a/Examples/test-suite/ruby/Makefile.in +++ b/Examples/test-suite/ruby/Makefile.in @@ -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 \ @@ -34,7 +35,6 @@ CPP11_TEST_CASES = \ cpp11_hash_tables \ cpp11_shared_ptr_upcast \ cpp11_shared_ptr_const \ - cpp11_shared_ptr_director C_TEST_CASES += \ li_cstring \ diff --git a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb b/Examples/test-suite/ruby/li_boost_shared_ptr_director_runme.rb similarity index 94% rename from Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb rename to Examples/test-suite/ruby/li_boost_shared_ptr_director_runme.rb index 51b338aef81..0f7f14b46dc 100644 --- a/Examples/test-suite/ruby/cpp11_shared_ptr_director_runme.rb +++ b/Examples/test-suite/ruby/li_boost_shared_ptr_director_runme.rb @@ -1,6 +1,6 @@ -require 'cpp11_shared_ptr_director' +require 'li_boost_shared_ptr_director' -include Cpp11_shared_ptr_director +include Li_boost_shared_ptr_director class Derived < Base