Boost logo

Boost :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2004-04-26 09:44:39


"Pavel Vozenilek" <pavel_vozenilek_at_[hidden]> wrote in message
news:c6head$r3r$1_at_sea.gmane.org...
>
> "Jeff Flinn" <TriumphSprint2000_at_[hidden]> wrote:
>
> > While the current methods for Free Functions and Splitting save/load
> methods
> > are certainly understandable and work. It would be nice to be able
> > (partial)specialize the "serialize" function base on its IO type. It
would
> > also be nice to utilize enable_if to generate different code when the
> > serialize function is instantiated for input/output archives. This would
> > allow compile time constructs similar to mfc's runtime Ar.IsLoad().
> >
>
> template<typename Archive>
> bool is_saving(const Archive& arch) {
> return boost::is_base_and_derived<boost::archive::basic_oarchive,
> Archive>::value;
> }
>
> Can be used like:
>
> template<typename Archive>
> void serialize(Archive & ar, const unsigned int version) {
> ar & value_1;
> ...
> ar & value_n
> if (is_saving(ar)) {
> ...
> } else {
> ..
> }
> ar & value_n+1;
> ...
> }

Thanks Pavel!

template<typename Archive> bool is_saving ( const Archive& )
{ return
boost::is_base_and_derived<boost::archive::basic_oarchive,Archive>::value; }
template<typename Archive> bool is_loading( const Archive& )
{ return
boost::is_base_and_derived<boost::archive::basic_iarchive,Archive>::value; }

This condenses the boost::filesystem::path save/load to a single serialize:

namespace boost {
namespace serialization {

   template<class Archive>
   inline void serialize( Archive& ar
                         , boost::filesystem::path& t
                         , unsigned int version )
   {
      std::string lPathString;

      if( is_saving(ar) )
        lPathString = t.native_file_string();

      ar & make_nvp( "PathString", lPathString );

      if( is_loading(ar) )
         t = boost::filesystem::path( lPathString,
boost::filesystem::native );
   }
}
}

This is even more attractive for more complex classes with only a few split
members. Robert, would these function be able to be added to the library?

Jeff F


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk