September 21

On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote:

>

Here is the macro:

#define NK_CONTAINER_OF(ptr,type,member)\
    (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))

I'm trying to translate the Nuklear GUI library to D here.

When you're done, will you put it on dub?

September 22

On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven wrote:

>

The 1st argument of getMember can just be T, like the original macro.
The 2nd argument needs to be a compile-time string.
Also the char* cast needs to apply to ptr before subtracting the offset AFAICS.

So reordering to keep type inference of ptr:

auto nk_container_of(T, string member, P)(P ptr)
{
    return cast(T*)(cast(void*)(cast(char*)ptr -
        __traits(getMember, T, member).offsetof)));
}

(Untested)

I will test it:)

September 22

On Thursday, 21 September 2023 at 16:50:51 UTC, Imperatorn wrote:

>

On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote:

>

Here is the macro:

#define NK_CONTAINER_OF(ptr,type,member)\
    (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))

I'm trying to translate the Nuklear GUI library to D here.

When you're done, will you put it on dub?

Yes, I will.

September 23

On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote:

>

Here is the macro:

#define NK_CONTAINER_OF(ptr,type,member)\
    (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))

I'm trying to translate the Nuklear GUI library to D here.

I did translate the library. Wow! That was a lot. It successfully builds, but does not work for long and crashes if I try to do something: it's either an assertion that fails or out of bounds array access is thrown by D. The only thing that works is pressing a button.

Now I need to somehow find and fix these things. Nuclear uses flexible array members in structs and I am a bit puzzled how to handle this without a major code refactor.

1 2
Next ›   Last »