Merge branch 'tamuratak-shared_ptr_derived_2'

* tamuratak-shared_ptr_derived_2:
  Correct comment about const removal for shared_ptr
  Correct ordering of declarations in testcase
  Ruby shared_ptr on error code improvement in traits_as::as
  Add support for pointers to shared_ptr and null shared_ptr in Ruby containers
  Add shared_ptr non-overloaded upcast tests
  use forward declaration to treat the dependency of fragments
  [ruby] must not do a null check for VALUE.
  [ruby] add tests for shared_ptr of const Type.
  [ruby] For swig::from, use template specialization to convert shared_ptr<const T> to shared_ptr<T>.
  [ruby] edit comments [skip ci]
  [ruby] move template specialization to std_shared_ptr.i.
  [ruby] add tests for upcasting std::shared_ptr within std containers.
  [ruby] use template specialization for swig::asptr,asval functions on std:shared_ptr.
This commit is contained in:
William S Fulton 2017-04-20 07:50:20 +01:00
commit c9d094e034
8 changed files with 460 additions and 1 deletions

View file

@ -0,0 +1,57 @@
%module cpp11_shared_ptr_const
%{
#include <memory>
#include <vector>
class Foo
{
public:
Foo(int i) : m(i) {}
int get_m() { return m;}
int m;
};
std::shared_ptr<Foo> foo(Foo v) {
return std::shared_ptr<Foo>(new Foo(v));
}
std::shared_ptr<const Foo> const_foo(Foo v) {
return std::shared_ptr<const Foo>(new Foo(v));
}
std::vector<std::shared_ptr<Foo> > foo_vec(Foo v) {
std::vector<std::shared_ptr<Foo> > result;
result.push_back( std::shared_ptr<Foo>(new Foo(v)) );
return result;
}
std::vector<std::shared_ptr<const Foo> > const_foo_vec(Foo v) {
std::vector<std::shared_ptr<const Foo> > result;
result.push_back( std::shared_ptr<Foo>(new Foo(v)) );
return result;
}
%}
%include <std_shared_ptr.i>
%include <std_vector.i>
%shared_ptr(Foo);
%template (FooVector) std::vector<std::shared_ptr<Foo> >;
%template (FooConstVector) std::vector<std::shared_ptr<Foo const> >;
class Foo
{
public:
Foo(int i);
int get_m();
int m;
};
std::shared_ptr<Foo> foo(Foo v);
std::shared_ptr<const Foo> const_foo(Foo v);
std::vector<std::shared_ptr<Foo> > foo_vec(Foo v) const;
std::vector<std::shared_ptr<const Foo> > const_foo_vec(Foo v) const;

View file

@ -0,0 +1,188 @@
%module cpp11_shared_ptr_upcast
%{
#include <set>
#include <map>
#include <memory>
#include <unordered_set>
#include <unordered_map>
#include <vector>
%}
%include <std_vector.i>
%include <std_map.i>
%include <std_shared_ptr.i>
%{
class Base {
public:
Base() : m(-1) {}
Base(int i) : m(i) {}
int get_m() { return m; }
int m;
};
class Derived : public Base {
public:
Derived() : n(-2) {}
Derived(int i) : n(i) {}
int get_n() { return n; }
int n;
};
typedef std::shared_ptr<Base> BasePtr;
typedef std::shared_ptr<Derived> DerivedPtr;
// non-overloaded
int derived_num1(DerivedPtr v) {
return v == nullptr ? 999 : (*v).get_n();
}
int derived_num2(std::vector<DerivedPtr> v) {
return v[0] == nullptr ? 999 : (*v[0]).get_n();
}
int derived_num3(std::map<int, DerivedPtr> v) {
return v[0] == nullptr ? 999 : (*v[0]).get_n();
}
int base_num1(BasePtr v) {
return v == nullptr ? 999 : (*v).get_m();
}
int base_num2(std::vector<BasePtr > v) {
return v[0] == nullptr ? 999 : (*v[0]).get_m();
}
int base_num3(std::map<int, BasePtr > v) {
return v[0] == nullptr ? 999 : (*v[0]).get_m();
}
// overloaded
int derived_num(DerivedPtr v) {
return derived_num1(v);
}
int derived_num(std::vector<DerivedPtr> v) {
return derived_num2(v);
}
int derived_num(std::map<int, DerivedPtr> v) {
return derived_num3(v);
}
int base_num(BasePtr v) {
return base_num1(v);
}
int base_num(std::vector<BasePtr > v) {
return base_num2(v);
}
int base_num(std::map<int, BasePtr > v) {
return base_num3(v);
}
%}
%shared_ptr(Base);
%shared_ptr(Derived);
%template(BaseList) std::vector<std::shared_ptr<Base> >;
%template(DerivedList) std::vector<std::shared_ptr<Derived> >;
%template(BaseMap) std::map<int, std::shared_ptr<Base> >;
%template(DerivedMap) std::map<int, std::shared_ptr<Derived> >;
class Base {
public:
Base();
int get_m();
int m;
};
class Derived : public Base {
public:
Derived();
Derived(int i);
int get_n();
int n;
};
typedef std::shared_ptr<Base> BasePtr;
typedef std::shared_ptr<Derived> DerivedPtr;
// non-overloaded
int derived_num1(DerivedPtr);
int derived_num2(std::vector<std::shared_ptr<Derived> > v);
int derived_num3(std::map<int, DerivedPtr> v);
int base_num1(BasePtr);
int base_num2(std::vector<std::shared_ptr<Base> > v);
int base_num3(std::map<int, BasePtr > v);
// overloaded
int derived_num(DerivedPtr);
int derived_num(std::vector<std::shared_ptr<Derived> > v);
int derived_num(std::map<int, DerivedPtr> v);
int base_num(BasePtr);
int base_num(std::vector<std::shared_ptr<Base> > 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();
}
%}

