Wrapping abstract C++ class in PHP

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Wrapping abstract C++ class in PHP

Keith Currier
I am trying to use SWIG to access some C++ classes from PHP.  It appears to me that SWIG is not properly handling the case where a C++ method returns a pointer to a C++ class that is abstract.

Here is my example C++ header:

// Trivial example of a C++ container returning a pointer to an abstract class
#include <string>

    #define ABSTRACT_CLASSES_DLL_API __declspec(dllexport)
    #define ABSTRACT_CLASSES_DLL_API __declspec(dllimport)

class ABSTRACT_CLASSES_DLL_API CAbstractParent {
        virtual std::string GetFoo() = 0;

class ABSTRACT_CLASSES_DLL_API CConcreteChild : public CAbstractParent {
        std::string Foo;
        virtual std::string GetFoo();

        static CContainer *pSingletonContainer;
        CConcreteChild Bar;
        static CContainer *GetSingletonContainer();
        CAbstractParent *GetBar();

And here is my SWIG interface file:

/* File : AbstractClassesWrapper.i */
%module AbstractClassesWrapper

#include "AbstractClasses.h"

%include "Windows.i"
%include "std_string.i"
%include "typemaps.i"

%include "AbstractClasses.h"

And here is a snippet of the PHP wrapper code generated by SWIG:

function GetBar() {
    $this->_cPtr = $r;
    return $this;

GetBar() is supposed to return a pointer to a CAbstractParent, but it is actually returning $this, which is a CContainer.  Worse, by first setting $this->_cPtr to the return value from CContainer_GetBar() it has corrupted $this so that it is no longer really a CContainer and it is also not a CAbstractParent.

Is this a bug, or am I misunderstanding something here?  Admittedly, I am very new to SWIG.

My environment is SWIG 3.0.10 on Windows 7.

When I tell SWIG to wrap the same interface file for Java, it behaves correctly.  The CAbstractParent Java class that gets generated is NOT abstract and GetBar() properly instantiates a Java CAbstractParent object to return.

What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. http://sdm.link/zohodev2dev
Swig-user mailing list
[hidden email]