dearimgui / dear_bindings

C header (and language binding metadata) generator for Dear ImGui
MIT License
221 stars 12 forks source link

Pointer metadata #48

Closed pdoane closed 8 months ago

pdoane commented 8 months ago

I am looking at generating bindings for Zig based on the metadata from dear_bindings. While Zig can work directly with C interfaces (and C pointers), its native pointer types are more expressive which helps to catch user errors, e.g.

*T - single-item pointer to exactly one item [*]T - many-item pointer to unknown number of items [N]T - array of N items *[N]T - pointer to N items, same as single-item pointer to an array []T - a slice which contains a pointer of type []T and a length `[:0]T- pointer that has a length determined by a sentinel value (0 in this case) [N:0]T- array which has a sentinel element of value (0 in this case) [:0]T` - a slice which has a runtime-known length and also a sentinel value at the element indexed by the length

There are also optional items ?T which can be combined with pointers.

Current strategy:

This looks like a pretty good start but some many-item scenarios are missed, e.g. ImVector::Data is translated as a single-item pointer so doesn't support indexing.

Would there be interest in providing additional metadata here? It likely would require annotations in imgui.h but it seems like that would provide helpful user documentation.

pdoane commented 8 months ago

Some concrete proposals...

Null pointers could be opt-in or opt-out. I prefer being explicit when it could be null.

IMGUI_API void          PushFont(IM_NULLABLE ImFont* font);

Many-item pointers

    const IM_MANY_ITEMS ImGuiTableColumnSortSpecs* Specs;     // Pointer to sort spec array.
pdoane commented 8 months ago

@ocornut - are you okay to add annotations like this to the public imgui.h? I can get this started for both imgui and dear_bindings if approved.

ocornut commented 8 months ago

Hello Patrick :)

It bothers me a bit to add annotations to imgui.h because it would make things rather noisy. I'm also not sure I know how many annotations would be required (what's the average function to annotation ratio ?).

Not ruling this out but I'm wondering if in the meantime dear bindings could ship with a data table, meaning those annotations would be external?

I do realize in theory it means separate maintenance and the possibility of a missing annotations, but I'd argue in practice a vast majority of API are stable by now, and my gut feeling is that readability of raw imgui.h seems like a more important feature to maintain.

(the slight irony being that those annotations are technically an extra info for the user, but that info is mostly covered by comments/docs/examples/asserts and in practice I worry we reach a certain amount of bulkiness that makes reading the file discouraging)

pdoane commented 8 months ago

Completely understand! Two other ideas to think about:

The number of places where IM_MANY_ITEMS is needed is very small. Because of the overall API design, it's rare for applications to work with multiple things at once. It's also not needed for function parameters as the [] syntax can be used, so this only needs to focus on public structure fields.

Nullable is probably the more interesting discussion. It doesn't typically overlap with comments because it's usually easy to guess correctly, but the extra annotation would make it very clear.

Hope things are going well!

pdoane commented 8 months ago

To move this forward, I'll create a data-table in my generator and we can check that out when it's complete.

ShironekoBen commented 8 months ago

Cool, that sounds good to me too. Thanks!

pdoane commented 8 months ago

An update on progress...

    .{ "TableSortSpecs", "Specs" },
    .{ "DrawList", "_VtxWritePtr" },
    .{ "DrawList", "_IdxWritePtr" },
    .{ "FontConfig", "GlyphRanges" },
    .{ "FontAtlas", "TexPixelsAlpha8" },
    .{ "FontAtlas", "TexPixelsRGBA32" },

It's too much work to figure out is_nullable manually, but I can update a table when people hit issues.

pdoane commented 8 months ago

I'm going to close this out for now - we can revisit when nullable is better understood.

ocornut commented 8 months ago

FYI i have spotted a Zig backend generated with dear bindings: https://github.com/DarknessFX/zig_workbench/

Added it to https://github.com/dearimgui/dear_bindings/wiki/Software-using-Dear-Bindings