diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2022-08-06 18:34:26 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2022-08-06 18:34:26 +0100 |
commit | fa9c7a719789f94ba14858182c2a512c8a3ba3aa (patch) | |
tree | 4696026cc2ac087cae08d7683afd81181c7d39b2 /Lib/php | |
parent | d4b1152d4b0a1be78e3ac7cab3d640f610ec2f39 (diff) | |
download | swig-fa9c7a719789f94ba14858182c2a512c8a3ba3aa.tar.gz |
Add PHP support for std::unique_ptr and std::auto_ptr
Equivalent to Python/Ruby implementations.
Diffstat (limited to 'Lib/php')
-rw-r--r-- | Lib/php/phprun.swg | 16 | ||||
-rw-r--r-- | Lib/php/std_auto_ptr.i | 35 | ||||
-rw-r--r-- | Lib/php/std_unique_ptr.i | 35 |
3 files changed, 83 insertions, 3 deletions
diff --git a/Lib/php/phprun.swg b/Lib/php/phprun.swg index a49683197..a81baf001 100644 --- a/Lib/php/phprun.swg +++ b/Lib/php/phprun.swg @@ -208,10 +208,20 @@ SWIG_ConvertPtrAndOwn(zval *z, void **ptr, swig_type_info *ty, int flags, swig_o *ptr = NULL; } } - if (*ptr == NULL) return SWIG_ERROR; - if (flags & SWIG_POINTER_DISOWN) { - value->newobject = 0; + + if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) && !value->newobject) { + return SWIG_ERROR_RELEASE_NOT_OWNED; + } else { + if (*ptr == NULL) + return SWIG_ERROR; /* should be SWIG_NullReferenceError?? */ + if (flags & SWIG_POINTER_DISOWN) { + value->newobject = 0; + } + if (flags & SWIG_POINTER_CLEAR) { + value->ptr = 0; + } } + return SWIG_OK; } case IS_NULL: diff --git a/Lib/php/std_auto_ptr.i b/Lib/php/std_auto_ptr.i new file mode 100644 index 000000000..7df497e60 --- /dev/null +++ b/Lib/php/std_auto_ptr.i @@ -0,0 +1,35 @@ +/* ----------------------------------------------------------------------------- + * 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) { + zend_type_error("Cannot release ownership as memory is not owned for argument $argnum of type 'TYPE *' of $symname"); + return; + } else { + zend_type_error("Expected TYPE * for argument $argnum of $symname"); + return; + } + } + $1.reset((TYPE *)argp); +} + +%typemap (out) std::auto_ptr< TYPE > %{ + SWIG_SetPointerZval($result, (void *)$1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN); +%} + +%template() std::auto_ptr< TYPE >; +%enddef + +namespace std { + template <class T> class auto_ptr {}; +} diff --git a/Lib/php/std_unique_ptr.i b/Lib/php/std_unique_ptr.i new file mode 100644 index 000000000..591f580cb --- /dev/null +++ b/Lib/php/std_unique_ptr.i @@ -0,0 +1,35 @@ +/* ----------------------------------------------------------------------------- + * 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) { + zend_type_error("Cannot release ownership as memory is not owned for argument $argnum of type 'TYPE *' of $symname"); + return; + } else { + zend_type_error("Expected TYPE * for argument $argnum of $symname"); + return; + } + } + $1.reset((TYPE *)argp); +} + +%typemap (out) std::unique_ptr< TYPE > %{ + SWIG_SetPointerZval($result, (void *)$1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN); +%} + +%template() std::unique_ptr< TYPE >; +%enddef + +namespace std { + template <class T> class unique_ptr {}; +} |