Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-11-19 12:58:08


Here is a little program which shows how you re-implement serialization of
std::list<nvp<...
to your taste. The code would only apply to these kinds of lists as only in
this case would the template arguments match. I've included different
versions depending upon whether you want to apply the change to all archives
or just xml archives. I've compiled it on VC 7.1. To actually run it, it
would have to be enhanced somewhat to include serialization of the item
count and maybe some other functionality of the current default
implementation.

Robert Ramey

#include <list>
#include <fstream>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
namespace boost {
namespace serialization {
// replace default serialization for lists of nvp in ALL archives
template<class Archive, class U, class Allocator>
inline void serialize(
Archive & ar,
std::list<boost::serialization::nvp<U>, Allocator> & t,
const unsigned int version
){
// r
std::list<boost::serialization::nvp<U>, Allocator>::iterator itr =
t.begin();
while(itr != t.end()){
ar & *itr;
itr++;
}
}
// OR
// replace default serialization for lists of nvp only in xml_archives
template<class U, class Allocator>
inline void serialize(
boost::archive::xml_oarchive & ar,
std::list<boost::serialization::nvp<U>, Allocator> & t,
const unsigned int version
){
std::list<boost::serialization::nvp<U>, Allocator>::iterator itr =
t.begin();
while(itr != t.end()){
ar & *itr;
itr++;
}
}
template<class U, class Allocator>
inline void serialize(
boost::archive::xml_iarchive & ar,
std::list<boost::serialization::nvp<U>, Allocator> & t,
const unsigned int version
){
// r
std::list<boost::serialization::nvp<U>, Allocator>::iterator itr =
t.begin();
while(itr != t.end()){
ar & *itr;
itr++;
}
}
} // serialization
} // boost
main(int arg, char *argv[]){
std::ofstream os("test");
boost::archive::xml_oarchive oa(os);
const std::list<boost::serialization::nvp<int> > l;
oa << BOOST_SERIALIZATION_NVP(l);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net