mirror of https://github.com/swig/swig
Add minimal shared_ptr support
Enable the tests and support of shared_ptr in them for C (which required disabling a previously passing, because not doing anything, attributes test which is currently broken for unrelated reasons).
This commit is contained in:
parent
d77e5d8b0e
commit
e78c8f39ed
|
@ -26,6 +26,12 @@ CPP_TEST_CASES := \
|
|||
c_backend_cpp_natural_std_string \
|
||||
c_backend_cpp_exception
|
||||
|
||||
CPP11_TEST_CASES := \
|
||||
cpp11_shared_ptr_const \
|
||||
cpp11_shared_ptr_nullptr_in_containers \
|
||||
cpp11_shared_ptr_overload \
|
||||
cpp11_shared_ptr_upcast \
|
||||
|
||||
# The following tests are currently broken and need to be fixed.
|
||||
FAILING_C_TESTS := \
|
||||
arrays \
|
||||
|
@ -60,10 +66,7 @@ FAILING_CPP_TESTS := \
|
|||
grouping \
|
||||
import_nomodule \
|
||||
li_attribute \
|
||||
li_boost_shared_ptr \
|
||||
li_boost_shared_ptr_bits \
|
||||
li_boost_shared_ptr_director \
|
||||
li_boost_shared_ptr_template \
|
||||
li_boost_shared_ptr_attribute \
|
||||
li_std_deque \
|
||||
li_std_wstring \
|
||||
li_windows \
|
||||
|
@ -71,9 +74,7 @@ FAILING_CPP_TESTS := \
|
|||
member_pointer \
|
||||
member_pointer_const \
|
||||
mixed_types \
|
||||
multiple_inheritance_shared_ptr \
|
||||
nested_class \
|
||||
smart_pointer_template_defaults_overload \
|
||||
template_basic \
|
||||
template_default \
|
||||
template_enum \
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
#include "cpp11_shared_ptr_const_wrap.h"
|
||||
#include <assert.h>
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
Foo* f;
|
||||
Foo* f2;
|
||||
|
||||
f = Foo_new(17);
|
||||
assert(Foo_get_m(f) == 17);
|
||||
f2 = cpp11_shared_ptr_const_foo(f);
|
||||
assert(Foo_get_m(f2) == 17);
|
||||
Foo_delete(f2);
|
||||
Foo_delete(f);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include "cpp11_shared_ptr_upcast_wrap.h"
|
||||
#include <assert.h>
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
{
|
||||
Derived* d;
|
||||
|
||||
d = Derived_new_i(17);
|
||||
assert( cpp11_shared_ptr_upcast_base_num1((Base *)d) == -1 );
|
||||
assert( cpp11_shared_ptr_upcast_derived_num1(d) == 17 );
|
||||
|
||||
Derived_delete(d);
|
||||
}
|
||||
|
||||
{
|
||||
Derived2* d2;
|
||||
|
||||
d2 = Derived2_new_i(289);
|
||||
assert( cpp11_shared_ptr_upcast_base2_num1((Base2 *)d2) == -1 );
|
||||
assert( cpp11_shared_ptr_upcast_derived2_num1(d2) == 289 );
|
||||
|
||||
Derived2_delete(d2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#include "li_boost_shared_ptr_wrap.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
{
|
||||
Klass* k = Klass_new_rcstd_string("me oh my");
|
||||
assert( strcmp(Klass_getValue(k), "me oh my") == 0 );
|
||||
Klass_delete(k);
|
||||
}
|
||||
|
||||
{
|
||||
Klass* k = li_boost_shared_ptr_factorycreate();
|
||||
assert( strcmp(Klass_getValue(k), "factorycreate") == 0 );
|
||||
Klass_delete(k);
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ public:
|
|||
|
||||
%}
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#if defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#define SHARED_PTR_WRAPPERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
# define SWIG_SHARED_PTR_NAMESPACE SwigBoost
|
||||
#endif
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#if defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#define SHARED_PTR_WRAPPERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
%module li_boost_shared_ptr_attribute
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#if defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#define SHARED_PTR_WRAPPERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
%module li_boost_shared_ptr_bits
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#if defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#define SHARED_PTR_WRAPPERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <boost/shared_ptr.hpp>
|
||||
%}
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY) || defined(SWIGR)
|
||||
#if defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY) || defined(SWIGR)
|
||||
#define SHARED_PTR_WRAPPERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
%}
|
||||
|
||||
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#if defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) || defined(SWIGOCTAVE) || defined(SWIGRUBY)
|
||||
#define SHARED_PTR_WRAPPERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
#ifndef SWIG_SHARED_PTR_NAMESPACE
|
||||
#define SWIG_SHARED_PTR_NAMESPACE boost
|
||||
#endif
|
||||
|
||||
%include <std_shared_ptr.i>
|
|
@ -0,0 +1,55 @@
|
|||
// This could be predefined in e.g. our own boost_shared_ptr.i
|
||||
#ifndef SWIG_SHARED_PTR_NAMESPACE
|
||||
#define SWIG_SHARED_PTR_NAMESPACE std
|
||||
#endif
|
||||
|
||||
%define SWIG_SHARED_PTR_TYPEMAPS(CONST, TYPE...)
|
||||
|
||||
%naturalvar TYPE;
|
||||
%naturalvar SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >;
|
||||
|
||||
// Replace the default "delete arg1" with the code destroying the smart pointer itself instead.
|
||||
%feature("unref") TYPE "(void)arg1; delete smartarg1;"
|
||||
|
||||
// All smart pointers look like normal objects to the code using the interface.
|
||||
%typemap(ctype) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >,
|
||||
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >&,
|
||||
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >*
|
||||
"$typemap(ctype, TYPE)";
|
||||
|
||||
// Typemap for smart pointer type itself.
|
||||
%typemap(out) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >
|
||||
"$result = new $1_ltype($1);"
|
||||
|
||||
// And for the plain type.
|
||||
%typemap(in) CONST TYPE (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) %{
|
||||
smartarg = (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr<CONST TYPE > *)$input;
|
||||
if (!smartarg || !smartarg->get()) {
|
||||
SWIG_exception(SWIG_RuntimeError, "$1_type value is null");
|
||||
return $null;
|
||||
}
|
||||
$1 = **smartarg;%}
|
||||
%typemap(out) CONST TYPE %{
|
||||
$result = (SwigObj*) new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr<CONST TYPE >(new $1_ltype($1));%}
|
||||
|
||||
// Plain type pointer.
|
||||
%typemap(in) CONST TYPE * (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) %{
|
||||
smartarg = (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr<CONST TYPE > *)$input;
|
||||
$1 = (TYPE *)(smartarg ? smartarg->get() : 0);%}
|
||||
%typemap(out, fragment="SWIG_null_deleter") CONST TYPE * %{
|
||||
$result = $1 ? (SwigObj*) new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr<CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner) : 0;%}
|
||||
|
||||
// Plain type references.
|
||||
%typemap(in) CONST TYPE & (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *smartarg = 0) %{
|
||||
smartarg = (SWIG_SHARED_PTR_QNAMESPACE::shared_ptr<CONST TYPE > *)$input;
|
||||
if (!smartarg || !smartarg->get()) {
|
||||
SWIG_exception(SWIG_RuntimeError, "$1_type reference is null");
|
||||
return $null;
|
||||
}
|
||||
$1 = (TYPE *)smartarg->get();%}
|
||||
%typemap(out, fragment="SWIG_null_deleter") CONST TYPE & %{
|
||||
$result = (SwigObj*) new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr<CONST TYPE >($1 SWIG_NO_NULL_DELETER_$owner);%}
|
||||
|
||||
%enddef
|
||||
|
||||
%include <shared_ptr.i>
|
Loading…
Reference in New Issue