Merge branch 'tamuratak-fix_ruby_null_shared_ptr'

* tamuratak-fix_ruby_null_shared_ptr:
  [ruby] add a test.
  [ruby] use std::vector::back() method.
  [ruby] enable a test for null shared_ptr in containers.
  [ruby] add a test for null shared_ptr in containers.
  [ruby] treat null shared_ptr in std containers properly.

Conflicts:
	Examples/test-suite/ruby/Makefile.in
This commit is contained in:
William S Fulton 2017-04-24 19:50:59 +01:00
commit ee44f9ba67
5 changed files with 81 additions and 2 deletions

View file

@ -0,0 +1,50 @@
%module cpp11_shared_ptr_nullptr_in_containers
%{
#include <memory>
#include <vector>
class C;
%}
%include <std_shared_ptr.i>
%include <std_vector.i>
%shared_ptr(C)
%inline %{
class C {
public:
C() : m(-1) {}
C(int i) : m(i) {}
int get_m() { return m; }
int m;
};
%}
%template() std::vector<std::shared_ptr<C> >;
%inline %{
std::vector<std::shared_ptr<C> > ret_vec_c_shared_ptr() {
std::vector<std::shared_ptr<C> > ret(3);
ret[0] = std::shared_ptr<C>(new C(0));
ret[2] = std::shared_ptr<C>(new C(2));
return ret;
}
std::vector<std::shared_ptr<C> > ret_arg_vec(const std::vector<std::shared_ptr<C> >& v) {
return v;
}
bool is_last_null(const std::vector<std::shared_ptr<C> >& v) {
if( v.back() ) {
return false;
} else {
return true;
}
}
%}

View file

@ -33,8 +33,9 @@ CPP_TEST_CASES = \
CPP11_TEST_CASES = \
cpp11_hash_tables \
cpp11_shared_ptr_upcast \
cpp11_shared_ptr_const \
cpp11_shared_ptr_nullptr_in_containers \
cpp11_shared_ptr_upcast \
C_TEST_CASES += \
li_cstring \

View file

@ -0,0 +1,16 @@
require "cpp11_shared_ptr_nullptr_in_containers"
include Cpp11_shared_ptr_nullptr_in_containers
a = ret_vec_c_shared_ptr()
raise unless a[0].get_m == 0
raise unless a[1] == nil
raise unless a[2].get_m == 2
a = ret_arg_vec([C.new(7), nil, C.new(9)])
raise unless a[0].get_m == 7
raise unless a[1] == nil
raise unless a[2].get_m == 9
raise unless is_last_null([C.new(7), C.new(8), nil])
raise if is_last_null([C.new(7), C.new(8)])

View file

@ -5,7 +5,8 @@ namespace swig {
template <class Type> struct traits_asval;
struct pointer_category;
template <class Type, class Category> struct traits_as;
template<class Type> struct traits_from;
template <class Type> struct traits_from;
template <class Type> struct traits_from_ptr;
template <class Type> struct noconst_traits;
template <class Type> swig_type_info* type_info();
template <class Type> const char* type_name();

View file

@ -113,6 +113,17 @@ namespace swig {
}
};
template <class Type>
struct traits_from_ptr<std::shared_ptr<Type> > {
static VALUE from(std::shared_ptr<Type> *val, int owner = 0) {
if (val && *val) {
return SWIG_NewPointerObj(val, type_info<std::shared_ptr<Type> >(), owner);
} else {
return Qnil;
}
}
};
/*
The descriptors in the shared_ptr typemaps remove the const qualifier for the SWIG type system.
Remove const likewise here, otherwise SWIG_TypeQuery("std::shared_ptr<const Type>") will return NULL.