Boost logo

Boost :

From: Neal Becker (ndbecker2_at_[hidden])
Date: 2005-08-26 20:02:16


Robert Ramey wrote:

> I checked the documentation and I see your point. I'll make adjustments to
> it.
>
> Considering more carefully what traits should be std::complex<T> I'm
> thinking now they should be:
>
> implementation level - object_serializable.
>
> This will guarentee that your serialization functions are called rather
> than using stream i/o but will not add class id or versioning information
> for the
> class. This presumes that the method of serialization for this
> std::complex will not change in the future - a fair assumption I believe.
>
> tracking - track_never I see std::complex as a type used in disparte
> places
> in the application and akin to float/complex etc. So I would mark this as
> track_never.
>
> Robert Ramey

OK, so how does this look? This implements std::complex and ublas::vector.
Does not yet support allocator paramater to ublas::vector, but I don't
think that would be difficult to add.

namespace ublas = boost::numeric::ublas;
namespace boost {
namespace serialization {

template<class T>
struct implementation_level<std::complex<T> >
{
  typedef mpl::integral_c_tag tag;
  typedef mpl::int_<object_serializable> type;
  BOOST_STATIC_CONSTANT(
                        int,
                        value = implementation_level::type::value
                        );
};

template<class T>
struct tracking_level<std::complex<T> >
{
  typedef mpl::integral_c_tag tag;
  typedef mpl::int_<track_never> type;
  BOOST_STATIC_CONSTANT(
                        int,
                        value = tracking_level::type::value
                        );

};

}
}

template<typename Archive, typename T>
inline void save (Archive &ar, const std::complex<T>& z, const unsigned int)
{
  ar << boost::serialization::make_nvp ("real", const_cast<const T&>(real
(z)));
  ar << boost::serialization::make_nvp ("imag", const_cast<const T&>(imag
(z)));
}

template<typename Archive, typename T>
inline void load (Archive &ar, std::complex<T>& z, const unsigned int) {
  T i, r;
  ar >> boost::serialization::make_nvp ("real", r);
  ar >> boost::serialization::make_nvp ("imag", i);
  z = std::complex<T> (r, i);
}

namespace boost { namespace serialization {
template<class Archive, class T>
inline void serialize (Archive &ar, std::complex<T>& z, const unsigned int
file_version) {
  boost::serialization::split_free (ar, z, file_version);
}
}
}

template<class Archive, class U>
inline void save (Archive &ar, const ublas::vector<U> &v, const unsigned
int) {
  unsigned int count = v.size();
  ar << count;
  typename ublas::vector<U>::const_iterator it = v.begin();
  while (count-- > 0) {
    ar << *it++;
  }
}

template<class Archive, class U>
inline void load (Archive &ar, ublas::vector<U> &v, const unsigned int) {
  unsigned int count;
  ar >> count;
  v.resize (count);
  typename ublas::vector<U>::iterator it = v.begin();
  while (count-- > 0) {
    ar >> *it++;
  }
}

namespace boost { namespace serialization {
template<class Archive, class U>
inline void serialize (Archive &ar, ublas::vector<U>& v, const unsigned int
file_version) {
  boost::serialization::split_free (ar, v, file_version);
}
}
}


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