Thanks for the test, I quite forgot about it. It revealed a case
to the "Descendant" proxy class (probably using "extend").
should be injected. The simplest place is the parser, but I didn't want to
touch the parser with these language specific changes.
swigPtr. This can be done in the language module at the start of the
class handler.
code.
implemented without applying the patch. I'd like to see proper
I originally asked Zhiyang to focus on first. I would like to see this
2.0.10 is out the door.
Post by Vladimir KalininI finally found some time to implement this feature (only for C# for now)
https://github.com/wkalinin/swig/tree/interfaces
It works as a combination of a feature and a set of typemaps.
Here are 2 versions of the typemaps, w/o namespaces and with them
%define DECLARE_INTERFACE(CTYPE, INTERFACE, IMPL)
%rename (IMPL) CTYPE;
%feature("interface", name = "INTERFACE", cptr = "GetCPtr") CTYPE;
%typemap(cstype) CTYPE*, const CTYPE& "INTERFACE"
%typemap(csdirectorout) CTYPE*, const CTYPE& "$cscall.GetCPtr()"
%typemap(csdirectorin) CTYPE*, const CTYPE&
%{
(INTERFACE)new IMPL($iminput,false)
%}
%typemap(csin) CTYPE*, const CTYPE& "$csinput.GetCPtr()"
%typemap(csout, excode=SWIGEXCODE) CTYPE*, const CTYPE&
{
IMPL ret = new IMPL($imcall,true);
$excode
return (INTERFACE)ret;
}
%enddef
%define DECLARE_INTERFACE_NS(CTYPE, INTERFACE, IMPL, NAMESPACE)
%rename (IMPL) CTYPE;
%feature("interface", name = "INTERFACE", cptr = "GetCPtr") CTYPE;
%typemap(cstype) CTYPE*, const CTYPE& "NAMESPACE.INTERFACE"
%typemap(csdirectorout) CTYPE*, const CTYPE& "$cscall.GetCPtr()"
%typemap(csdirectorin) CTYPE*, const CTYPE&
%{
(NAMESPACE.INTERFACE)new NAMESPACE.IMPL($iminput,false)
%}
%typemap(csin) CTYPE*, const CTYPE& "$csinput.GetCPtr()"
%typemap(csout, excode=SWIGEXCODE) CTYPE*, const CTYPE&
{
NAMESPACE.IMPL ret = new NAMESPACE.IMPL($imcall,true);
$excode
return (NAMESPACE.INTERFACE)ret;
}
%enddef
%rename is optionally used if implementation class should be named
differently from the original class.
These macros are more of a sample/illustration, because e.g. in our
project we use quite different typemaps to implement proper
downcast behaviour.
I attached the sample file I used for testing.
-----Original Message-----
I suppose that's true for Java, but in C# you could use the same name
interface IA { IntPtr getCPtr(); }
interface IB : IA { IntPtr getCPtr(); }
interface IC { IntPtr getCPtr(); }
class Foo : IA, IB, IC {
...
IntPtr IA.getCPtr() { return ???; }
IntPtr IB.getCPtr() { return ???; }
IntPtr IC.getCPtr() { return ???; }
}
Foo foo = ...;
IntPtr aPtr = ((IA)foo).getCPtr();
Explicit interface implementations have the advantage that they are
less visible (interface methods are always public, technically, but they are
invisible on an object of type "Foo" until you cast to IA or IB or IC.)
Note that since these methods are public, there is greater reason to
make it possible to rename them (it is very annoying that opaque pointers
are hardcoded with the name SWIGTYPE_p_Xyz, so I %ignore everything that
would cause an opaque pointer to be produced, or map opaque pointers to
IntPtr instead, through typemaps.)
Date: 03/22/2013 01:25 PM
Subject: Re: [Swig-devel] Abstract base classes to interfaces conversion
Thinking some more about the subject, it turns out that the only
reliable way to obtain valid C pointer from the interface is to extend the
interface with the corresponding function (like "internal IntPtr getCPtr();"
).
It cannot be called just getCPtr(), because if some class implements
several interfaces, it has to implement several "getCPtr" functions.
Interface name may be appended|prepended to the function name to distinguish
them.
Implementation of these functions is the same as that of SwigUPCAST()
-----Original Message-----
I have one suggestion about interface support, if you implement this
feature: it should be possible to wrap a class with BOTH a normal wrapper
AND an interface. That is, don't limit interfaces to abstract base
classes--also allow non-abstract base classes. For example, given a class
struct A { int a; void fa(); }
struct B { int b; void fb(); }
struct C : A, B { int c; void fc(); }
public interface IB {
int b { get; set; }
void fb();
}
public class B : IB {
public B(...) {...}
public int b { get { ... } set { ... } }
public void fb() { ... }
...
}
public class C : A, IB {
public C(...) {...}
public int b { get { ... } set { ... } }
public void fb() { ... }
public int c { get { ... } set { ... } }
public void fc() { ... }
...
}
This way, it is possible to create a standalone B that is not a C. Of
course, if B is an abstract base class then an interface alone should
suffice, i.e. only an interface IB is needed, not a class B.
It would also be nice if there were some way to request SWIG to
create a property that returns a wrapper for B, e.g. in C#, C could have an
// C can convert to B implicitly
public static implicit operator B(C c) { ??? }
But this feature is optional, since the user could add the necessary
code manually, or (I assume) with help from a macro.
Date: 03/19/2013 04:55 PM
Subject: [Swig-devel] Abstract base classes to interfaces conversion
As far as I know, currently there is no easy way to convert C++ abstract base
classes to Java/C# interfaces. (short of redeclaring the classes and
doing some more work)
I found the only reference to the attempt to implement that feature
here: http://thread.gmane.org/gmane.comp.programming.swig.devel/18403
Is there any information about the status of that work?
(As I understand, the patch posted in that thread is far from
perfection)
Maybe there is some alternative solution being worked at in some SWIG branch?
If no one is planning to implement that feature in near future, I
will do it (in some way at least).
(Because, converting abstract base classes to interfaces is
currently the most time consuming feature in our
project wrappers (after nested classes were implemented)).
I think "feature:interface" syntax proposed by Zhiyang Jiang is ok.
Any suggestions, advices?
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
No virus found in this message.
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!