That is why I said, serialize
It would be nice if all wx relevant data classes would have some kind of bridge function or would be inherited from a serializable class.
For the record, a class is nothing more then a struct with some code around it. The problem with just writing back the data belonging to a class/struct is:
- Pointers (created with new) are not valid anymore after a read. The memory allocated and pointed to is never the same after a second time
- The class can be changed in the meantime, eg extended so the whole binary footprint is not the same
- No deep copy / writing. Classes created with new inside your other class is simply not copied only theyr pointer is taken.
I have developed a class for serializing if you are interested. It is called wxSIO I can see if I can brush it up enough to be workable for you.
With this class you can do the following:
Code: Select all
class SomeDataClass
{
// serializing of file
void Serialize(wxSIO &file) {
if(file.IsWriting())
{
// we are writing
file << m_myString;
file << m_myInt;
}
else
{
// we are reading
file >> m_myString;
file >> m_myInt;
}
};
}
// use the class like this
wxSIOFile myFile();
if(myFile.ReadFromFile("c:\\some\\file.bin"))
{
m_data = new SomeDataClass();
m_data->Serialize(file);
// ofcourse you can propagate the serializing ..
}
The wxSIO class has some features which might interest you;
- Platform independent binary code (meaning a linux bin file can be read also on a windows machine)
- A header can be set which is written to the file and read back. This has as advantage that you can check if the file is what you think it is, if it does not match the file returns an error
- The serializing is done with type safe mechanism. Every value is preceded with a type to make sure that the next value is actually an int, string, quad, etc
- Whenever an error occurs during reading the file locks up and will only read predictable values (0 for int, empty string, false for bool etc).
- wxSIO::OK() can be checked everytime to make sure the reading is still going ok
If you (or someone else) is interested in this class, I will try to make it compile again, with a small sample. Just say so.
Regards,
- Jorgen