Serializing A Struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MomoCPP
    New Member
    • Oct 2008
    • 1

    #1

    Serializing A Struct

    Hello all,

    I'm attempting to serialize the following:

    Code:
    typedef struct 
    {
    	const char* name;
    	const char* layer1[12];
    	const char* layer2[12];
    	const char* layer3[12];
    	const char* layer4[12];
    	bool passability[12*800][800];
    }DataMap;
    
    
    typedef struct 
    {
    	DataMap map;
    }MapManager;
    And my code for serializing this is:
    Code:
    		 // Save
    private: System::Void SaveMap(MapManager* map)
    		 {
    			 ofstream ofs(StringToChar(CharToString(map->map.name) + ".map"), ios::binary);
    			 ofs.write((char *)&map->map, sizeof(map->map));
    		 }
    		 // Load
    private: System::Void LoadMap(System::String^ fname, MapManager* map)
    		 {
    			 ifstream ifs(StringToChar(fname), ios::binary);
    			 ifs.read((char *)&map->map, sizeof(map->map));
    		 }
    (This is in .Net but I need to serialize in Cpp cause the game is in non-managed Cpp. You can ignore the StringToChar and CharToString.)

    However, I recently learned that I can't serialize pointers which puts me in a bind because I don't know any other way to go about this.

    What I'm trying to do is make a simple map editor that saves map data and loads it. I also want to be able to load this data from the game I'm programming. So I decided to use a simple struct the hold the data.

    note: I don't necessarily need to use "const char*" if there is a way to convert an object to "const char*" but like i said, I don't know any other way to go about this. The reason I'm using "const char*" is because the engine i'm using only accepts "const char*" for loading textures and so on.

    What should I do to save/load my data? Is there a workaround I can do?
    I don't necessarily want to know a way to serialize pointers themselves because it might become troublesome in the future, so a workaround would be better. Such as getting the data that the pointer is pointing to a temporary variable and saving that variable instead and loading it in reverse.

    (If you need more info than just ask :))
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    That's true, you cannot serialize pointers. However, you can serialize offsets. So determine the offset from the start of the array to the char*.

    Next, write the data pointed at vt the char*. Precede this data with the length oif the data and precede that by the offset.

    On the readback, you create a array of char*. Then start reading. The offset will tell you whuich array element to use, the length ofthe data will tell you how many bytes of memory to allocate for the data, and then you copy the data from the disc to this new allocation.

    I recommend you use a Serialize object designed as a Visitor. You would visit the datamap object with the Serialize object and you data goes to disc. That is, no code in the datamap object about serialization. Similary, a Restore object can visit an empty datamao object and pump if full of data.

    Check out the Visitor design pattern: http://bytes.com/forum/thread674645.html.

    Comment

    Working...