Add support for pointers to shared_ptr and null shared_ptr in Ruby containers
Upcasting of pointers to shared_ptr would need some more fundamental changes, but not done yet ... pointers to shared_ptr are not common.
This commit is contained in:
parent
bd233408e8
commit
83a389d3fb
3 changed files with 160 additions and 32 deletions
|
|
@ -36,27 +36,27 @@ typedef std::shared_ptr<Derived> DerivedPtr;
|
||||||
|
|
||||||
// non-overloaded
|
// non-overloaded
|
||||||
int derived_num1(DerivedPtr v) {
|
int derived_num1(DerivedPtr v) {
|
||||||
return (*v).get_n();
|
return v == nullptr ? 999 : (*v).get_n();
|
||||||
}
|
}
|
||||||
|
|
||||||
int derived_num2(std::vector<DerivedPtr> v) {
|
int derived_num2(std::vector<DerivedPtr> v) {
|
||||||
return (*v[0]).get_n();
|
return v[0] == nullptr ? 999 : (*v[0]).get_n();
|
||||||
}
|
}
|
||||||
|
|
||||||
int derived_num3(std::map<int, DerivedPtr> v) {
|
int derived_num3(std::map<int, DerivedPtr> v) {
|
||||||
return (*v[0]).get_n();
|
return v[0] == nullptr ? 999 : (*v[0]).get_n();
|
||||||
}
|
}
|
||||||
|
|
||||||
int base_num1(BasePtr v) {
|
int base_num1(BasePtr v) {
|
||||||
return (*v).get_m();
|
return v == nullptr ? 999 : (*v).get_m();
|
||||||
}
|
}
|
||||||
|
|
||||||
int base_num2(std::vector<BasePtr > v) {
|
int base_num2(std::vector<BasePtr > v) {
|
||||||
return (*v[0]).get_m();
|
return v[0] == nullptr ? 999 : (*v[0]).get_m();
|
||||||
}
|
}
|
||||||
|
|
||||||
int base_num3(std::map<int, BasePtr > v) {
|
int base_num3(std::map<int, BasePtr > v) {
|
||||||
return (*v[0]).get_m();
|
return v[0] == nullptr ? 999 : (*v[0]).get_m();
|
||||||
}
|
}
|
||||||
|
|
||||||
// overloaded
|
// overloaded
|
||||||
|
|
@ -128,3 +128,61 @@ int derived_num(std::map<int, DerivedPtr> v);
|
||||||
int base_num(BasePtr);
|
int base_num(BasePtr);
|
||||||
int base_num(std::vector<std::shared_ptr<Base> > v);
|
int base_num(std::vector<std::shared_ptr<Base> > v);
|
||||||
int base_num(std::map<int, BasePtr > v);
|
int base_num(std::map<int, BasePtr > v);
|
||||||
|
|
||||||
|
// ptr to shared_ptr
|
||||||
|
%shared_ptr(Base2);
|
||||||
|
%shared_ptr(Derived2)
|
||||||
|
|
||||||
|
%inline %{
|
||||||
|
class Base2 {
|
||||||
|
public:
|
||||||
|
Base2() : m(-1) {}
|
||||||
|
Base2(int i) : m(i) {}
|
||||||
|
int get_m() { return m; }
|
||||||
|
int m;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class Derived2 : public Base2 {
|
||||||
|
public:
|
||||||
|
Derived2() : n(0) {}
|
||||||
|
Derived2(int i) : n(i) {}
|
||||||
|
int get_n_2() { return n; }
|
||||||
|
int n;
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
|
||||||
|
%template(Base2List) std::vector<std::shared_ptr<Base2> * >;
|
||||||
|
%template(Base2Map) std::map<int, std::shared_ptr<Base2> * >;
|
||||||
|
|
||||||
|
%template(Derived2List) std::vector<std::shared_ptr<Derived2> * >;
|
||||||
|
%template(Derived2Map) std::map<int, std::shared_ptr<Derived2> * >;
|
||||||
|
|
||||||
|
%inline %{
|
||||||
|
typedef std::shared_ptr<Derived2> * Derived2Ptr;
|
||||||
|
typedef std::shared_ptr<Base2> * Base2Ptr;
|
||||||
|
|
||||||
|
int base2_num1(Base2Ptr v) {
|
||||||
|
return v == nullptr ? 999 : *v == nullptr ? 888 : (*v)->get_m();
|
||||||
|
}
|
||||||
|
|
||||||
|
int base2_num2(std::vector<Base2Ptr> v) {
|
||||||
|
return v[0] == nullptr ? 999 : *v[0] == nullptr ? 888 : (*v[0])->get_m();
|
||||||
|
}
|
||||||
|
|
||||||
|
int base2_num3(std::map<int, Base2Ptr> v) {
|
||||||
|
return v[0] == nullptr ? 999 : *v[0] == nullptr ? 888 : (*v[0])->get_m();
|
||||||
|
}
|
||||||
|
|
||||||
|
int derived2_num1(Derived2Ptr v) {
|
||||||
|
return v == nullptr ? 999 : *v == nullptr ? 888 : (*v)->get_n_2();
|
||||||
|
}
|
||||||
|
|
||||||
|
int derived2_num2(std::vector<Derived2Ptr> v) {
|
||||||
|
return v[0] == nullptr ? 999 : *v[0] == nullptr ? 888 : (*v[0])->get_n_2();
|
||||||
|
}
|
||||||
|
|
||||||
|
int derived2_num3(std::map<int, Derived2Ptr> v) {
|
||||||
|
return v[0] == nullptr ? 999 : *v[0] == nullptr ? 888 : (*v[0])->get_n_2();
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,14 @@ simple_assert_equal(-1, base_num1(Derived.new(7)) )
|
||||||
simple_assert_equal(-1, base_num2([Derived.new(7)]) )
|
simple_assert_equal(-1, base_num2([Derived.new(7)]) )
|
||||||
simple_assert_equal(-1, base_num3({0 => Derived.new(7)}) )
|
simple_assert_equal(-1, base_num3({0 => Derived.new(7)}) )
|
||||||
|
|
||||||
|
simple_assert_equal( 999, derived_num1(nil) )
|
||||||
|
simple_assert_equal( 999, derived_num2([nil]) )
|
||||||
|
simple_assert_equal( 999, derived_num3({0 => nil}) )
|
||||||
|
|
||||||
|
simple_assert_equal( 999, base_num1(nil) )
|
||||||
|
simple_assert_equal( 999, base_num2([nil]) )
|
||||||
|
simple_assert_equal( 999, base_num3({0 => nil}) )
|
||||||
|
|
||||||
# overloaded
|
# overloaded
|
||||||
simple_assert_equal( 7, derived_num(Derived.new(7)) )
|
simple_assert_equal( 7, derived_num(Derived.new(7)) )
|
||||||
simple_assert_equal( 7, derived_num([Derived.new(7)]) )
|
simple_assert_equal( 7, derived_num([Derived.new(7)]) )
|
||||||
|
|
@ -21,3 +29,32 @@ simple_assert_equal( 7, derived_num({0 => Derived.new(7)}) )
|
||||||
simple_assert_equal(-1, base_num(Derived.new(7)) )
|
simple_assert_equal(-1, base_num(Derived.new(7)) )
|
||||||
simple_assert_equal(-1, base_num([Derived.new(7)]) )
|
simple_assert_equal(-1, base_num([Derived.new(7)]) )
|
||||||
simple_assert_equal(-1, base_num({0 => Derived.new(7)}) )
|
simple_assert_equal(-1, base_num({0 => Derived.new(7)}) )
|
||||||
|
|
||||||
|
# ptr to shared_ptr
|
||||||
|
simple_assert_equal( 7, derived2_num1(Derived2.new(7)) )
|
||||||
|
simple_assert_equal( 7, derived2_num2([Derived2.new(7)]) )
|
||||||
|
simple_assert_equal( 7, derived2_num3({0 => Derived2.new(7)}) )
|
||||||
|
|
||||||
|
simple_assert_equal( -1, base2_num1(Derived2.new(7)) )
|
||||||
|
|
||||||
|
begin
|
||||||
|
# Upcast for pointers to shared_ptr in this generic framework has not been implemented
|
||||||
|
simple_assert_equal( -1, base2_num2([Derived2.new(7)]) )
|
||||||
|
raise RuntimeError, "Failed to catch TypeError"
|
||||||
|
rescue TypeError
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
# Upcast for pointers to shared_ptr in this generic framework has not been implemented
|
||||||
|
simple_assert_equal( -1, base2_num3({0 => Derived2.new(7)}) )
|
||||||
|
raise RuntimeError, "Failed to catch TypeError"
|
||||||
|
rescue TypeError
|
||||||
|
end
|
||||||
|
|
||||||
|
simple_assert_equal( 888, derived2_num1(nil) )
|
||||||
|
simple_assert_equal( 999, derived2_num2([nil]) ) # although 888 would be more consistent
|
||||||
|
simple_assert_equal( 999, derived2_num3({0 => nil}) ) # although 888 would be more consistent
|
||||||
|
|
||||||
|
simple_assert_equal( 888, base2_num1(nil) )
|
||||||
|
simple_assert_equal( 999, base2_num2([nil]) ) # although 888 would be more consistent
|
||||||
|
simple_assert_equal( 999, base2_num3({0 => nil}) ) # although 888 would be more consistent
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,23 +7,32 @@
|
||||||
{
|
{
|
||||||
namespace swig {
|
namespace swig {
|
||||||
/*
|
/*
|
||||||
template specialization for functions defined in rubystdcommon.swg.
|
Template specialization for functions defined in rubystdcommon.swg. Special handling for shared_ptr
|
||||||
here we should treat smart pointers in a way different from the way we treat raw pointers.
|
is required as, shared_ptr<T> * is used rather than the usual T *, see shared_ptr.i.
|
||||||
*/
|
*/
|
||||||
template <class Type>
|
template <class Type>
|
||||||
struct traits_asptr<std::shared_ptr<Type> > {
|
struct traits_asptr<std::shared_ptr<Type> > {
|
||||||
static int asptr(VALUE obj, std::shared_ptr<Type> **val) {
|
static int asptr(VALUE obj, std::shared_ptr<Type> **val) {
|
||||||
std::shared_ptr<Type> *p=0;
|
std::shared_ptr<Type> *p = 0;
|
||||||
swig_type_info *descriptor = type_info<std::shared_ptr<Type> >();
|
swig_type_info *descriptor = type_info<std::shared_ptr<Type> >();
|
||||||
swig_ruby_owntype newmem = {0, 0};
|
swig_ruby_owntype newmem = {0, 0};
|
||||||
int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR;
|
int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR;
|
||||||
if (SWIG_IsOK(res) && p) {
|
if (SWIG_IsOK(res)) {
|
||||||
if (val && *val) **val = *p;
|
if (val) {
|
||||||
if (newmem.own & SWIG_CAST_NEW_MEMORY) delete p;
|
if (*val) {
|
||||||
return SWIG_OK;
|
**val = p ? *p : std::shared_ptr<Type>();
|
||||||
} else {
|
} else {
|
||||||
return SWIG_ERROR;
|
*val = p;
|
||||||
|
if (newmem.own & SWIG_CAST_NEW_MEMORY) {
|
||||||
|
// Upcast for pointers to shared_ptr in this generic framework has not been implemented
|
||||||
|
res = SWIG_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (newmem.own & SWIG_CAST_NEW_MEMORY)
|
||||||
|
delete p;
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -31,12 +40,13 @@ namespace swig {
|
||||||
struct traits_asval<std::shared_ptr<Type> > {
|
struct traits_asval<std::shared_ptr<Type> > {
|
||||||
static int asval(VALUE obj, std::shared_ptr<Type> *val) {
|
static int asval(VALUE obj, std::shared_ptr<Type> *val) {
|
||||||
if (val) {
|
if (val) {
|
||||||
std::shared_ptr<Type> ret;
|
std::shared_ptr<Type> ret;
|
||||||
std::shared_ptr<Type> *p=&ret;
|
std::shared_ptr<Type> *p = &ret;
|
||||||
int res = traits_asptr<std::shared_ptr<Type> >::asptr(obj, &p);
|
int res = traits_asptr<std::shared_ptr<Type> >::asptr(obj, &p);
|
||||||
if (!SWIG_IsOK(res)) return res;
|
if (!SWIG_IsOK(res))
|
||||||
if (val) *val = ret;
|
return res;
|
||||||
return SWIG_OK;
|
*val = ret;
|
||||||
|
return SWIG_OK;
|
||||||
} else {
|
} else {
|
||||||
return traits_asptr<std::shared_ptr<Type> >::asptr(obj, (std::shared_ptr<Type> **)(0));
|
return traits_asptr<std::shared_ptr<Type> >::asptr(obj, (std::shared_ptr<Type> **)(0));
|
||||||
}
|
}
|
||||||
|
|
@ -44,17 +54,24 @@ namespace swig {
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Type>
|
template <class Type>
|
||||||
struct traits_asval<std::shared_ptr<Type>*> {
|
struct traits_asval<std::shared_ptr<Type> *> {
|
||||||
static int asval(VALUE obj, std::shared_ptr<Type> **val) {
|
static int asval(VALUE obj, std::shared_ptr<Type> **val) {
|
||||||
if (val && *val) {
|
if (val) {
|
||||||
typedef typename noconst_traits<std::shared_ptr<Type> >::noconst_type noconst_type;
|
typedef typename noconst_traits<std::shared_ptr<Type> >::noconst_type noconst_type;
|
||||||
noconst_type ret;
|
if (*val) {
|
||||||
noconst_type *p = &ret;
|
noconst_type ret;
|
||||||
int res = traits_asptr<noconst_type>::asptr(obj, &p);
|
noconst_type *p = &ret;
|
||||||
if (SWIG_IsOK(res)) {
|
int res = traits_asptr<noconst_type>::asptr(obj, &p);
|
||||||
**(const_cast<noconst_type**>(val)) = ret;
|
if (SWIG_IsOK(res))
|
||||||
|
**(const_cast<noconst_type**>(val)) = ret;
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
noconst_type *p = 0;
|
||||||
|
int res = traits_asptr<noconst_type>::asptr(obj, &p);
|
||||||
|
if (SWIG_IsOK(res))
|
||||||
|
*val = p;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
} else {
|
} else {
|
||||||
return traits_asptr<std::shared_ptr<Type> >::asptr(obj, (std::shared_ptr<Type> **)(0));
|
return traits_asptr<std::shared_ptr<Type> >::asptr(obj, (std::shared_ptr<Type> **)(0));
|
||||||
}
|
}
|
||||||
|
|
@ -73,16 +90,32 @@ namespace swig {
|
||||||
// Uninitialized return value, no Type() constructor required.
|
// Uninitialized return value, no Type() constructor required.
|
||||||
if (throw_error) throw std::invalid_argument("bad type");
|
if (throw_error) throw std::invalid_argument("bad type");
|
||||||
VALUE lastErr = rb_gv_get("$!");
|
VALUE lastErr = rb_gv_get("$!");
|
||||||
if (lastErr == Qnil) {
|
if (lastErr == Qnil)
|
||||||
SWIG_Error(SWIG_TypeError, swig::type_name<std::shared_ptr<Type> >());
|
SWIG_Error(SWIG_TypeError, swig::type_name<std::shared_ptr<Type> >());
|
||||||
}
|
static std::shared_ptr<Type> *v_def = (std::shared_ptr<Type> *) malloc(sizeof(std::shared_ptr<Type>));
|
||||||
static std::shared_ptr<Type> *v_def = (std::shared_ptr<Type>*) malloc(sizeof(std::shared_ptr<Type>));
|
|
||||||
memset(v_def,0,sizeof(std::shared_ptr<Type>));
|
memset(v_def,0,sizeof(std::shared_ptr<Type>));
|
||||||
return *v_def;
|
return *v_def;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <class Type>
|
||||||
|
struct traits_as<std::shared_ptr<Type> *, pointer_category> {
|
||||||
|
static std::shared_ptr<Type> * as(VALUE obj, bool throw_error) {
|
||||||
|
std::shared_ptr<Type> *p = 0;
|
||||||
|
int res = traits_asptr<std::shared_ptr<Type> >::asptr(obj, &p);
|
||||||
|
if (SWIG_IsOK(res)) {
|
||||||
|
return p;
|
||||||
|
} else {
|
||||||
|
if (throw_error) throw std::invalid_argument("bad type");
|
||||||
|
VALUE lastErr = rb_gv_get("$!");
|
||||||
|
if (lastErr == Qnil)
|
||||||
|
SWIG_Error(SWIG_TypeError, swig::type_name<std::shared_ptr<Type> *>());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
we have to remove the const qualifier to work around a BUG
|
we have to remove the const qualifier to work around a BUG
|
||||||
SWIG_TypeQuery("std::shared_ptr<const Type>") == NULL,
|
SWIG_TypeQuery("std::shared_ptr<const Type>") == NULL,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue