Overload resolution now works in PHP out of the box.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9305 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2006-09-20 06:59:07 +00:00
commit 5baafd12c0
6 changed files with 28 additions and 20 deletions

View file

@ -1,6 +1,14 @@
Version 1.3.30 (in progress) Version 1.3.30 (in progress)
============================ ============================
09/20/2006: olly
*** POTENTIAL INCOMPATIBILITY ***
[php] Overload resolution now works. However to allow this, SWIG
generated wrappers no longer coerce PHP types (which reverts a change
made in 1.3.26). So for example, if a method takes a string, you
can no longer pass a number without explicitly converting it to a
string in PHP using: (string)x
09/20/2006: olly 09/20/2006: olly
[php] Previously -noproxy was required when wrapping some C code [php] Previously -noproxy was required when wrapping some C code
to avoid SWIG generating new_XXX methods. Now proxy classes are to avoid SWIG generating new_XXX methods. Now proxy classes are

View file

@ -42,6 +42,10 @@ char *overloaded(double d) {
return "Overloaded with double"; return "Overloaded with double";
} }
char *overloaded(const char * str) {
return "Overloaded with char *";
}
char *overloaded( const Circle& ) { char *overloaded( const Circle& ) {
return "Overloaded with Circle"; return "Overloaded with Circle";
} }

View file

@ -9,7 +9,7 @@ public:
} }
virtual ~Shape() { virtual ~Shape() {
nshapes--; nshapes--;
}; }
double x, y; double x, y;
void move(double dx, double dy); void move(double dx, double dy);
virtual double area(void) = 0; virtual double area(void) = 0;
@ -22,8 +22,8 @@ class Circle : public Shape {
private: private:
double radius; double radius;
public: public:
Circle(double r) : radius(r) { }; Circle(double r) : radius(r) { }
~Circle() { }; ~Circle() { }
virtual double area(void); virtual double area(void);
virtual double perimeter(void); virtual double perimeter(void);
}; };
@ -32,7 +32,7 @@ class Square : public Shape {
private: private:
double width; double width;
public: public:
Square(double w) : width(w) { }; Square(double w) : width(w) { }
~Square() { } ~Square() { }
virtual double area(void); virtual double area(void);
virtual double perimeter(void); virtual double perimeter(void);
@ -40,6 +40,7 @@ public:
char *overloaded( int i ); char *overloaded( int i );
char *overloaded( double d ); char *overloaded( double d );
char *overloaded( const char * str );
char *overloaded( const Circle& ); char *overloaded( const Circle& );
char *overloaded( const Shape& ); char *overloaded( const Shape& );

View file

@ -36,9 +36,9 @@ print " Square = (" . $s->x . "," . $s->y . ")\n";
# ----- Call some methods ----- # ----- Call some methods -----
print "\nHere are some properties of the shapes:\n"; print "\nHere are some properties of the shapes:\n";
foreach (array(1,2.1,"3.6",$c,$s) as $o) { foreach (array(1, 2.1, "quick brown fox", $c, $s) as $o) {
print " ".get_class($o)." $o\n"; print " ".get_class($o)." $o\n";
print " overloaded= " . overloaded($o) . "\n"; print " overloaded = " . overloaded($o) . "\n";
} }
# Need to unset($o) or else we hang on to a reference to the Square object. # Need to unset($o) or else we hang on to a reference to the Square object.

View file

@ -229,17 +229,12 @@
RETVAL_STRINGL($1,$1_dim0,1); RETVAL_STRINGL($1,$1_dim0,1);
} }
// This typecheck would do hard checking for proper argument type. // This typecheck does hard checking for proper argument type. If you want
//%define %php_typecheck(_type,_prec,is) // an argument to be converted from a different PHP type, you must convert
//%typemap(typecheck,precedence=_prec) _type // it yourself before passing it (e.g. (string)4.7 or (int)"6").
// " $1 = Z_TYPE_PP($input) == is ? 1 : 0; "
//%enddef
%define %php_typecheck(_type,_prec,is) %define %php_typecheck(_type,_prec,is)
%typemap(typecheck,precedence=_prec) _type %typemap(typecheck,precedence=_prec) _type
" $1 = (Z_TYPE_PP($input) == IS_LONG || " $1 = (Z_TYPE_PP($input) == is); "
Z_TYPE_PP($input) == IS_DOUBLE ||
Z_TYPE_PP($input) == IS_STRING) ? 1 : 0; "
%enddef %enddef
%php_typecheck(int,SWIG_TYPECHECK_INTEGER,IS_LONG) %php_typecheck(int,SWIG_TYPECHECK_INTEGER,IS_LONG)
@ -269,11 +264,11 @@
SWIGTYPE & SWIGTYPE &
{ {
void *tmp; void *tmp;
_v = (SWIG_ConvertPtr( *$input, (void**)&tmp, $1_descriptor, 0) < 0)? 0:1; _v = (SWIG_ConvertPtr( *$input, (void**)&tmp, $1_descriptor, 0) >= 0);
} }
%typecheck(SWIG_TYPECHECK_VOIDPTR) void * %typecheck(SWIG_TYPECHECK_VOIDPTR) void *
" /* tyepcheck void * */ " " /* typecheck void * */ "
/* Exception handling */ /* Exception handling */

View file

@ -159,14 +159,14 @@ SWIG_ZTS_ConvertPtr(zval *z, void **ptr, swig_type_info *ty, int flags TSRMLS_DC
if (zend_hash_find(HASH_OF(z),"_cPtr",sizeof("_cPtr"),(void**)&_cPtr)==SUCCESS) { if (zend_hash_find(HASH_OF(z),"_cPtr",sizeof("_cPtr"),(void**)&_cPtr)==SUCCESS) {
if ((*_cPtr)->type==IS_RESOURCE) { if ((*_cPtr)->type==IS_RESOURCE) {
*ptr = SWIG_ZTS_ConvertResourcePtr(*_cPtr, ty, flags TSRMLS_CC); *ptr = SWIG_ZTS_ConvertResourcePtr(*_cPtr, ty, flags TSRMLS_CC);
return (ptr == NULL ? -1 : 0); return (*ptr == NULL ? -1 : 0);
} }
} }
break; break;
} }
case IS_RESOURCE: case IS_RESOURCE:
*ptr = SWIG_ZTS_ConvertResourcePtr(z, ty, flags TSRMLS_CC); *ptr = SWIG_ZTS_ConvertResourcePtr(z, ty, flags TSRMLS_CC);
return (ptr == NULL ? -1 : 0); return (*ptr == NULL ? -1 : 0);
case IS_NULL: case IS_NULL:
*ptr = 0; *ptr = 0;
return 0; return 0;