My application is written in C. I have a module that uses some data from a certain given global structure. I now have to extend the module to optionally work against a different given global structure, which basically provides the same fields (as far as my module is concerned), but under different names.
Here’s a car analogy to hopefully make my problem clearer. I’ve got these two global structures I have no control over.
struct {
unsigned char manufacturer_id;
unsigned short top_speed;
} Car;
struct {
RGB_t color;
unsigned short topSpeed;
unsigned char mfr;
} Automobile;
Let’s say my Car Manager module uses information from Automobile. E.g.,
const char *car_manager__get_manufacturer_name(car_manager_t *self)
{
return self->manufacturers[Automobile.mfr];
}
I’d like to extend Car Manager to optionally (perhaps decided by a flag in the car_manager_t instance) use the same information from Car, so the above function would return self->manufacturers[Car.manufacturer_id]. I don’t want to duplicate any logic in the module while adding this functionality.
I assume I’ll have to put an interface on the access to the global structures. Any suggestions on how to do that?
I would define functions for getting the needed values, and pass pointers to the functions. You could even pass a struct which contains the needed function pointers.
I haven’t written any C for a long time; please correct my syntax if necessary!