Boost logo

Boost Users :

Subject: Re: [Boost-users] [multi_array][serialization]
From: er (erwann.rogard_at_[hidden])
Date: 2009-11-14 23:00:26


er wrote:
> The issue of serializing a multi-array came up here:
>
> http://lists.boost.org/boost-users/2007/06/28639.php
>
> I was wondering if anyone had followed up on this and would be willing
> to share his/her code.

Here's the code based on the above suggestions. I would have thought
that overloading serialize (see below) would permit oa << array and ia
>> array, but not (compile error : array has no member serialize).
Therefore, if I want to embed multi_array in another data-structure,
what is the next step?

Thanks in advance.

     template<typename T,class Archive>
     void load(
         Archive & ar,
         boost::multi_array<T,2> & t,
         const unsigned int file_version
     )
     {
                typedef boost::multi_array<T,2> multi_array_;
                typedef typename multi_array_::size_type size_;

         size_ n0;
         ar >> BOOST_SERIALIZATION_NVP(n0);
         size_ n1;
         ar >> BOOST_SERIALIZATION_NVP(n1);

         t.resize(boost::extents[n0][n1]);
         ar >> make_array(t.data(), t.num_elements());
     }

     template<typename T,typename Archive>
     void save(
         Archive & ar,
         const boost::multi_array<T,2> & t,
         const unsigned int file_version
     )
        {
             typedef boost::multi_array<T,2> multi_array_;
         typedef typename multi_array_::size_type size_;

         size_ n0 = (t.shape()[0]);
         ar << BOOST_SERIALIZATION_NVP(n0);
         size_ n1 = (t.shape()[1]);
         ar << BOOST_SERIALIZATION_NVP(n1);
         ar << boost::serialization::make_array(t.data(),
t.num_elements());
     }

     template<typename T,class Archive>
     void serialize(
         Archive & ar,
         const boost::multi_array<T,2>& t,
         const unsigned int file_version
     )
     {
         split_free(ar, t, file_version);
     }


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