Quantcast

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>

#ifdef ABSTRACT_CLASSES_DLL_EXPORT
    #define ABSTRACT_CLASSES_DLL_API __declspec(dllexport)
#else
    #define ABSTRACT_CLASSES_DLL_API __declspec(dllimport)
#endif

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

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

class ABSTRACT_CLASSES_DLL_API CContainer {
    private:
        static CContainer *pSingletonContainer;
        CConcreteChild Bar;
       
    public:
        CContainer();
        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() {
    $r=CContainer_GetBar($this->_cPtr);
    $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.

P.S.
My environment is SWIG 3.0.10 on Windows 7.

P.P.S.
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]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...