View file

@ -32,6 +32,8 @@ CPP_TEST_CASES = \
CPP11_TEST_CASES = \
cpp11_hash_tables \
cpp11_shared_ptr_upcast \
cpp11_shared_ptr_const
C_TEST_CASES += \
li_cstring \

View file

@ -0,0 +1,9 @@
require "swig_assert"
require "cpp11_shared_ptr_const"
include Cpp11_shared_ptr_const
simple_assert_equal(1, foo( Foo.new(1) ).get_m )
simple_assert_equal(7, const_foo( Foo.new(7) ).get_m )
simple_assert_equal(7, foo_vec( Foo.new(7) )[0].get_m )
simple_assert_equal(8, const_foo_vec( Foo.new(8) )[0].get_m )

View file

@ -0,0 +1,60 @@
require 'swig_assert'
require 'cpp11_shared_ptr_upcast'
include Cpp11_shared_ptr_upcast
# non-overloaded
simple_assert_equal( 7, derived_num1(Derived.new(7)) )
simple_assert_equal( 7, derived_num2([Derived.new(7)]) )
simple_assert_equal( 7, derived_num3({0 => Derived.new(7)}) )
simple_assert_equal(-1, base_num1(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( 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
simple_assert_equal( 7, derived_num(Derived.new(7)) )
simple_assert_equal( 7, derived_num([Derived.new(7)]) )
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({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

View file

@ -3,8 +3,9 @@
* The Ruby classes, for C++
* ------------------------------------------------------------ */
%include <rubyclasses.swg>
%include <rubystdcommon_forward.swg>
%fragment("StdTraits","header",fragment="StdTraitsCommon")
%fragment("StdTraits","header",fragment="StdTraitsCommon",fragment="StdTraitsForwardDeclaration")
{
namespace swig {

View file

@ -0,0 +1,14 @@
%fragment("StdTraitsForwardDeclaration","header")
{
namespace swig {
template <class Type> struct traits_asptr;
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 noconst_traits;
template <class Type> swig_type_info* type_info();
template <class Type> const char* type_name();
template <class Type> VALUE from(const Type& val);
}
}

View file

@ -1,2 +1,130 @@
#define SWIG_SHARED_PTR_NAMESPACE std
%include <boost_shared_ptr.i>
%include <rubystdcommon_forward.swg>
%fragment("StdSharedPtrTraits","header",fragment="StdTraitsForwardDeclaration")
{
namespace swig {
/*
Template specialization for functions defined in rubystdcommon.swg. Special handling for shared_ptr
is required as, shared_ptr<T> * is used rather than the usual T *, see shared_ptr.i.
*/
template <class Type>
struct traits_asptr<std::shared_ptr<Type> > {
static int asptr(VALUE obj, std::shared_ptr<Type> **val) {
std::shared_ptr<Type> *p = 0;
swig_type_info *descriptor = type_info<std::shared_ptr<Type> >();
swig_ruby_owntype newmem = {0, 0};
int res = descriptor ? SWIG_ConvertPtrAndOwn(obj, (void **)&p, descriptor, 0, &newmem) : SWIG_ERROR;
if (SWIG_IsOK(res)) {
if (val) {
if (*val) {
**val = p ? *p : std::shared_ptr<Type>();
} else {
*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;
}
};
template <class Type>
struct traits_asval<std::shared_ptr<Type> > {
static int asval(VALUE obj, std::shared_ptr<Type> *val) {
if (val) {
std::shared_ptr<Type> ret;
std::shared_ptr<Type> *p = &ret;
int res = traits_asptr<std::shared_ptr<Type> >::asptr(obj, &p);
if (!SWIG_IsOK(res))
return res;
*val = ret;
return SWIG_OK;
} else {
return traits_asptr<std::shared_ptr<Type> >::asptr(obj, (std::shared_ptr<Type> **)(0));
}
}
};
template <class Type>
struct traits_asval<std::shared_ptr<Type> *> {
static int asval(VALUE obj, std::shared_ptr<Type> **val) {
if (val) {
typedef typename noconst_traits<std::shared_ptr<Type> >::noconst_type noconst_type;
if (*val) {
noconst_type ret;
noconst_type *p = &ret;
int res = traits_asptr<noconst_type>::asptr(obj, &p);
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;
}
} else {
return traits_asptr<std::shared_ptr<Type> >::asptr(obj, (std::shared_ptr<Type> **)(0));
}
}
};
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> ret;
std::shared_ptr<Type> *v = &ret;
int res = traits_asptr<std::shared_ptr<Type> >::asptr(obj, &v);
if (SWIG_IsOK(res)) {
return ret;
} 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 std::shared_ptr<Type>();
}
}
};
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;
}
}
};
/*
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.
*/
template<class Type>
struct traits_from<std::shared_ptr<const Type> > {
static VALUE from(const std::shared_ptr<const Type>& val) {
std::shared_ptr<Type> p = std::const_pointer_cast<Type>(val);
return swig::from(p);
}
};
}
}
%fragment("StdSharedPtrTraits");