Re: Object (de)serialization
On Mon, 25 Jan 2010 05:13:01 -0800, Brian wrote:
class Triangle : public Shape {
?? ?? ?? ?? public:
?? ?? ?? ?? ?? ?? ?? ?? Triangle() {
?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? cerr<<"ctor: Triangle\n";
?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? creationMap["triangle"] = new
?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? Triangle();
?? ?? ?? ?? ?? ?? ?? ?? }
That default constructor looks like trouble. Perhaps you could move the
second line to another function.
The thing is, I need some way of creating an arbitrary object (in this
case a Shape) based on a given ID string.
Basically, I'm saving objects to/reading objects from a "chunky" file
format (a bit like EA IFF 85). The file is structured into chunks, which
have this format:
4-byte FOURCC (Chunk ID)
8-byte length
Chunk payload
A chunk may have children, in which case the MSBit of the length is set.
What I want is to have as little copy-pasted code as possible, while
still having easy-to-read code. For serialisation, I've got two functions
in Chunk:
vector<uint8_t> Serialise()
virtual vector<uint8_t> SerialisePayload() =0;
(there's also a pure-virtual getChunkID() fn which returns a 4-character
std::string containing the FOURCC code; this is implemented in all child
classes)
Chunk::Serialise calls this->SerialisePayload() to get the payload data,
then outputs the header and payload into a vector and returns it. The
idea being that the headers are common to all chunks, but payload data
depends on the specific class being serialised.
Now back to the deserialisation problem...
At this point I haven't even managed to get an example implementation of
the C++FAQ deserialiser working -- the static ctors aren't being called,
so the std::map doesn't contain anything, thus the code bombs (current
version throws an exception, the one I posted segfaults)...
Thanks,
Phil.