add implicitconv support and cosmetics for cast rank

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8095 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-27 22:06:26 +00:00
commit 0e4b388ec9
24 changed files with 484 additions and 308 deletions

View file

@ -28,19 +28,14 @@ SWIG_AsArgcArgv(PyObject* input,
argv[i] = 0;
}
} else {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
PyErr_Clear();
PyErr_SetString(PyExc_TypeError,"list or tuple must contain strings only");
SWIG_PYTHON_THREAD_END_BLOCK;
SWIG_Error(SWIG_TypeError,"list or tuple must contain strings only");
}
}
argv[i] = 0;
return argv;
} else {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
*argc = 0;
PyErr_SetString(PyExc_TypeError,"a list or tuple is expected");
SWIG_PYTHON_THREAD_END_BLOCK;
SWIG_Error(SWIG_TypeError,"a list or tuple is expected");
return 0;
}
} else {

View file

@ -1,2 +1,7 @@
%include <std_common.i>
%include <typemaps/implicit.swg>
#warning "This file provide the %implicit directive, which is an old and fragil"
#warning "way to implement the C++ implicit conversion mechanism."
#warning "Try using the more robust '%implicitconv Type;' directive instead."

View file

@ -32,7 +32,7 @@ SWIG_AsVal(Type) (PyObject *o, Type* val)
return SWIG_OK;
} else {
double d;
int res = SWIG_CastRank(SWIG_AsVal(double)(o, &d));
int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(d, 0.0);
return res;
@ -63,7 +63,7 @@ SWIG_AsVal(Type)(PyObject *o, Type *val)
}
} else {
float re;
int res = SWIG_CastRank(SWIG_AsVal(float)(o, &re));
int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(re, 0.0);
return res;

View file

@ -702,7 +702,7 @@ namespace swig {
PyErr_SetString(PyExc_TypeError, e.what());
}
}
return 0;
return SWIG_ERROR;
}
} else {
sequence *p;
@ -716,7 +716,7 @@ namespace swig {
PyErr_Format(PyExc_TypeError, "a %s is expected",
swig::type_name<sequence>());
}
return 0;
return SWIG_ERROR;
}
};

View file

