Wrapping abstract class

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

Wrapping abstract class

Juan Manuel Alvarez
Hi everyone!

I am trying to wrap an abstract class to C#, but I always get the
"cannot instantiate abstract class" error, since SWIG generates a
construction function. I was looking to csinterfaces, but I could not
make it work. I dont know if I am doing wrong or if csinterfaces is
not what I need.

Here is a snippet:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
myclass.i
-----------

%typemap(csinterfaces) myclass "myclass"
%{
#include "myclass.h"
%}

class myclass
{
public::
         void doSomething();

         virtual void myFunc() = 0;

};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The C++ class interface is the same

Can anyone point me in the right direction?

Thanks in advance!

Juan Manuel

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapping abstract class

Rocco Moretti
Juan Manuel Alvarez wrote:
> Hi everyone!
>
> I am trying to wrap an abstract class to C#, but I always get the
> "cannot instantiate abstract class" error, since SWIG generates a
> construction function.

You should be able to turn off automatic constructor generation with the
%nodefaultctor feature flag directive. See the manual:
http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIGPlus_nn8

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapping abstract class

stefano ruberto
Juan Manuel Alvarez wrote:

>> Hi everyone!
>>
>> I am trying to wrap an abstract class to C#, but I always get the
>> "cannot instantiate abstract class" error, since SWIG generates a
>> construction function.
>>    
>
> You should be able to turn off automatic constructor generation with the
> %nodefaultctor feature flag directive. See the manual:
> http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIGPlus_nn8
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save $100.
> Use priority code J8TL2D2.
> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>  
I'm trying to solve the same situation..
..the problem is tha you don't have the body of the pure virtual
function .. so eliminating the constructor is not a solution ... I think
...( or I'm wrong ? )...swig  translate the abstract class as a usual
class  ( not interface! ) and try to instantiate it anyway... so you get
that error ... or is there  some news on that?

anyway in the manul page you read .. "Default constructors are not
generated for classes with pure virtual methods or for classes that
inherit from an abstract class, but don't provide definitions for all of
the pure methods."...

Hope this help
Rubio



-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapping abstract class

William S Fulton
Rubyo wrote:
> Juan Manuel Alvarez wrote:
>>> Hi everyone!
>>>
>>> I am trying to wrap an abstract class to C#, but I always get the
>>> "cannot instantiate abstract class" error, since SWIG generates a
>>> construction function.
>>>    
This shouldn't happen by default. Please show a minimal example
demonstrating the problem if you want any more advice.

>> You should be able to turn off automatic constructor generation with the
>> %nodefaultctor feature flag directive. See the manual:
>> http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIGPlus_nn8
>>
> I'm trying to solve the same situation..
> ..the problem is tha you don't have the body of the pure virtual
> function .. so eliminating the constructor is not a solution ... I think
> ...( or I'm wrong ? )...swig  translate the abstract class as a usual
> class  ( not interface! ) and try to instantiate it anyway... so you get
> that error ... or is there  some news on that?
>
> anyway in the manul page you read .. "Default constructors are not
> generated for classes with pure virtual methods or for classes that
> inherit from an abstract class, but don't provide definitions for all of
> the pure methods."...
>

Stefano, I have an example of how to wrap abstract base classes which
I've pasted below. It is for Java, but with a bit of modification can be
made to work for C#. Just modify the typemap names as described in the
CSharp.html documentation, eg javabody->csbody. The example shows how
multiple inheritance can be made to work, even though there is a lot of
swig machinery to write. As Zhiyang Jiang mentioned in another thread,
he might be able to modify swig so that just one line of code can
replace all of the typemaps and features below.

William

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

// Turn the proxy class into an interface
%typemap(javaclassmodifiers) IRemoteSyncIO "interface";
%typemap(javaclassmodifiers) IRemoteAsyncIO "interface";
%typemap(javabody) IRemoteSyncIO "";
%typemap(javabody) IRemoteAsyncIO "";
%typemap(javafinalize) IRemoteSyncIO "";
%typemap(javafinalize) IRemoteAsyncIO "";
%typemap(javadestruct) IRemoteSyncIO "";
%typemap(javadestruct) IRemoteAsyncIO "";

// Turn the methods into abstract methods
%javamethodmodifiers IRemoteSyncIO::syncmethod "abstract public";
%javamethodmodifiers IRemoteAsyncIO::asyncmethod "abstract public";
%typemap(javaout) void IRemoteSyncIO::syncmethod ";"
%typemap(javaout) void IRemoteAsyncIO::asyncmethod ";"

// Features are inherited by derived classes, so override this
%javamethodmodifiers RemoteMpe::syncmethod "public"
%javamethodmodifiers RemoteMpe::asyncmethod "public"

// Modify multiple inherited base classes into inheriting interfaces
%typemap(javainterfaces) RemoteMpe "IRemoteSyncIO, IRemoteAsyncIO";
%typemap(javabase, replace="1") RemoteMpe "";


%inline %{
class IRemoteSyncIO
{
public:
   virtual ~IRemoteSyncIO () {}
   virtual void syncmethod() = 0;
protected:
   IRemoteSyncIO () {}

private:
   IRemoteSyncIO (const IRemoteSyncIO&);
   IRemoteSyncIO& operator= (const IRemoteSyncIO&);
};

class IRemoteAsyncIO
{
public:
   virtual ~IRemoteAsyncIO () {}
   virtual void asyncmethod() = 0;
protected:
   IRemoteAsyncIO () {}

private:
   IRemoteAsyncIO (const IRemoteAsyncIO&);
   IRemoteAsyncIO& operator= (const IRemoteAsyncIO&);
};

class RemoteMpe : public IRemoteSyncIO, public IRemoteAsyncIO
{
public:
   virtual void syncmethod() {}
   virtual void asyncmethod() {}
};

%}


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapping abstract class

stefano ruberto
William S Fulton ha scritto:

> Rubyo wrote:
>> Juan Manuel Alvarez wrote:
>>>> Hi everyone!
>>>>
>>>> I am trying to wrap an abstract class to C#, but I always get the
>>>> "cannot instantiate abstract class" error, since SWIG generates a
>>>> construction function.    
> This shouldn't happen by default. Please show a minimal example
> demonstrating the problem if you want any more advice.
>
>>> You should be able to turn off automatic constructor generation with
>>> the
>>> %nodefaultctor feature flag directive. See the manual:
>>> http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIGPlus_nn8
>>>
>> I'm trying to solve the same situation..
>> ..the problem is tha you don't have the body of the pure virtual
>> function .. so eliminating the constructor is not a solution ... I
>> think ...( or I'm wrong ? )...swig  translate the abstract class as a
>> usual class  ( not interface! ) and try to instantiate it anyway...
>> so you get that error ... or is there  some news on that?
>>
>> anyway in the manul page you read .. "Default constructors are not
>> generated for classes with pure virtual methods or for classes that
>> inherit from an abstract class, but don't provide definitions for all
>> of the pure methods."...
>>
>
> Stefano, I have an example of how to wrap abstract base classes which
> I've pasted below. It is for Java, but with a bit of modification can
> be made to work for C#. Just modify the typemap names as described in
> the CSharp.html documentation, eg javabody->csbody. The example shows
> how multiple inheritance can be made to work, even though there is a
> lot of swig machinery to write. As Zhiyang Jiang mentioned in another
> thread, he might be able to modify swig so that just one line of code
> can replace all of the typemaps and features below.
>
> William
>
> /* File : example.i */
> %module example
>
> // Turn the proxy class into an interface
> %typemap(javaclassmodifiers) IRemoteSyncIO "interface";
> %typemap(javaclassmodifiers) IRemoteAsyncIO "interface";
> %typemap(javabody) IRemoteSyncIO "";
> %typemap(javabody) IRemoteAsyncIO "";
> %typemap(javafinalize) IRemoteSyncIO "";
> %typemap(javafinalize) IRemoteAsyncIO "";
> %typemap(javadestruct) IRemoteSyncIO "";
> %typemap(javadestruct) IRemoteAsyncIO "";
>
> // Turn the methods into abstract methods
> %javamethodmodifiers IRemoteSyncIO::syncmethod "abstract public";
> %javamethodmodifiers IRemoteAsyncIO::asyncmethod "abstract public";
> %typemap(javaout) void IRemoteSyncIO::syncmethod ";"
> %typemap(javaout) void IRemoteAsyncIO::asyncmethod ";"
>
> // Features are inherited by derived classes, so override this
> %javamethodmodifiers RemoteMpe::syncmethod "public"
> %javamethodmodifiers RemoteMpe::asyncmethod "public"
>
> // Modify multiple inherited base classes into inheriting interfaces
> %typemap(javainterfaces) RemoteMpe "IRemoteSyncIO, IRemoteAsyncIO";
> %typemap(javabase, replace="1") RemoteMpe "";
>
>
> %inline %{
> class IRemoteSyncIO
> {
> public:
>   virtual ~IRemoteSyncIO () {}
>   virtual void syncmethod() = 0;
> protected:
>   IRemoteSyncIO () {}
>
> private:
>   IRemoteSyncIO (const IRemoteSyncIO&);
>   IRemoteSyncIO& operator= (const IRemoteSyncIO&);
> };
>
> class IRemoteAsyncIO
> {
> public:
>   virtual ~IRemoteAsyncIO () {}
>   virtual void asyncmethod() = 0;
> protected:
>   IRemoteAsyncIO () {}
>
> private:
>   IRemoteAsyncIO (const IRemoteAsyncIO&);
>   IRemoteAsyncIO& operator= (const IRemoteAsyncIO&);
> };
>
> class RemoteMpe : public IRemoteSyncIO, public IRemoteAsyncIO
> {
> public:
>   virtual void syncmethod() {}
>   virtual void asyncmethod() {}
> };
>
> %}
>
Thank you William,
                                I'm studing this code in my use
case...(soon some try)
Obviously Zhiyang Jiang's work will be greatly appreciated...

Stefano






-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wrapping abstract class

fasna
This post has NOT been accepted by the mailing list yet.
Hi,
did u tried this in C# ..please do post the way
Loading...