I'm doing some work in C++ for a company that has everything else written in C (using C isn't an option for me :( ). They have a number of data structures that are VERY similar (i.e., they all have fields such as "name", "address", etc. But, for whatever reason there isn't a common structure that they used to base everything else off of (makes doing anything hell). Anywho, I need to do a system-wide analysis of these structs that are in memory, and through it all into a table. Not too bad, but the table has to include entries for all the fields of all the variables, even if they don't have the field (struct b may have field "latency", but struct a doesn't - in the table the entry for each instance of a must have an empty entry for "latency".
So, my question is, is there a way to determine at runtime if a structure that has been passed into a template function has a specific field? Or will I have to write some black magic macro that does it for me? (The problem is basically that I can't use template specialization)
Thanks! If you have any questions please feel free to ask!
Here's a snippit of what I was thinking...
struct A
{
char name[256];
int index;
float percision;
};
struct B
{
int index;
char name[256];
int latency;
};
/* More annoying similar structs... note that all of the above are defined in files that were compiled as C - not C++ */
struct Entry
{
char name[256];
int index;
float percision;
int latency;
/* more fields that are specific to only 1 or more structure */
};
template<typename T> struct Entry gatherFrom( T *ptr )
{
Entry entry;
strcpy( entry.name, ptr->name, strlen( ptr->name ) );
entry.index = ptr->index;
/* Something like this perhaps? */
entry.percision = type_contains_field( "percision" ) ? ptr->percision : -1;
}
int main()
{
struct A a;
struct B b;
/* initialization.. */
Entry e = gatherFrom( a );
Entry e2 = gatherFrom ( b );
return 0;
}