[boost] [serialization] New Primitive Types

Hi, If I create a new class, my_class, am I allowed to mark it as follows: BOOST_CLASS_IMPLEMENTATION ( my_class , boost::serialization::primitive_type ) The comments in level.hpp say that it won't call serialization code, thus I believe (and experience) that I don't need to provide either: template<class Archive> void my_class::serialize(Archive &ar, const unsigned int version) or template<class Archive> void serialize(Archive &ar, my_class &t, const unsigned int version) The comments also say that 'It presumes a member function or template in the archive class that can handle this type.' Does this mean that current archives (text, xml, and binary) are not able to use new primitive types without altering the archive to handle the new primitive? As I try it, both the text and xml archives use stream operators to convert my_class to a string and archive that. The binary archive casts my_class to a void* and outputs sizeof(my_class) bytes to the archive. It this expected behavior? Thanks, Andy.

Andy Tompkins wrote:
Hi,
If I create a new class, my_class, am I allowed to mark it as follows:
BOOST_CLASS_IMPLEMENTATION ( my_class , boost::serialization::primitive_type )
The comments in level.hpp say that it won't call serialization code, thus I believe (and experience) that I don't need to provide either:
template<class Archive> void my_class::serialize(Archive &ar, const unsigned int version) or template<class Archive> void serialize(Archive &ar, my_class &t, const unsigned int version)
correct.
The comments also say that 'It presumes a member function or template in the archive class that can handle this type.'
correct.
Does this mean that current archives (text, xml, and binary) are not able to use new primitive types without altering the archive to handle the new primitive?
My first answer was "correct"
As I try it, both the text and xml archives use stream operators to convert my_class to a string and archive that. The binary archive casts my_class to a void* and outputs sizeof(my_class) bytes to the archive. It this expected behavior?
But now you make me think about it, It turns out that all the archives mentioned have a default "fallback" for anything marked "primitive". So, a) You could mark anything primitive. b) In binary archives it will still work just by copying/restoring the bitstream c) In text based archives it will work if and only if there are stream << and >> operators for the type. Robert Ramey

< snip >
So,
a) You could mark anything primitive. b) In binary archives it will still work just by copying/restoring the bitstream c) In text based archives it will work if and only if there are stream << and >> operators for the type.
Robert Ramey
This is exactly what I wanted to know. Thank you very much, Andy.

Robert Ramey skrev:
So,
a) You could mark anything primitive. b) In binary archives it will still work just by copying/restoring the bitstream c) In text based archives it will work if and only if there are stream << and >> operators for the type.
But there's a problem here and I don't know how to fix it... I use the xml archive format and tried to define operators << and >> (non-intrusively) and saving the archive for my juce::String works fine, however, loading doesn't work. Say that the tag looks like this in my saved xml file: <MyTestValue>Yo, this is the test value!</MyTestValue> When loading it, my variable gets the value "Yo, this is the test value!</MyTestValue>" instead of the value between start and end tag, and the whole load process then crashes of course. How is the std::string/std::wstring handled since they obviously are saved/loaded correctly? /R

Robert Bielik wrote:
How is the std::string/std::wstring handled since they obviously are saved/loaded correctly?
Off hand I don't remember, you'd have to look into the implemenation of xml_?archive. And maybe into the xml input parser built with spirit. Or maybe you >> operator isn't checking for the tag> so it doesn't know when to stop.? Robert Ramey
/R

Robert Ramey skrev:
Or maybe you >> operator isn't checking for the tag> so it doesn't know when to stop.?
I'm definitely not looking for the tag> in the >> operator. Is it guaranteed that the character sequence is not to be found in the value itself (i.e. it is escaped)? In the case of the xml archive I'd rather have some other kind of operator that passes the value itself, not leaving the >> operator of each class to have to check for the character sequence, something like: template<class char_type> void operator () (const char_type* start, const char_type* end) { } which hides the archive implementation, and only passes on the correct value when loading the archive. For saving, the << operator is just fine. /R

I think you should look into xml_?archive to see how std::string is serialized and work from that. Robert Ramey Robert Bielik wrote:
Robert Ramey skrev:
Or maybe you >> operator isn't checking for the tag> so it doesn't know when to stop.?
I'm definitely not looking for the tag> in the >> operator. Is it guaranteed that the character sequence is not to be found in the value itself (i.e. it is escaped)?
In the case of the xml archive I'd rather have some other kind of operator that passes the value itself, not leaving the >> operator of each class to have to check for the character sequence, something like:
template<class char_type> void operator () (const char_type* start, const char_type* end) { }
I don't think making the item "primitive" is the way to go here.
which hides the archive implementation, and only passes on the correct value when loading the archive. For saving, the << operator is just fine.
/R
participants (3)
-
Andy Tompkins
-
Robert Bielik
-
Robert Ramey