I have to write a function update(char* name, void* data); which receives the name of the message and void* to the data of that message. The type of the data is always fixed as “xxx_name”, depending on what the name is.
My function should do the following :-
- Generate a new structure of type of xxx_name within the function.
- Copy the data from the pointer data, of size xxx_name to a new location, as data would soon be freed by external interface.
In my case, the list of possible structures of type xxx_name is pretty huge and spread across, so writing if/else or switch conditions is out of question.
Is there a way to do this in C via some macro / stringizing operator etc?
This solution uses NULL terminated look up table (LUT) to find the appropriate size. Then allocates memory in that size. Then copies the input data to the new object. If the number of types of objects is large enough the LUT can be presorted then a binary search performed instead of a linear search.