./test
test:
/home/goetz/local/src/boost-trunk/boost/multi_array/multi_array_ref.hpp:487:
boost::multi_array_ref& boost::multi_array_ref::operator=(const ConstMultiArray&) [with ConstMultiArray =
boost::multi_array, T = float, long unsigned int NumDims = 1ul,
boost::multi_array_ref = boost::multi_array_ref]:
Assertion `std::equal(other.shape(),other.shape()+this->num_dimensions(),
I am trying to serialize a multi_array object but keep the dimensions
variable (I only want to support 1, 2 and 3 dimensions). So I attempted to
create a Variant of the three multi_array's and the corresponding serialize
methods as shown below, but I am getting a run-time error when for the
1-Dimensional case. Here is the minimal code example I came up with that
shows the problem. I am using the boost SVN trunk as of this posting (April
26, 2011).
Maybe I am implementing the load() method incorrectly for the multi_array.
Should there be a special case for when Dim == 1?
Running the program, I get the following error:
this->shape())' failed.
Aborted (core dumped)
/// Begin File: multiarray-variant-serialize.cpp
/**
* gcc compile command:
* g++ multiarray-variant-serialize.cpp -otest -lboost_serialization
*
* to run:
* ./test
**/
#include <fstream>
#include <string>
#include
#include
#include
#include
#include
typedef boost::variant<
boost::multi_array,
boost::multi_array,
boost::multi_array
> multi_array_variant;
namespace boost {
namespace serialization {
template
void save(
Archive& ar,
const boost::multi_array& ma,
const unsigned int version)
{
ar << boost::serialization::make_array(ma.shape(), Dim);
ar << boost::serialization::make_array(ma.data(),
ma.num_elements());
}
template
void load(
Archive& ar,
boost::multi_array& ma,
const unsigned int version)
{
typedef typename boost::multi_array::index index_t;
boost::array shape;
ar >> boost::serialization::make_array(shape.data(), Dim);
ma.resize(shape);
ar >> boost::serialization::make_array(ma.data(),
ma.num_elements());
}
template
void serialize(
Archive & ar,
boost::multi_array& ma,
const unsigned int version)
{
split_free(ar, ma, version);
}
} /// namespace boost::serialization
} /// namespace boost
int main()
{
boost::multi_array a1(boost::extents[5]);
/// replacing the above line with either of the following two
/// works as anticipated. Only the 1-Dimension breaks
/// breaks at run-time.
//boost::multi_array a1(boost::extents[5][5]);
//boost::multi_array a1(boost::extents[5][5][5]);
multi_array_variant a3 = a1;
multi_array_variant a4;
std::string filename = "archive.tmp";
{
std::ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
oa << a3;
}
{
std::ifstream ifs(filename.c_str());
boost::archive::text_iarchive ia(ifs);
ia >> a4;
}
return 0;
}
/// EOF
--
Johann T. Goetz