I am working on a large project that is provided to our customers as a static c lib and one header file that has the prototypes for our exposed API. The project has two similar but unique builds that require different data structures to be exposed in the header. I am trying to come up with the best design to allow a single API function to work with different data structures depending on the build. This is my idea so far, but I’m worried this is a bad design.
My function will be implemented like this
void foo(void *generic_data_struct)
{
#ifdef BUILD1
build1_t *data_struct = generic_data_struct;
#else
build2_t *data_struct = generic_data_struct;
#endif
...
}
And the exposed API header will be, depending on the build the customer orders, either
void foo(build1_t *data_struct);
or
void foo(build2_t *data_struct);
Is this a reasonable design pattern or is this frowned upon? Thanks!
Why not like this:
AFAIK, the standard does not require different pointer types to be bitwise compatible, so your original idea might not be portable to all possible implementations. It would’ve been if the prototype also used
void*(but that of course sacrifices some type safety in the process).