I'm trying to simulate the "template" style behavior in C as part of an exercise to refactor code from cpp to C. I constructed a MACRO approach by defining as struct like so:
#define FIFO(TYPE, depth, NAME) \
typedef struct fifo_##TYPE##NAME \
{ \
TYPE array[depth]; \
uint8 st_idx; \
uint8 end_idx; \
uint32 cnt; \
}fifo_##TYPE##NAME; \
void fifo_##TYPE##NAME_init(fifo_##TYPE##NAME *f); \
int fifo_##TYPE##NAME_push(fifo_##TYPE##NAME *f, TYPE *elem); \
TYPE* fifo_##TYPE##NAME_pop(fifo_##TYPE##NAME *f); \
TYPE* fifo_##TYPE##NAME_peek(fifo_##TYPE##NAME *f); \
I then defined MACRO functions for the prototypes shown above. This actually increased my code size as compared to template approach, since each of the functions were being defined for every TYPE.
I'm now attempting a void* approach instead of MACRO. I'm still using the MACRO struct definition as above, but I'm not defining MACRO functions. I'm using ordinary inline functions in the .h file but I'm stuck on how to access the members of the generic TYPE struct through a void* ptr. What type can I cast it to? If the name of the struct changes as per TYPE, I don't have a definite struct type to cast it to. Also, MACROs wouldn't work in non-macro functions.
Sorry for the verbiage! Thanks.