Add Guile support for std::unique_ptr and std::auto_ptr

Equivalent to Python/Ruby implementations.
This commit is contained in:
William S Fulton 2022-08-12 23:50:54 +01:00
commit 23ce0655a6
10 changed files with 248 additions and 7 deletions

View file

@ -118,6 +118,8 @@ static SCM swig_symbol = SCM_EOL;
( !scm_is_null(x) && SCM_INSTANCEP(x) && scm_is_true(scm_slot_exists_p(x, swig_symbol)) \
? scm_slot_ref(x, swig_symbol) : (x) )
SWIGINTERN void SWIG_Guile_MarkPointerNoncollectable(SCM s);
SWIGINTERN SCM
SWIG_Guile_NewPointerObj(void *ptr, swig_type_info *type, int owner)
{
@ -191,6 +193,7 @@ SWIG_Guile_ConvertPtr(SCM s, void **result, swig_type_info *type, int flags)
swig_cast_info *cast;
swig_type_info *from;
SCM smob = SWIG_Guile_GetSmob(s);
int ret = SWIG_ERROR;
if (scm_is_null(smob)) {
*result = NULL;
@ -203,22 +206,34 @@ SWIG_Guile_ConvertPtr(SCM s, void **result, swig_type_info *type, int flags)
} else if (SWIG_Guile_IsValidSmob(smob)) {
from = (swig_type_info *) SCM_CELL_WORD_2(smob);
if (!from) return SWIG_ERROR;
if ((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) {
if ((SCM_CELL_TYPE(smob) == swig_collectable_tag && SCM_CELL_WORD_1(smob) == 0) || SCM_CELL_TYPE(smob) == swig_tag) {
return SWIG_ERROR_RELEASE_NOT_OWNED;
}
}
if (type) {
cast = SWIG_TypeCheckStruct(from, type);
if (cast) {
int newmemory = 0;
*result = SWIG_TypeCast(cast, (void *) SCM_CELL_WORD_1(smob), &newmemory);
assert(!newmemory); /* newmemory handling not yet implemented */
return SWIG_OK;
} else {
return SWIG_ERROR;
ret = SWIG_OK;
}
} else {
*result = (void *) SCM_CELL_WORD_1(smob);
return SWIG_OK;
ret = SWIG_OK;
}
if (flags & SWIG_POINTER_DISOWN) {
SWIG_Guile_MarkPointerNoncollectable(smob);
}
if (flags & SWIG_POINTER_CLEAR) {
SCM_SET_CELL_WORD_1(smob, 0);
}
}
return SWIG_ERROR;
return ret;
}
SWIGINTERNINLINE void *

33
Lib/guile/std_auto_ptr.i Normal file
View file

@ -0,0 +1,33 @@
/* -----------------------------------------------------------------------------
* std_auto_ptr.i
*
* SWIG library file for handling std::auto_ptr.
* Memory ownership is passed from the std::auto_ptr C++ layer to the proxy
* class when returning a std::auto_ptr from a function.
* Memory ownership is passed from the proxy class to the std::auto_ptr in the
* C++ layer when passed as a parameter to a wrapped function.
* ----------------------------------------------------------------------------- */
%define %auto_ptr(TYPE)
%typemap(in, noblock=1) std::auto_ptr< TYPE > (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
scm_misc_error(FUNC_NAME, "Cannot release ownership as memory is not owned for argument $argnum of type 'TYPE *'", SCM_EOL);
} else {
%argument_fail(res, "TYPE *", $symname, $argnum);
}
}
$1.reset((TYPE *)argp);
}
%typemap (out) std::auto_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN));
%}
%template() std::auto_ptr< TYPE >;
%enddef
namespace std {
template <class T> class auto_ptr {};
}

View file

@ -0,0 +1,33 @@
/* -----------------------------------------------------------------------------
* std_unique_ptr.i
*
* SWIG library file for handling std::unique_ptr.
* Memory ownership is passed from the std::unique_ptr C++ layer to the proxy
* class when returning a std::unique_ptr from a function.
* Memory ownership is passed from the proxy class to the std::unique_ptr in the
* C++ layer when passed as a parameter to a wrapped function.
* ----------------------------------------------------------------------------- */
%define %unique_ptr(TYPE)
%typemap(in, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) {
res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE);
if (!SWIG_IsOK(res)) {
if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
scm_misc_error(FUNC_NAME, "Cannot release ownership as memory is not owned for argument $argnum of type 'TYPE *'", SCM_EOL);
} else {
%argument_fail(res, "TYPE *", $symname, $argnum);
}
}
$1.reset((TYPE *)argp);
}
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN));
%}
%template() std::unique_ptr< TYPE >;
%enddef
namespace std {
template <class T> class unique_ptr {};
}