How does inheritance and vtables work wrt. C++ and interop with D? Fns w/ Multiple-inheritance args impossible to bind to?
Gavin Ray
gavinray at site.com
Tue May 25 18:08:52 UTC 2021
On Tuesday, 25 May 2021 at 11:38:03 UTC, evilrat wrote:
> I did some basic testing with code above, it seems class layout
> is recursively linear at least on Windows, and D follows C++
> rules close enough, at least it works if one comments out all
> but the first base, and the rest can be hand crafted using
> structs and templates.
>
>
> With this code it is possible to pass back C++ class received
> from C++ code and call it.
> So it seems support for multiple inheritance shouldn't be that
> hard to implement (for Windows), no idea how it works on Linux
> though.
>
> Maybe I'll be able to create templated base class that does all
> this stuff with mixins and templates.
>
> like this
> ```d
> extern(C++)
> abstract class MultipleInheritance(T...)
> {
> // implementation //
> }
>
> class Derived : MultipleInheritance!(Base1, Base2)
> { ... }
> ```
Oh this is brilliant! Absolutely incredible work + findings!
Is this conceptually similar at all to this bit of code that adr
had given to me?
```d
// Same thing repeated for Abstract2
interface Abstract1 {
/* virtual */ void overrideMe1();
void sayHello();
mixin template Abstract1Impl() {
void sayHello() { import std.stdio; writeln("Hello"); }
}
}
class MyClass : Abstract1, Abstract2 {
mixin Abstract1Impl; mixin Abstract2Impl;
override void overrideMe1() { writeln("overrode 1"); }
override void overrideMe2() { writeln("overrode 2"); }
}
void main() {
auto it = new MyClass();
it.sayHello(); it.sayGoodbye();
it.overrideMe1(); it.overrideMe2();
}
```
---
Also, I wonder if there's a good way to build a quick CLI
visualization + debugging tool for this using D's `.vtble[]`
method. I would find it really useful for learning =D
There are also some automatic ABI compliance checker
tools/libraries, which could be good for programmatically
verifying that the class produced by D is valid with the ABI of a
method/class from a C++ library file.
(I know for example, that Qt does this with their Python codegen
tool, `Shiboken`, for auto-testing the generated code)
-
https://sourceware.org/libabigail/manual/libabigail-overview.html
- https://github.com/lvc/abi-compliance-checker (this is what
Shiboken is/was using)
> ... libabigail provides facilities to perform deep comparisons
> of two ABIs. That is, it can compare the types of two sets of
> functions or variables and represents the result in a way that
> allows it to emit textual reports about the differences.
> This allows us to write tools like `abidiff` that can compare
> the ABI of two shared libraries and represent the result in a
> meaningful enough way to help us spot ABI incompatibilities.
> There are several other tools that are built using the Abigail
> framwork.
```sh
$ g++ -g -Wall -shared -o libtest-v0.so test-v0.cc
$ g++ -g -Wall -shared -o libtest-v1.so test-v1.cc
$
$ ../build/tools/abidiff libtest-v0.so libtest-v1.so
Functions changes summary: 0 Removed, 1 Changed, 0 Added function
Variables changes summary: 0 Removed, 0 Changed, 0 Added variable
1 function with some indirect sub-type change:
[C]'function void foo(S0&)' has some indirect sub-type changes:
parameter 0 of type 'S0&' has sub-type changes:
in referenced type 'struct S0':
size changed from 32 to 64 bits
1 data member insertion:
'char S0::inserted_member', at offset 0 (in bits)
1 data member change:
'int S0::m0' offset changed from 0 to 32
```
More information about the Digitalmars-d-learn
mailing list