I have a struct defined like this:
struct GameState {
int score;
int moves;
bool won;
void *metadata;
};
typedef struct GameState GameState;
The metadata pointer will point to another struct of a type decided at runtime. For example it might be:
struct KlondikeMetadata{
bool draw3;
int drawcount;
};
typedef struct KlondikeMetadata KlondikeMetadata;
Or maybe:
struct FreeCellMetadata{
int reserveCells;
};
typedef struct FreeCellMetadata FreeCellMetadata;
The actual metadata struct used depends on the game the user is playing. 99% of the time this isn't a problem because I know what game the user is playing. However, there are cases where I don't (and can't) know this.
My question is, is there a way to determine or specify the correct metadata type at runtime?
For example if I could add a property to the GameState struct indicating that the metadata value is of type KlondikeMetadata and use that to cast the metadata to that type, I think I'd be golden. Is there a way to do this? Is there a way to specify a type and cast a variable at runtime in C?