www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Classes in D with betterC

reply Test123 <test123 gmail.com> writes:
https://forum.dlang.org/post/mailman.1040.1637987151.11670.digitalmars-d-announce puremagic.com

On Friday, 26 November 2021 at 16:01:40 UTC, Luís Ferreira wrote:
 The dynamic cast is perfectly doable too, you just need to 
 implement your own typeinfo, and it is not that hard and 
 unusual. A clear example of that is LLVM. They use their own 
 implementation of TypeInfo, see 
 [here](https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html).
Is there any example how to use betterC with LLVM Style RTTI ?
May 01 2022
parent reply user1234 <user1234 12.de> writes:
On Sunday, 1 May 2022 at 19:51:19 UTC, Test123 wrote:
 Is there any example how to use betterC with LLVM Style RTTI ?
The basic blocks: - you need an allocator that places RTTI after the instances - you need an helper func that retrieve the RTTI ```d module runnable; import core.stdc.stdlib, core.stdc.stdio; struct RTTI { immutable uint nameLen; immutable char* name; immutable uint size; // etc... } struct RTTIAllocator { static T** make(T,A...)(A a) { // one per type... __gshared RTTI rtti = RTTI(T.stringof.length, T.stringof.ptr, cast(uint)T.sizeof); void*[2]* r = cast(void*[2]*) malloc(size_t.sizeof * 2); (*r)[0] = malloc(T.sizeof); (*r)[1] = &rtti; auto result = cast(T**) r; (*result).__ctor(a); return result; } } const(RTTI)* getTypeInfo(void** instance) { void*[2]* r = cast(void*[2]*) instance; return cast(RTTI*) (*r)[1]; } struct Test { int a; this(int a) { this.a = a; } } extern(C) void main() { auto t = RTTIAllocator.make!Test(123); auto r = getTypeInfo(cast(void**)t); printf("`%s` has a size of `%d` byte\n", r.name, r.size); } ``` The RTTI struct can contain everything that can be retrieved at compile time. Typically function addresses suc as the ctor, the dtor, vtable entries. One must take care to the RTTI layout however. For example static arrays must not be stored, but rather just a pointer to first element plus its length, just like in the example. And best of the lucks to make that ` safe`
May 01 2022
parent user1234 <user1234 12.de> writes:
On Monday, 2 May 2022 at 01:03:59 UTC, user1234 wrote:
 On Sunday, 1 May 2022 at 19:51:19 UTC, Test123 wrote:
 Is there any example how to use betterC with LLVM Style RTTI ?
The basic blocks: - you need an allocator that places RTTI after the instances - you need an helper func that retrieve the RTTI
updated example https://gitlab.com/-/snippets/2309865
May 01 2022