@ -27,7 +27,7 @@ SWIG_AsVal_dec(bool)(PyObject *obj, bool *val)
return SWIG_OK;
} else {
long v = 0;
int res = SWIG_CastRank(SWIG_AsVal(long)(obj, val ? &v : 0));
int res = SWIG_AddCast(SWIG_AsVal(long)(obj, val ? &v : 0));
if (SWIG_IsOK(res) && val) *val = v ? true : false;
return res;
}
@ -64,13 +64,13 @@ SWIG_AsVal_dec(long)(PyObject *obj, long* val)
long v = PyInt_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_CastRank(SWIG_OK);
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
double d;
int res = SWIG_CastRank(SWIG_AsVal(double)(obj,&d));
int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, LONG_MAX)) {
if (val) *val = (long)(d);
return res;
@ -123,7 +123,7 @@ SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val)
if (!PyErr_Occurred()) {
if (v >= 0) {
if (val) *val = v;
return SWIG_CastRank(SWIG_OK);
return SWIG_AddCast(SWIG_OK);
} else {
return SWIG_OverflowError;
}
@ -132,7 +132,7 @@ SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val)
}
if (!dispatch) {
double d;
int res = SWIG_CastRank(SWIG_AsVal(double)(obj,&d));
int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULONG_MAX)) {
if (val) *val = (unsigned long)(d);
return res;
@ -181,13 +181,13 @@ SWIG_AsVal_dec(long long)(PyObject *obj, long long *val)
long v = PyInt_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_CastRank(SWIG_OK);
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
double d;
int res = SWIG_CastRank(SWIG_AsVal(double)(obj,&d));
int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LLONG_MIN, LLONG_MAX)) {
if (val) *val = (long long)(d);
return res;
@ -240,7 +240,7 @@ SWIG_AsVal_dec(unsigned long long)(PyObject *obj, unsigned long long *val)
if (!PyErr_Occurred()) {
if (v >= 0) {
if (val) *val = v;
return SWIG_CastRank(SWIG_OK);
return SWIG_AddCast(SWIG_OK);
} else {
return SWIG_OverflowError;
}
@ -249,7 +249,7 @@ SWIG_AsVal_dec(unsigned long long)(PyObject *obj, unsigned long long *val)
}
if (!dispatch) {
double d;
int res = SWIG_CastRank(SWIG_AsVal(double)(obj,&d));
int res = SWIG_AddCast(SWIG_AsVal(double)(obj,&d));
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, 0, ULLONG_MAX)) {
if (val) *val = (unsigned long)(d);
return res;
@ -292,7 +292,7 @@ SWIG_AsVal_dec(double)(PyObject *obj, double *val)
double d = PyFloat_AsDouble(obj);
if (!PyErr_Occurred()) {
if (val) *val = d;
return SWIG_CastRank(SWIG_OK);
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
@ -300,7 +300,7 @@ SWIG_AsVal_dec(double)(PyObject *obj, double *val)
long v = PyInt_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_CastRank(SWIG_CastRank(SWIG_OK));
return SWIG_AddCast(SWIG_AddCast(SWIG_OK));
} else {
PyErr_Clear();
}

View file

@ -15,6 +15,7 @@
#define SWIG_ConvertPtr(obj, pptr, type, flags) SWIG_Python_ConvertPtr(obj, pptr, type, flags)
#define SWIG_ConvertPtrAndOwn(obj,pptr,type,flags,own) SWIG_Python_ConvertPtrAndOwn(obj, pptr, type, flags, own)
#define SWIG_NewPointerObj(ptr, type, flags) SWIG_Python_NewPointerObj(ptr, type, flags)
#define SWIG_CheckImplicit(ty) SWIG_Python_CheckImplicit(ty)
#define SWIG_AcquirePtr(ptr, src) SWIG_Python_AcquirePtr(ptr, src)
#define swig_owntype int
@ -41,6 +42,13 @@
#define SWIG_SetModule(clientdata, pointer) SWIG_Python_SetModule(pointer)
#define SWIG_NewClientData(obj) PySwigClientData_New(obj)
/* A functor is a function object with one single object argument */
#if PY_VERSION_HEX >= 0x02020000
#define SWIG_Python_CallFunctor(functor, obj) PyObject_CallFunctionObjArgs(functor, obj, NULL);
#else
#define SWIG_Python_CallFunctor(functor, obj) PyObject_CallFunction(functor, "O", obj);
#endif
/* Error manipulation */
#define SWIG_SetErrorObj(type, obj) {SWIG_PYTHON_THREAD_BEGIN_BLOCK; PyErr_SetObject(type, obj); SWIG_PYTHON_THREAD_END_BLOCK; }
#define SWIG_SetErrorMsg(type, msg) {SWIG_PYTHON_THREAD_BEGIN_BLOCK; PyErr_SetString(type, msg); SWIG_PYTHON_THREAD_END_BLOCK; }
@ -48,6 +56,7 @@
#define SWIG_Error(code, msg) SWIG_SetErrorMsg(SWIG_Python_ErrorType(code), msg)
#define SWIG_fail goto fail
/*
Helper for static pointer initialization for both C and C++ code, for example
static PyObject *SWIG_STATIC_POINTER(MyVar) = NewSomething(...);
@ -63,8 +72,10 @@
* ----------------------------------------------------------------------------- */
/* Flags for new pointer objects */
#define SWIG_POINTER_NOSHADOW SWIG_POINTER_OWN << 1
#define SWIG_POINTER_NEW SWIG_POINTER_NOSHADOW | SWIG_POINTER_OWN
#define SWIG_POINTER_NOSHADOW (SWIG_POINTER_OWN << 1)
#define SWIG_POINTER_NEW (SWIG_POINTER_NOSHADOW | SWIG_POINTER_OWN)
#define SWIG_POINTER_IMPLICIT_CONV (SWIG_POINTER_DISOWN << 1)
/* For backward compatibility only */
#define SWIG_POINTER_EXCEPTION 0
@ -175,8 +186,17 @@ typedef struct {
PyObject *newargs;
PyObject *destroy;
int delargs;
int implicitconv;
} PySwigClientData;
SWIGRUNTIME int
SWIG_Python_CheckImplicit(swig_type_info *ty)
{
PySwigClientData *data = (PySwigClientData *)ty->clientdata;
return data ? data->implicitconv : 0;
}
SWIGRUNTIME PySwigClientData *
PySwigClientData_New(PyObject* obj)
{
@ -222,6 +242,7 @@ PySwigClientData_New(PyObject* obj)
} else {
data->delargs = 0;
}
data->implicitconv = 0;
return data;
}
}
@ -358,11 +379,7 @@ PySwigObject_dealloc(PyObject *v)
if (data->delargs) {
/* we need to create a temporal object to carry the destroy operation */
PyObject *tmp = PySwigObject_New(sobj->ptr, ty, 0);
#if PY_VERSION_HEX >= 0x02020000
res = PyObject_CallFunctionObjArgs(destroy, tmp, NULL);
#else
res = PyObject_CallFunction(destroy, "O", tmp);
#endif
res = SWIG_Python_CallFunctor(destroy, tmp);
Py_DECREF(tmp);
} else {
PyCFunction meth = PyCFunction_GET_FUNCTION(destroy);
@ -902,7 +919,7 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int
if (!tc) {
sobj = (PySwigObject *)sobj->next;
} else {
*ptr = SWIG_TypeCast(tc,vptr);
if (ptr) *ptr = SWIG_TypeCast(tc,vptr);
break;
}
}
@ -918,7 +935,42 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int
}
return SWIG_OK;
} else {
return SWIG_ERROR;
int res = SWIG_ERROR;
if (flags & SWIG_POINTER_IMPLICIT_CONV) {
PySwigClientData *data = ty ? (PySwigClientData *) ty->clientdata : 0;
if (data && !data->implicitconv) {
PyObject *klass = data->klass;
if (klass) {
PyObject *impconv;
data->implicitconv = 1; /* avoid recursion and call 'explicit' constructors*/
impconv = SWIG_Python_CallFunctor(klass, obj);
data->implicitconv = 0;
if (PyErr_Occurred()) {
PyErr_Clear();
impconv = 0;
}
if (impconv) {
PySwigObject *sobj = SWIG_Python_GetSwigThis(impconv);
if (sobj) {
void *vptr;
res = SWIG_Python_ConvertPtrAndOwn((PyObject*)sobj, &vptr, ty, 0, 0);
if (SWIG_IsOK(res)) {
if (ptr) {
*ptr = vptr;
/* transfer the ownership to 'ptr' */
sobj->own = 0;
res = SWIG_AddNewMask(SWIG_AddCast(res));
} else {
res = SWIG_AddCast(res);
}
}
}
Py_DECREF(impconv);
}
}
}
}
return res;
}
}
}

