[mapguide-commits] r9942 - sandbox/jng/vanilla_swig/Bindings/src/SwigCommon/Php

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Thu May 26 04:07:02 PDT 2022


Author: jng
Date: 2022-05-26 04:07:01 -0700 (Thu, 26 May 2022)
New Revision: 9942

Modified:
   sandbox/jng/vanilla_swig/Bindings/src/SwigCommon/Php/pointer.i
Log:
Do not apply most-derived-type checking for MgCoordinate* out typemap. PHP type checking in the SWIG wrapper is exact and does not take inheritance into account. This means we cannot pass a MgCoordinateXY to a method that takes a parameter of type MgCoordinate despite MgCoordinateXY being derived from MgCoordinate. This appears to be the only type that requires such treatment

Modified: sandbox/jng/vanilla_swig/Bindings/src/SwigCommon/Php/pointer.i
===================================================================
--- sandbox/jng/vanilla_swig/Bindings/src/SwigCommon/Php/pointer.i	2022-05-23 13:10:18 UTC (rev 9941)
+++ sandbox/jng/vanilla_swig/Bindings/src/SwigCommon/Php/pointer.i	2022-05-26 11:07:01 UTC (rev 9942)
@@ -5,15 +5,21 @@
  */
 
 ///////////////////////////////////////////////////////////
-// Custom generic pointer typemap. This overrides the default
-// typemap to support downcasting
+// Custom generic pointer typemaps. This overrides the default
+// typemap to support downcasting and/or hooking into our refcounting
+// diagnostics
 //
+
+
+// Default typemap is to try for the most-derived type
+//
+// TODO: We can avoid the most-derived type check if we know SWIGTYPE to not
+// be an abstract class. Is there a SWIG-typemap-level way to check for this?
 %typemap(out) SWIGTYPE* 
 {
     swig_type_info* ty = NULL;
-    if ($1)
+    if ($1) // Try to resolve most-derived type through its class id
     {
-        //TODO: Can we get away with not having to do this (ie. Just use $1_descriptor)?
         const char* retClassName = ResolveMgClassName(static_cast<MgObject*>($1)->GetClassId());
         if (NULL != retClassName)
         {
@@ -29,4 +35,22 @@
         NewObjectHook($1);
     else
         RefCountHook($1);
+}
+
+// But for certain return types, we want to preserve its base-type-ness, otherwise
+// we can't pass said instances as arguments to methods that are expecting that base
+// type (because the PHP type checking in the SWIG wrapper is by exact type and isn't 
+// aware of inheritance, so you can't pass a MgCoordinateXY to an argument expecting
+// MgCoordinate for example). Also said types are "pure abstract" and the consumer will
+// almost never need downcast to the most-derived type.
+//
+// For now, MgCoordinate is such a return type
+%typemap(out) MgCoordinate* 
+{
+    swig_type_info* ty = $1_descriptor;
+    SWIG_SetPointerZval($result, (void *)$1, ty, 1); /* owner = $owner */
+    if ($owner) // is owner?
+        NewObjectHook($1);
+    else
+        RefCountHook($1);
 }
\ No newline at end of file



More information about the mapguide-commits mailing list