I'm trying to remove device specific defines from my files. I would like to put all device specific names in a single header file. Currently I check a device type value and based on this value I will read/write a certain value defined in a header. I would like to make this read/write value generic.
For example:
if (type == 1)
read(a);
elseif (type ==2)
read(b);
else
read(c);
Every time I add a new type I have to find every instance and add a case.
I would prefer doing:
read(x);
every time;
I was thinking I could create a generic struct with a list of all the variables I need.
Based on this I could initialize the structure once s.t. x = a, b, or c as needed. This values are determined once at the begininng and never need to change later.
I would prefer not creating a function to initialize the structure based on my device either. Is there a way to create a generic structure and have multiple instances predefined and generic pointer that can point to any structure I choose:
Something like this:
And then have a pointer to type generic that I can give the address of A or B to? I can't seem to get it working. I wanted to do this all in a header file so it is reusable and have my app. have a pointer that points to either A or B as needed. Is this possible and how can it be done?Code:struct generic { int x = 0; int y = 0; } A, B; A.x = 1; A.y= 2; B.x = 1; B.y= 2;