View file

@ -40,7 +40,7 @@ namespace swig {
static int asptr(PyObject *obj, Type **val) {
Type *p;
int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0) == SWIG_OK) ? SWIG_OLDOBJ : 0;
if (res) {
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
@ -57,17 +57,21 @@ namespace swig {
static int asval(PyObject *obj, Type *val) {
if (val) {
Type *p = 0;
int res = traits_asptr<Type>::asptr(obj, (val ? &p : 0));
if ((res != 0) && p) {
int res = traits_asptr<Type>::asptr(obj, &p);
if (!SWIG_IsOK(res)) return res;
if (p) {
typedef typename noconst_traits<Type>::noconst_type noconst_type;
*(const_cast<noconst_type*>(val)) = *p;
if (res == SWIG_NEWOBJ) %delete(p);
return SWIG_OK;
if (SWIG_IsNewObj(res)){
%delete(p);
res = SWIG_DelNewMask(res);
}
return res;
} else {
return SWIG_ERROR;
}
} else {
return traits_asptr<Type>::asptr(obj, (Type **)(0)) ? SWIG_OK : SWIG_ERROR;
return traits_asptr<Type>::asptr(obj, (Type **)(0));
}
}
};
@ -77,15 +81,13 @@ namespace swig {
if (val) {
typedef typename noconst_traits<Type>::noconst_type noconst_type;
noconst_type *p = 0;
int res = traits_asptr<noconst_type>::asptr(obj, (val ? &p : 0));
if (res) {
int res = traits_asptr<noconst_type>::asptr(obj, &p);
if (SWIG_IsOK(res)) {
*(const_cast<noconst_type**>(val)) = p;
return SWIG_OK;
} else {
return SWIG_ERROR;
}
return res;
} else {
return traits_asptr<Type>::asptr(obj, (Type **)(0)) ? SWIG_OK : SWIG_ERROR;
return traits_asptr<Type>::asptr(obj, (Type **)(0));
}
}
};
@ -113,9 +115,9 @@ namespace swig {
struct traits_as<Type, pointer_category> {
static Type as(PyObject *obj, bool throw_error) {
Type *v = 0;
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : 0);
if (res && v) {
if (res == SWIG_NEWOBJ) {
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
if (SWIG_IsOK(res) && v) {
if (SWIG_IsNewObj(res)) {
Type r(*v);
%delete(v);
return r;
@ -139,8 +141,8 @@ namespace swig {
struct traits_as<Type*, pointer_category> {
static Type* as(PyObject *obj, bool throw_error) {
Type *v = 0;
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : 0);
if (res) {
int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
if (SWIG_IsOK(res)) {
return v;
} else {
if (!PyErr_Occurred()) {
@ -215,7 +217,7 @@ namespace swig {
typedef Type value_type;
static int asval(PyObject *obj, value_type *val) {
if (Check(obj)) {
*val = As(obj);
if (val) *val = As(obj);
return SWIG_OK;
}
return SWIG_ERROR;

View file

@ -41,6 +41,10 @@
#define SWIG_Object PyObject *
#define VOID_Object SWIG_Py_Void()
/* Python allows implicit conversion */
#define %implicitconv_flag $implicitconv
/* Overload of the output/constant/exception/dirout handling */
/* append output */
@ -63,6 +67,7 @@
SWIG_fail %enddef
/* Include the unified typemap library */
%include <typemaps/swigtypemaps.swg>

View file

@ -152,11 +152,9 @@ These methods "may be called" if needed.
/* ------------------------------------------------------------------------- */
/*
Directors
Implicit Conversion using the C++ constructor mechanism
*/
#define %director %feature("director")
#define %nodirector %feature("director","0")
#define %cleardirector %feature("director","")
#define %implicitconv %feature("implicitconv")
#define %noimplicitconv %feature("implicitconv", "0")
#define %clearimplicitconv %feature("implicitconv", "")

View file

@ -17,22 +17,20 @@
{
if (val) {
T *pfirst = &(val->first);
int res1 = swig::asval((PyObject*)first, pfirst);
if (!SWIG_IsOK(res1)) return res1;
U *psecond = &(val->second);
if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK)
&& (swig::asval((PyObject*)second, psecond) == SWIG_OK)) {
return SWIG_OK;
} else {
return SWIG_ERROR;
}
int res2 = swig::asval((PyObject*)second, psecond);
if (!SWIG_IsOK(res2)) return res2;
return res1 > res2 ? res1 : res2;
} else {
T *pfirst = 0;
int res1 = swig::asval((PyObject*)first, 0);
if (!SWIG_IsOK(res1)) return res1;
U *psecond = 0;
if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK)
&& (swig::asval((PyObject*)second, psecond) == SWIG_OK)) {
return SWIG_OK;
} else {
return SWIG_ERROR;
}
int res2 = swig::asval((PyObject*)second, psecond);
if (!SWIG_IsOK(res2)) return res2;
return res1 > res2 ? res1 : res2;
}
}
@ -50,13 +48,8 @@
}
} else {
value_type *p;
if (SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0) == SWIG_OK) {
if (val) *val = *p;
res = SWIG_OK;
}
}
if ((res == SWIG_ERROR) && val) {
PyErr_Format(PyExc_TypeError, "a %s is expected", swig::type_name<value_type>());
res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0);
if (SWIG_IsOK(res) && val) *val = *p;
}
return res;
}
@ -73,29 +66,26 @@
if (val) {
value_type *vp = %new_instance(std::pair<T,U>);
T *pfirst = &(vp->first);
int res1 = swig::asval((PyObject*)first, pfirst);
if (!SWIG_IsOK(res1)) return res1;
U *psecond = &(vp->second);
if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK)
&& (swig::asval((PyObject*)second, psecond) == SWIG_OK)) {
*val = vp;
return SWIG_NEWOBJ;
} else {
%delete(vp);
return SWIG_BADOBJ;
}
int res2 = swig::asval((PyObject*)second, psecond);
if (!SWIG_IsOK(res2)) return res2;
*val = vp;
return SWIG_AddNewMask(res1 > res2 ? res1 : res2);
} else {
T *pfirst = 0;
int res1 = swig::asval((PyObject*)first, pfirst);
if (!SWIG_IsOK(res1)) return res1;
U *psecond = 0;
if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK)
&& (swig::asval((PyObject*)second, psecond) == SWIG_OK)) {
return SWIG_NEWOBJ;
} else {
return SWIG_BADOBJ;
}
int res2 = swig::asval((PyObject*)second, psecond);
if (!SWIG_IsOK(res2)) return res2;
return res1 > res2 ? res1 : res2;
}
}
static int asptr(PyObject *obj, std::pair<T,U> **val) {
int res = SWIG_BADOBJ;
int res = SWIG_ERROR;
if (PyTuple_Check(obj)) {
if (PyTuple_GET_SIZE(obj) == 2) {
res = get_pair(PyTuple_GET_ITEM(obj,0),PyTuple_GET_ITEM(obj,1), val);
@ -108,13 +98,8 @@
}
} else {
value_type *p;
if (SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0) == SWIG_OK) {
if (val) *val = p;
res = SWIG_OLDOBJ;
}
}
if ((res == SWIG_BADOBJ) && val) {
PyErr_Format(PyExc_TypeError, "a %s is expected", swig::type_name<value_type>());
res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0);
if (SWIG_IsOK(res) && val) *val = p;
}
return res;
}