#14232 - ScottLininger - Sat Jan 03, 2004 1:51 am
Does anyone know of a way to iterate across all members of an arbitrary STRUCT in C? I'd like a function that is able to "deconstruct" the struct at runtime to figure out, for example, what the data type and value of the "2nd member" is.
It seems to me that simple pointer math isn't an option since the data types within the struct can vary, but I'm hoping there is some clever person out there who can tell me different. I know you can get the memory size of the entire struct, or the memory size of a given member, but ONLY if you know the name of that member.
Okay, okay, I know you're saying "If you create the struct yourself then you *know* the freaking memory sizes and names already!"
What I'm trying to do is create a generic library for handling simple multiplayer data exchange (via linkcable) using a struct whose name stays the same project to project but whose content can be totally different.
For example, to create a multiplayer pong game, your struct might look like.
Whereas for a battleship game it might look like...
I would like to write a generic function that handles all the IO transfers and makes sure that every client GBA has a "current" copy of the gamestate struct, and ensures that only the GBA with "control" of a given member can actually update it.
I'm working on code that does this manually on a variable-by-variable basis, but I'd love to make it more portable, because right now there's just a lot of recoding when you find your gamestate needs to accommodate more information.
Thanks in advance...
Scott
It seems to me that simple pointer math isn't an option since the data types within the struct can vary, but I'm hoping there is some clever person out there who can tell me different. I know you can get the memory size of the entire struct, or the memory size of a given member, but ONLY if you know the name of that member.
Okay, okay, I know you're saying "If you create the struct yourself then you *know* the freaking memory sizes and names already!"
What I'm trying to do is create a generic library for handling simple multiplayer data exchange (via linkcable) using a struct whose name stays the same project to project but whose content can be totally different.
For example, to create a multiplayer pong game, your struct might look like.
Code: |
typedef struct gamestate { u8 leftPaddleY; //updated by GB0 u8 rightPaddleY; //updated by GB1 u8 ballX; //updated by GB0 u8 ballY; //updated by GB0 u32 leftPlayerKeyPressState; //updated by GB0 u32 rightPlayerKeyPressState; //updated by GB1 }; |
Whereas for a battleship game it might look like...
Code: |
typedef struct gamestate { u8 player0GuessX; //updated by GB0 u8 player0GuessY; //updated by GB0 u8 player1GuessX; //updated by GB1 u8 player1GuessY; //updated by GB1 u8 Player1Map[10][10]; //updated by GB0 u8 Player2Map[10][10]; //updated by GB1 }; |
I would like to write a generic function that handles all the IO transfers and makes sure that every client GBA has a "current" copy of the gamestate struct, and ensures that only the GBA with "control" of a given member can actually update it.
I'm working on code that does this manually on a variable-by-variable basis, but I'd love to make it more portable, because right now there's just a lot of recoding when you find your gamestate needs to accommodate more information.
Thanks in advance...
Scott