[ruby] For swig::from, use template specialization to convert shared_ptr<const T> to shared_ptr<T>.
This commit is contained in:
parent
b8d383cb4a
commit
f96c2ad73d
5 changed files with 72 additions and 0 deletions
47
Examples/test-suite/cpp11_shared_ptr_const.i
Normal file
47
Examples/test-suite/cpp11_shared_ptr_const.i
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
%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::vector<std::shared_ptr<Foo> > foo_vec() {
|
||||
std::vector<std::shared_ptr<Foo> > result;
|
||||
result.push_back( std::shared_ptr<Foo>(new Foo(7)) );
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<const Foo> > const_foo_vec() {
|
||||
std::vector<std::shared_ptr<const Foo> > result;
|
||||
result.push_back( std::shared_ptr<Foo>(new Foo(7)) );
|
||||
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> >;
|
||||
|
||||
std::vector<std::shared_ptr<Foo> > foo_vec() const;
|
||||
std::vector<std::shared_ptr<const Foo> > const_foo_vec() const;
|
||||
|
||||
class Foo
|
||||
{
|
||||
public:
|
||||
Foo(int i);
|
||||
int get_m();
|
||||
int m;
|
||||
};
|
||||
|
|
@ -31,6 +31,7 @@ CPP_TEST_CASES = \
|
|||
|
||||
CPP11_TEST_CASES = \
|
||||
cpp11_hash_tables \
|
||||
cpp11_shared_ptr_const
|
||||
|
||||
C_TEST_CASES += \
|
||||
li_cstring \
|
||||
|
|
|
|||
7
Examples/test-suite/ruby/cpp11_shared_ptr_const_runme.rb
Normal file
7
Examples/test-suite/ruby/cpp11_shared_ptr_const_runme.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
require "swig_assert"
|
||||
require "cpp11_shared_ptr_const"
|
||||
|
||||
include Cpp11_shared_ptr_const
|
||||
|
||||
simple_assert_equal(7, foo_vec()[0].get_m )
|
||||
simple_assert_equal(7, const_foo_vec()[0].get_m )
|
||||
|
|
@ -7,6 +7,8 @@
|
|||
%fragment("StdTraits","header",fragment="StdTraitsCommon")
|
||||
{
|
||||
|
||||
%#define SWIG_RUBYSTDCOMMON
|
||||
|
||||
namespace swig {
|
||||
/*
|
||||
Traits that provides the from method
|
||||
|
|
|
|||
|
|
@ -1,2 +1,17 @@
|
|||
#define SWIG_SHARED_PTR_NAMESPACE std
|
||||
%include <boost_shared_ptr.i>
|
||||
|
||||
|
||||
%wrapper %{
|
||||
#ifdef SWIG_RUBYSTDCOMMON
|
||||
namespace swig {
|
||||
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);
|
||||
}
|
||||
};
|
||||
};
|
||||
#endif
|
||||
%}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue