Problem of directors in C++ -> Csharp

View: New views
2 Messages — Rating Filter:   Alert me  

Problem of directors in C++ -> Csharp

by Giorgio S :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This is a very simple example of the problem I'm having with Swig directors

///////////////////////////
in C++
////////////////////////

class Obj
{
public:
        Obj() {...}
        virtual void fn() {...}
};

class Container
{
        [...]
public:
         Container();
        void insert(Obj* obj);
        Obj* getLastInserted();
};

///////////////////////////
in Swig
///////////////////////////

%feature("director") Obj;

///////////////////////////
in CSharp
///////////////////////////

class CsObj: Obj
{
        CsObj() {...}
        public override void fn() {...}

};

static void Main()
{
        Container c=new Container();
        
        CsObj o1=new CsObj();
        o1.fn(); //OKvirtual is working
        
         c.insert(o1);

        CsObj o2=(CsObj)c.getLastInserted(); //ERROR HERE!!!!
}

I'm sure the object returned by getLastInserted is a CsObj,
but .NET produce an error (impossible to cast).

The strange thing is that the same example is perfecly working in Swig/Python!

Do you have any idea how to solve the problem?
Basically I need to get back the C# class.
Thanks.

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Problem of directors in C++ -> Csharp

by David Piepgrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

This is normal behavior for SWIG C#. The problem is that SWIG has to convert the pointer returned by the getLastInserted function into a C# object, and it has no idea that a C# object already exists, so it creates a wrapper of type Obj to represent the pointer. Note that this problem exists whenever a C++ function returns a derived-class object in a base-class pointer, regardless of whether directors are involved. On the plus side, you can still call the virtual function fn() through the new wrapper. But I don’t know if anyone has developed a workaround for this problem. Sorry.

 

From: Giorgio Scorzelli [mailto:scrgiorgio@...]
Sent: Tuesday, October 06, 2009 11:03 AM
To: swig-user@...
Subject: [Swig-user] Problem of directors in C++ -> Csharp

 

This is a very simple example of the problem I'm having with Swig directors

///////////////////////////
in C++
////////////////////////

class Obj
{
public:
        Obj() {...}
        virtual void fn() {...}
};

class Container
{
        [...]
public:
         Container();
        void insert(Obj* obj);
        Obj* getLastInserted();
};

///////////////////////////
in Swig
///////////////////////////

%feature("director") Obj;

///////////////////////////
in CSharp
///////////////////////////

class CsObj: Obj
{
        CsObj() {...}
        public override void fn() {...}

};

static void Main()
{
        Container c=new Container();
        
        CsObj o1=new CsObj();
        o1.fn(); //OKvirtual is working
        
         c.insert(o1);

        CsObj o2=(CsObj)c.getLastInserted(); //ERROR HERE!!!!
}

I'm sure the object returned by getLastInserted is a CsObj,
but .NET produce an error (impossible to cast).

The strange thing is that the same example is perfecly working in Swig/Python!

Do you have any idea how to solve the problem?
Basically I need to get back the C# class.
Thanks.


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user