Skip to content

Commit

Permalink
Add valarray serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzv committed Jul 5, 2016
1 parent 0ab94c2 commit 684b200
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions include/diy/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DIY_SERIALIZATION_HPP

#include <vector>
#include <valarray>
#include <map>
#include <set>
#include <string>
Expand Down Expand Up @@ -205,6 +206,34 @@ namespace diy
}
};

template<class U>
struct Serialization< std::valarray<U> >
{
typedef std::valarray<U> ValArray;

static void save(BinaryBuffer& bb, const ValArray& v)
{
size_t s = v.size();
diy::save(bb, s);
#if __cplusplus > 199711L // C++11
diy::save(bb, &v[0], v.size());
#else
// Before C++11 valarray::operator[] const returns by value, not const
// reference, so we cannot dereference it and pass directly to save.
for (size_t i = 0; i < v.size(); ++i)
diy::save(bb, v[i]);
#endif
}

static void load(BinaryBuffer& bb, ValArray& v)
{
size_t s;
diy::load(bb, s);
v.resize(s);
diy::load(bb, &v[0], s);
}
};

// save/load for std::string
template<>
struct Serialization< std::string >
Expand Down

0 comments on commit 684b200

Please sign in to comment.