Storing interfaces as void[]

Imperatorn johan_forsberg_86 at hotmail.com
Fri Mar 12 17:46:22 UTC 2021


On Friday, 12 March 2021 at 17:37:43 UTC, David  Zhang wrote:
> I want to store interfaces as untyped void[], then cast them 
> back to the interface at a later time. However, it appears to 
> produce garbage values on get().
>
> Is this even possible, and if so, what is happening here? The 
> alternative would be a struct { CheckedPtr self; api_fns.... }
>
> e.g.
>
> void register(I)(I i) {
>   auto mem = new void[](I.sizeof);
>   memcpy(mem.ptr, cast(void*) i, I.sizeof);
>
>   // CheckedPtr includes a hash of fullyQualifiedName
>   map[i.get_name()] = CheckedPtr!I(mem.ptr);
> }
>
> I get(I)() {
>   // basically cast(I) p
>   return map[I.get_name()].as!I();
> }

Have you tried using Variant or jsvar 
(https://code.dlang.org/packages/arsd-official%3Ajsvar)? 🤔


More information about the Digitalmars-d-learn mailing list