Class info on interfaces

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 29 08:21:37 PDT 2015


On 2015-08-28 22:40, rumbu wrote:

> The linkage check it's good as long you don't have an abomination like
> this:
>
> extern(C++) interface CPPInterface
> {
>      extern(D) void foo();
> }

Good point.

> Anyway, the problem is the availability of such function. If the
> interface doesn't contain any function, there is no way to detect the
> type, except for COM Interfaces which are clearly implementing IUnknown.
>
> But deriving a new interface and defining a sentinel function, it works:
>
> import std.stdio;
> import std.traits;
> import core.sys.windows.com;
>
> interface NativeInterface {}
>
> interface COMInterface : IUnknown {}
>
> extern(C++) interface CPPInterface {}
>
> enum InterfaceKind { native, windows, cpp }
>
> template interfaceKind(I) if (is(I == interface))
> {
>      interface J : I { void __foo__(); }
>      static if (functionLinkage!(J.__foo__) == "D")
>          enum interfaceKind = InterfaceKind.native;
>      else static if (functionLinkage!(J.__foo__) == "Windows")
>          enum interfaceKind = InterfaceKind.windows;
>      else static if (functionLinkage!(J.__foo__) == "C++")
>          enum interfaceKind = InterfaceKind.cpp;
>      else static assert(false, "Unknown interface kind.");
> }
>
> int main(string[] argv)
> {
>     static assert(interfaceKind!NativeInterface == InterfaceKind.native);
>     static assert(interfaceKind!COMInterface == InterfaceKind.windows);
>     static assert(interfaceKind!CPPInterface == InterfaceKind.cpp);
>     return 0;
> }

That looks like a pretty good idea, thanks. I'm wondering if it's worth 
implementing a trait for this in the compiler.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list