add support for more 'expressive' result values, which now can carry the old OK/ERROR state, and also the cast rank and the New/Tmp masks

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8094 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-27 22:04:58 +00:00
commit e4e72e63fa

View file

@ -47,28 +47,142 @@
/* Flags for new pointer objects */
#define SWIG_POINTER_OWN 0x1
/* Alloc. memory flags */
#define SWIG_BADOBJ 0
#define SWIG_OLDOBJ 1
#define SWIG_NEWOBJ 2
/*
Flags/methods for returning states.
The swig conversion methods, as ConvertPtr, return and integer
that tells if the conversion was successful or not. And if not,
an error code can be returned (see swigerrors.swg for the codes).
Use the following macros/flags to set or process the returning
states.
In old swig versions, you usually write code as:
if (SWIG_ConvertPtr(obj,vptr,ty.flags) != -1) {
// success code
} else {
//fail code
}
Now you can be more explicit as:
int res = SWIG_ConvertPtr(obj,vptr,ty.flags);
if (SWIG_IsOK(res)) {
// success code
} else {
// fail code
}
that seems to be the same, but now you can also do
Type *ptr;
int res = SWIG_ConvertPtr(obj,(void **)(&ptr),ty.flags);
if (SWIG_IsOK(res)) {
// success code
if (SWIG_IsNewObj(res) {
...
delete *ptr;
} else {
...
}
} else {
// fail code
}
I.e., now SWIG_ConvertPtr can return new objects and you can
identify the case and take care of the deallocation. Of course that
requires also to SWIG_ConvertPtr to return new result values, as
int SWIG_ConvertPtr(obj, ptr,...) {
if (<obj is ok>) {
if (<need new object>) {
*ptr = <ptr to new allocated object>;
return SWIG_NEWOBJ;
} else {
*ptr = <ptr to old object>;
return SWIG_OLDOBJ;
}
} else {
return SWIG_BADOBJ;
}
}
Of course, returning the plain '0(success)/-1(fail)' still works, but you can be
more explicit by returning SWIG_BADOBJ, SWIG_ERROR or any of the
swig errors code.
Finally, if the SWIG_CASTRANK_MODE is enabled, the result code
allows to return the 'cast rank', for example, if you have this
int food(double)
int fooi(int);
and you call
food(1) // cast rank '1' (1 -> 1.0)
fooi(1) // cast rank '0'
just use the SWIG_AddCast()/SWIG_CheckState()
/* Flags for returning states */
#define SWIG_OK 0
#define SWIG_ERROR -1
*/
#define SWIG_OK (0)
#define SWIG_ERROR (-1)
#define SWIG_IsOK(r) (r >= 0)
/* Flags to manage cast and return states */
#ifndef SWIG_TypeRank
# define SWIG_TypeRank unsigned long
#endif
#ifndef SWIG_MAX_CAST_RANK
# define SWIG_MAX_CAST_RANK 2 /* Two cast allowed */
#endif
#define SWIG_IsOK(r) (r >= 0)
SWIGINTERNINLINE int
SWIG_CastRank(int r) {
return (SWIG_IsOK(r)) ? ((r < SWIG_MAX_CAST_RANK) ? (r + 1) : SWIG_ERROR) : r;
/* The CastRankLimit says how many bits are used for the cast rank */
#define SWIG_CASTRANKLIMIT (1 << 8)
/* The NewMask denotes the object was created (using new/malloc) */
#define SWIG_NEWOBJMASK (SWIG_CASTRANKLIMIT << 1)
/* The TmpMask is for in/out typemaps that use temporal objects */
#define SWIG_TMPOBJMASK (SWIG_NEWOBJMASK << 1)
/* Simple returning values */
#define SWIG_BADOBJ (SWIG_ERROR)
#define SWIG_OLDOBJ (SWIG_OK)
#define SWIG_NEWOBJ (SWIG_OK | SWIG_NEWOBJMASK)
#define SWIG_TMPOBJ (SWIG_OK | SWIG_TMPOBJMASK)
/* Check, add and del mask methods */
#define SWIG_AddNewMask(r) (SWIG_IsOK(r) ? (r | SWIG_NEWOBJMASK) : r)
#define SWIG_DelNewMask(r) (SWIG_IsOK(r) ? (r & ~SWIG_NEWOBJMASK) : r)
#define SWIG_IsNewObj(r) (SWIG_IsOK(r) && (r & SWIG_NEWOBJMASK))
#define SWIG_AddTmpMask(r) (SWIG_IsOK(r) ? (r | SWIG_TMPOBJMASK) : r)
#define SWIG_DelTmpMask(r) (SWIG_IsOK(r) ? (r & ~SWIG_TMPOBJMASK) : r)
#define SWIG_IsTmpObj(r) (SWIG_IsOK(r) && (r & SWIG_TMPOBJMASK))
/* Cast-Rank Mode */
#if defined(SWIG_CASTRANK_MODE)
# ifndef SWIG_TypeRank
# define SWIG_TypeRank unsigned long
# endif
# ifndef SWIG_MAXCASTRANK /* Default cast allowed */
# define SWIG_MAXCASTRANK (2)
# endif
# define SWIG_CASTRANKMASK ((SWIG_CASTRANKLIMIT) -1)
# define SWIG_CastRank(r) (r & SWIG_CASTRANKMASK)
SWIGINTERNINLINE int SWIG_AddCast(int r) {
return SWIG_IsOK(r) ? ((SWIG_CastRank(r) < SWIG_MAXCASTRANK) ? (r + 1) : SWIG_ERROR) : r;
}
SWIGINTERNINLINE int SWIG_CheckState(int r) {
#if 1
if (SWIG_IsOK(r)) {
int rank = SWIG_CastRank(r);
return rank + 1;
} else {
return 0;
}
#else
return SWIG_IsOK(r) ? SWIG_CastRank(r) + 1 : 0;
#endif
}
#else /* no cast-rank mode */
#define SWIG_AddCast
#define SWIG_CheckState(r) (SWIG_IsOK(r) ? 1 : 0)
#endif
#include <string.h>