I
Ingo Nolden
Hi there,
I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl.
For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my
own.
One goal is to make it robust against version changes and corrupted files. That means, when an object cannot
be deserialized, it should be skipped. The validity of the remaining document can be verified later.
For this purpose I begin every object with a code that identifies the class. A factory can use it to construct
the class, giving it the node of the serialized stream.
The next is the size of the object. This way the object can be skipped if something is wrong. An earlier version
might not contain all the variables for a class, a later one may contain too much. The serializer or deserializer
methods of the class will have apropriate default values at hand.
The problem is that the size is of course unknown before th object is already serialized. For performance
matters I dont want to have a kind of size inquiry before the serialization and I dont want something to serialize
to another place and then copy to the real stream. Even though my files might not be going to be soo big, I
don't know what comes in the future.
So my way is to save the current stream position before the serialization, and then jump back and forth to put
the size in the right place.
My first question is: Does anyone now how costly that is? I guess not too much, but I don't really knwo about
the implementation of the streams.
Then I see that it sometimes doesn't behave as expected.
Ok, the cout can get the position it returns only zero. No problem since serializing to cout is not really useful
to me.
one might ask why I want to use this general ostream to serialize to. Well I dont know what it is going to do
later: saving docs, transaction ( undo/redo), network communication or just with another process, dragn drop...
therefore I think a general apporoach is ok.
Now the file stream sometime puts the very first int size not into the first place but shifted about one byte. THis
is weird, ugly and it corrupts the actual data. But it depends on the size of the actual data. It happens if it is
small, and depends on wether the size is odd or even !?! I tried to overcome this with an initializing "initialize"
string wich will be known and therefore needs no size info. But in this case the problem appears after this
initilaizing string. So it is something with my jumping around with the seekp() method.
The strstream is a little similar but the first data value is not corrupted. But the whole data is shifted by one bit
:-(.
So if one is really familiar with the streams: I just want to know what do I have to consider to make my code
work securely. It should not only work no, but in any case on any machine and on compilers different than my
VS.net2003 compiler
thanks for your help in advance. I tried to post source, but the server rejected. Probably too big. Ask if
necessary and I will post it
cheers
Ingo
I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl.
For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my
own.
One goal is to make it robust against version changes and corrupted files. That means, when an object cannot
be deserialized, it should be skipped. The validity of the remaining document can be verified later.
For this purpose I begin every object with a code that identifies the class. A factory can use it to construct
the class, giving it the node of the serialized stream.
The next is the size of the object. This way the object can be skipped if something is wrong. An earlier version
might not contain all the variables for a class, a later one may contain too much. The serializer or deserializer
methods of the class will have apropriate default values at hand.
The problem is that the size is of course unknown before th object is already serialized. For performance
matters I dont want to have a kind of size inquiry before the serialization and I dont want something to serialize
to another place and then copy to the real stream. Even though my files might not be going to be soo big, I
don't know what comes in the future.
So my way is to save the current stream position before the serialization, and then jump back and forth to put
the size in the right place.
My first question is: Does anyone now how costly that is? I guess not too much, but I don't really knwo about
the implementation of the streams.
Then I see that it sometimes doesn't behave as expected.
Ok, the cout can get the position it returns only zero. No problem since serializing to cout is not really useful
to me.
one might ask why I want to use this general ostream to serialize to. Well I dont know what it is going to do
later: saving docs, transaction ( undo/redo), network communication or just with another process, dragn drop...
therefore I think a general apporoach is ok.
Now the file stream sometime puts the very first int size not into the first place but shifted about one byte. THis
is weird, ugly and it corrupts the actual data. But it depends on the size of the actual data. It happens if it is
small, and depends on wether the size is odd or even !?! I tried to overcome this with an initializing "initialize"
string wich will be known and therefore needs no size info. But in this case the problem appears after this
initilaizing string. So it is something with my jumping around with the seekp() method.
The strstream is a little similar but the first data value is not corrupted. But the whole data is shifted by one bit
:-(.
So if one is really familiar with the streams: I just want to know what do I have to consider to make my code
work securely. It should not only work no, but in any case on any machine and on compilers different than my
VS.net2003 compiler
thanks for your help in advance. I tried to post source, but the server rejected. Probably too big. Ask if
necessary and I will post it
cheers
Ingo