www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12627] New: extern(C++) interfaces should format

https://issues.dlang.org/show_bug.cgi?id=12627

          Issue ID: 12627
           Summary: extern(C++) interfaces should format
           Product: D
           Version: D2
          Hardware: x86
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

This is very similar to issue #11175, and can be solved in the same way.

The gist of it is that when an interface does not support toString, it is cast
to Object to call toString. If casting to object does not work, it simply
prints null.

Well, we can print at least the address of the object if toString is not
supported.

One issue is that it's a little difficult to unit-test this. You need a C++
object, and there is no way to create it in D. From my attempts, I can't even
*cast* a void * pointer to a C++ interface. The compiler doesn't seem to allow
any type of casting to C++ interfaces (bug?).

Here is the only way I was able to get it to test:

module cppcast;

// need to circumvent mangling
extern(C) void *cppcast(void *ptr)
{
   return ptr;
}

module testcppcast;

version(unittest)
{
   extern(C++) interface X
   {
   }

   extern(C) X cppcast(void *ptr);
}

unittest
{
   auto dummy = cast(void*)1234;
   auto x = cppcast(dummy); // dummy c++ class, doesn't need to be valid
   string expected = format("%X", dummy);
   formatTest(x, expected); // fails, x is printed as 'null'
}

Special-casing C++ interfaces to cast to void * if they don't have a toString
method should work, just like the IUnknown solution.

--
Apr 23 2014