libui-ng / libui-ng

libui-ng: a portable GUI library for C. "libui for the next generation"
https://libui-ng.github.io/libui-ng/
MIT License
581 stars 51 forks source link

Add a boolean type to the public API. #235

Open szanni opened 8 months ago

szanni commented 8 months ago

There are many places within the API that should use a boolean type but currently use int. The docs even talk about TRUE and FALSE, hence I would like a proper boolean type for clarity.

Suggested ideas:

  1. Use <stdbool.h> which would give us bool, true, and false as native types
  2. Define our own uiBool or _UI_BOOL and use TRUE and FALSE (which seem to be defined in glib, the win32 API and Objective-C?)
  3. A mix of both <stdbool.h> to get true and false for use internally and our own uiBool or _UI_BOOL type to expose in the pulic API.

C99 interop with C++ seems to be a non issue, otherwise no software would work anywhere.

So unless I'm missing something the only concern would be ABI stability:

From the C99 standard:

An object declared as type _Bool is large enough to store the values 0 and 1.

Essentially meaning bool is implementation defined. In practice each arch seems to define sizeof(bool) to prevent havoc (similar to how each arch defines int?). On x86_64 all compilers I checked (gcc, clang, tcc, icc, zig cc, g++, clang++) define it as 1, which makes sense, because otherwise the entire shared library system on unix platforms would not work.

I am fairly certain using option 1 and exposing bool in the public API would not be breaking ABI. So my vote would be option 1, nice and clean, using compiler native types.

If this is deemed to risky we could define our own uiBool type: #define uiBool int Then we could redefine uiBool to be bool in doxygen to make the intent clear. In this case I would like to see a build flag -Dforce-stable-abi=(true|false) which would default to true. For true this would define uiBool int, _UI_ENUM int For false this would define uiBool bool, _UI_ENUM enum to get actual type correctness.

I currently build everything in tree, hence I care more about type hints by the compiler than ABI. For shared library distribution, I am definitely for ensuring ABI compatibility. From what I can tell exposing bool would not pose a problem though.