How to alias struct ?

Hi all.
I am working on a program which uses external hardware to acquire data. we have the option to use hardware from two different companies, each with it's own driver. However both do the same job. My program is meant to read data packets which are structured as:
typedef struct _data
{
DWORD ID;
BYTE bf;
BYTE bd;
BYTE bData[8];
DWORD bt;
}data;

Now both the companis use the same data structure, but the first has defined the structure, let's say, as:
typedef struct _dataX
{
DWORD ID_X;
BYTE bf_X;
BYTE bd_X_;
BYTE bData_X[8];
DWORD bt_X;
}dataX;

while the second one defines it as:
typedef struct _dataY
{
DWORD ID_Y;
BYTE bf_Y;
BYTE bd_Y_;
BYTE bData_Y[8];
DWORD bt_Y;
}dataY;

I am programming my software so as t allow the customer to use the hardware of their choice. Simply select the card they are using and our program should be able to take care of the rest of stuff. I am using #ifdef directives to include the header for the corresponding hardware dll. Now I want to define my own data struct of the type:
typedef struct _dataMY
{
DWORD ID_MY;
BYTE bf_MY;
BYTE bd_MY_;
BYTE bData_MY[8];
DWORD bt_MY;
}dataMY;

and sue something like this
#ifdef company1
_dataMY = _dataX
#else
_dataMY = _dataY

and when I use _dataMY.ID_MY .. it should directly be able to see if it is _dataX.ID_X or _dataY.ID_Y, based upon the directive I have used earlier. I am sorry if I am unable to explain my question completely. I would appreciate any help with aliasing the structs here.

Thanks
Vj
You could use a #define?

1
2
3
4
5
#ifdef company1
#define DATA dataX
#else
#define DATA dataY
#endif 


then you use DATA later on.

But it looks like the structures are equivalent, so do you actually need to do this?

Andy
But it looks like the structures are equivalent, so do you actually need to do this?

These are third-party interfaces, so the OP has no control over when and how they may change in the future. And there's no knowing whether you might want to add support for more hardware in the future, that might use an entirely different interface.

Much better to abstract this stuff away now, rather than assuming you'll always have identical data structures.
@andywestken

This is exactly what I am trying to do. But later on. e.g. if I want to check the ID of the packet, I can have something like

DWORD varA = DATA.ID_X

But let's say user is using hardware 2 with dataY. In that that would throw an exception because dataY.ID_X won't be a memebr of dataY structure. Is theira way around?

@MikeBoy

yeah the structure are pretty much same, but they have different names for the same members. How do I tell this to compiler?


I am trying to use 'Union'. It seems to work, but I am a little skeptical about the use of 'Union'. People say it's not a healthy way of programming.
1
2
3
4
5
6
7
8
9
10
11
#ifdef company1
 union mydata{
_dataX
_dataMY
};
#else
 union mydata{
_dataY
_dataMY
};
#endif  


and then use 'mydata' later on. Any comments?

Thanks
Vijay
Last edited on
Topic archived. No new replies allowed.