Re:[boost] serialization: buffers

I've a class like this:
class Example{ int str[BSize]; int *p; };
Where p points to next available element (p==str+BSize is possible when it is full). I.e. str is like begin() and p is like end(). I want to add a serialize() function, something like this:
template<class Archive> void serialize(Archive &ar,const unsigned int){ ar & BOOST_SERIALIZATION_NVP(buf,p); }
but there seems to be no support for buffers like this?
Of course the following would work: ar & BOOST_SERIALIZATION_NVP(buf,str); but it would copy too many elements. Or you might try the following (non-portable) idea ar & BOOST_SERIALIZATION_NVP(buf, make_binary_object((p - str)* sizeof(int), str) ); But really, I think you might be best off replacing the class above with class Example { std::slist<int> str; // or maybe std::vector<int> str } For which serialization is already implemented. Robert Ramey

class Example{ int str[BSize]; int *p; }; .... Or you might try the following (non-portable) idea
ar & BOOST_SERIALIZATION_NVP( make_binary_object(str, (p - str)* sizeof(int)) );
Thanks, that is closest to what I was after; I don't need a portable version here.
But really, I think you might be best off replacing the class above with
class Example { std::slist<int> str; // or maybe std::vector<int> str }
It was done using a fixed size buffer to avoid dynamic memory allocation (mainly for speed reasons). Serializing is relatively rare, but when it happens there will be a very large number of objects so I didn't want the full BSize elements stored. However as I worked through the class some more I found it can output a string representation, which uses fewer bytes. So in future I might add a parser for input and then switch to using that (though I'll be back to needing separate load/save functions then won't I, oh well :-). Darren
participants (2)
-
Darren Cook
-
Robert Ramey