
Robert Ramey wrote:
Neal Becker wrote:
Robert Ramey wrote:
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.
...
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))); }
This doesn't seem right to me. real(z) returns a value. I don't see how this can be cast to reference to a const value without a lot of (conforming) compilers objecting. I believe the correct way will be:
const T r = real(z); ar << boost::serialization::make_nvp ("real", r); const T i = imag(z); ar << boost::serialization::make_nvp ("imag", i);
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++; } }
I wonder about this - but I know nothing of ublas. If v.resize actually creates the entries I guess it would be just fine. Now I wonder about my own implementation of serialization for std::vector. It seems yours here is more efficient.
You're right! I forgot, real() and imag() have been changed to now return a ref (or const ref). This works with gcc-4.0.1, at least. Does this look OK? Does the ublas::vector stuff also need some nvp added? namespace boost { namespace serialization { template<class T> struct implementation_level<std::complex<T> > { typedef mpl::integral_c_tag tag; // typedef mpl::int_<primitive_type> type; typedef mpl::int_<object_serializable> type; BOOST_STATIC_CONSTANT( int, value = implementation_level::type::value ); }; // // nvp objects are generally created on the stack and are never tracked 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", real (z)); ar << boost::serialization::make_nvp ("imag", imag (z)); } template<typename Archive, typename T> inline void load (Archive &ar, std::complex<T>& z, const unsigned int) { ar >> boost::serialization::make_nvp ("real", real(z)); ar >> boost::serialization::make_nvp ("imag", imag(z)); } 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); } } }