gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > "Deconstructing" a struct

#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.

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

#14235 - sajiimori - Sat Jan 03, 2004 2:05 am

The C runtime environment (if you could even call it that) doesn't store any type information, so you can't write a function that iterates over the data members of an arbitrary struct.

You can iterate over the bytes of a struct and transfer those via a link cable. In a traditional network application you wouldn't normally do that, because the bytewise layout of a struct is totally dependent on the compiler and is not necessarily the same across systems. But on GBA you can get away with it as long as the exact same ROM is being used on each system.
Code:

// Warning: only works when sending to
// exact same executable image.
send_struct(SomeStruct* s)
{
    u8* p = s;
    u8* end = p + sizeof(SomeStruct);

    while(p < end)
        send_byte(*p++);
}