[ruby] add %typemap(directorin) and %typemap(directorout) for shared_ptr.
This commit is contained in:
parent
b3c2b1c51c
commit
17b4e0c66e
3 changed files with 133 additions and 29 deletions
|
|
@ -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<Created> create() = 0;
|
||||
virtual ~Creator() {}
|
||||
struct Base {
|
||||
Base() {};
|
||||
virtual std::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(std::shared_ptr<C> c) = 0;
|
||||
virtual int take_c_shared_ptr_by_ref(std::shared_ptr<C>& c) = 0;
|
||||
virtual ~Base() {}
|
||||
};
|
||||
|
||||
int crash(Creator* creator) {
|
||||
std::shared_ptr<Created> ptr = creator->create();
|
||||
int call_ret_c_shared_ptr(Base* b) {
|
||||
std::shared_ptr<C> 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<C> 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<C> ptr(new C(7));
|
||||
return b->take_c_shared_ptr_by_ref(ptr);
|
||||
}
|
||||
|
||||
%}
|
||||
|
|
|
|||
|
|
@ -2,23 +2,47 @@ require 'cpp11_shared_ptr_director'
|
|||
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue