Exposing interfaces

51 views
Skip to first unread message

spen...@gmail.com

unread,
Mar 30, 2015, 5:24:05 PM3/30/15
to cppsha...@googlegroups.com
Hi,

I would like to expose a C++ abstract base class as an interface that can be implemented by a C# class and passed back into the C++ library. The C++ header would look something like this:

class IInterface
{
public:
virtual ~IInterface()
{
}

virtual int doSomething() = 0;
};

class Concrete
{
public:
Concrete(IInterface *thing);
};

Is this possible?

Thanks,

Chris

João Matos

unread,
Mar 30, 2015, 6:50:33 PM3/30/15
to spen...@gmail.com, cppsha...@googlegroups.com
It's possible but not currently supported by CppSharp. Since the feature seemed easy I spent a couple spare minutes here trying to make it work: https://gist.github.com/tritao/97cda97b65ac31d8e211

That simple attempt almost worked but is missing the Internal definitions in the interface. If you add a GenerateClassInternals(@class) call to GenerateInterface it also errors since interfaces cannot contain inner types.

The solution is to either generate the Internal definitions in the extra "interface abstract implementation" class that we must generate to support methods returning interface types or use the same solution that Dimitar implemented to handle multiple inheritance, see the relevant code here: https://github.com/mono/CppSharp/blob/master/src/Generator/Passes/MultipleInheritancePass.cs#L123

Overall I think the feature is useful and can be added quite easily with some more time.


--
You received this message because you are subscribed to the Google Groups "CppSharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cppsharp-lis...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
João Matos

spen...@gmail.com

unread,
Mar 31, 2015, 4:05:21 AM3/31/15
to cppsha...@googlegroups.com, spen...@gmail.com
Thanks João, that sounds promising.

Chris
Reply all
Reply to author
Forward
0 new messages