Value typemaps (Type, const Type&) for "Ptr" types, such as swig
wrapped classes, that define the AsPtr/From methods
%define PYPTR_IN_TYPEMAP(asptr_meth,pyfrag,Type...)
%typemap(in,fragment=pyfrag) Type {
int res = asptr_meth($input, &ptr);
SWIG_type_error("$basetype", $input);
SWIG_null_ref("$basetype");
if (SWIG_arg_fail($argnum)) SWIG_fail;
if (res == SWIG_NEWOBJ) delete ptr;
%typemap(in,fragment=pyfrag) const Type & (int res = 0) {
res = asptr_meth($input, &ptr);
SWIG_type_error("$basetype", $input);
SWIG_null_ref("$basetype");
if (SWIG_arg_fail($argnum)) SWIG_fail;
%typemap(freearg) const Type &
"if (res$argnum == SWIG_NEWOBJ) delete $1;";
%define PYPTR_VARIN_TYPEMAP(asptr_meth,pyfrag,Type...)
%typemap(varin,fragment=pyfrag) Type {
int res = asptr_meth($input, &ptr);
SWIG_type_error("$basetype", $input);
SWIG_append_errmsg(" C/C++ variable '$name'");
SWIG_null_ref("$basetype");
SWIG_append_errmsg(" C/C++ variable '$name'");
if (res == SWIG_NEWOBJ) delete ptr;
%define PYPTR_DIRECTOROUT_TYPEMAP(asptr_meth,pyfrag,Type...)
%typemap(directorargout,fragment=pyfrag) Type *DIRECTOROUT ($*1_ltype temp) {
int res = $input ? asptr_meth($input, &ptr) : 0;
Swig::DirectorTypeMismatchException::raise("Error converting Python object when using asptr_meth.");
if (res == SWIG_NEWOBJ) delete ptr;
%typemap(directorout,fragment=pyfrag) Type {
int res = $input ? asptr_meth($input, &ptr) : 0;
Swig::DirectorTypeMismatchException::raise("Error converting Python object when using asptr_meth.");
if (res == SWIG_NEWOBJ) delete ptr;
%typemap(directorout,fragment=pyfrag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& {
int res = $input ? asptr_meth($input, &ptr) : 0;
Swig::DirectorTypeMismatchException::raise("Error converting Python object when using asptr_meth.");
if (res == SWIG_NEWOBJ) {
/* Possible thread/reentrant problem here! */
static $*ltype temp = *ptr;
%typemap(directorout,fragment=pyfrag) Type &DIRECTOROUT = Type
%define PYPTR_TYPECHECK_TYPEMAP(check,asptr_meth,pyfrag,Type...)
%typemap(typecheck,precedence=check,fragment=pyfrag)
"$1 = asptr_meth($input, (Type**)(0));";
typemap definition for types with AsPtr/From methods
%define %typemap_asptrfrom(CheckCode, AsPtrMeth, FromMeth, AsPtrFrag, FromFrag, Type...)
%fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsPtr_frag(Type)) %{
SWIG_AsVal(Type)(PyObject* obj, Type *val)
int res = SWIG_AsPtr(Type)(obj, &v);
if (!res || !v) return 0;
if (res == SWIG_NEWOBJ) delete v;
%fragment(SWIG_As_frag(Type),"header",
fragment=SWIG_AsVal_frag(Type)) %{
SWIG_As(Type)(PyObject* obj)
SWIG_AsVal(Type)(obj, &v);
PYPTR_IN_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type);
PYPTR_VARIN_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type);
PYPTR_DIRECTOROUT_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type);
PYPTR_TYPECHECK_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(AsPtrMeth),
SWIG_arg(AsPtrFrag), Type);
%typemap_from(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type);
PYPTR_INPUT_TYPEMAP(SWIG_arg(CheckCode),SWIG_arg(AsPtrMeth),
SWIG_arg(AsPtrFrag),Type);
PYVAL_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type);
PYPTR_INOUT_TYPEMAP(Type);
typemap for simple swig types with only AsPtr/From methods
%define %typemap_asptrfromn(CheckCode, Type...)
%typemap_asptrfrom(SWIG_arg(CheckCode),
SWIG_arg(SWIG_AsPtr(Type)),
SWIG_arg(SWIG_From(Type)),
SWIG_arg(SWIG_AsPtr_frag(Type)),
SWIG_arg(SWIG_From_frag(Type)),