Skip to content

Commit 71514c5

Browse files
authored
Merge pull request #487 from m-a-d-n-e-s-s/evaleev/fix/serialize-allocator
serialization of allocators
2 parents 22b860d + e432970 commit 71514c5

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/madness/world/archive.h

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,38 @@ namespace madness {
11281128
}
11291129
};
11301130

1131+
/// Serialize a \c std::allocator.
1132+
1133+
/// This is a no-op.
1134+
/// \tparam Archive the archive type.
1135+
/// \tparam T The data type allocated by the \c allocator.
1136+
template <class Archive, typename T>
1137+
struct ArchiveStoreImpl< Archive, std::allocator<T>, std::enable_if_t<!is_future<T>::value && is_serializable_v<Archive, T>> > {
1138+
1139+
/// Storing a \c std::allocator is a no-op
1140+
1141+
/// \param[in] ar The archive.
1142+
/// \param[in] v The \c allocator.
1143+
static inline void store(const Archive& ar, const std::allocator<T>& v) {
1144+
}
1145+
};
1146+
1147+
1148+
/// Deserialize a \c std::allocator.
1149+
1150+
/// This is a no-op.
1151+
/// \tparam Archive the archive type.
1152+
/// \tparam T The data type alllocated by in the \c allocator.
1153+
template <class Archive, typename T>
1154+
struct ArchiveLoadImpl< Archive, std::allocator<T>, std::enable_if_t<!is_future<T>::value && is_serializable_v<Archive, T>> > {
1155+
1156+
/// Loading a \c std::allocator is a no-op
1157+
1158+
/// \param[in] ar The archive.
1159+
/// \param[out] v The \c allocator.
1160+
static void load(const Archive& ar, std::allocator<T>& v) {
1161+
}
1162+
};
11311163

11321164
/// Serialize a \c std::vector.
11331165

@@ -1143,6 +1175,9 @@ namespace madness {
11431175
/// \param[in] v The \c vector.
11441176
static inline void store(const Archive& ar, const std::vector<T, Alloc>& v) {
11451177
MAD_ARCHIVE_DEBUG(std::cout << "serialize std::vector of plain data" << std::endl);
1178+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1179+
ar & v.get_allocator();
1180+
}
11461181
ar & v.size();
11471182
ar & wrap(v.data(),v.size());
11481183
}
@@ -1164,6 +1199,11 @@ namespace madness {
11641199
/// \param[out] v The \c vector.
11651200
static void load(const Archive& ar, std::vector<T, Alloc>& v) {
11661201
MAD_ARCHIVE_DEBUG(std::cout << "deserialize std::vector of plain data" << std::endl);
1202+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1203+
Alloc allocator;
1204+
ar & allocator;
1205+
v = std::vector<T, Alloc>(allocator);
1206+
}
11671207
std::size_t n = 0ul;
11681208
ar & n;
11691209
if (n != v.size()) {
@@ -1188,6 +1228,9 @@ namespace madness {
11881228
/// \param[in] v The \c vector.
11891229
static inline void store(const Archive& ar, const std::vector<bool, Alloc>& v) {
11901230
MAD_ARCHIVE_DEBUG(std::cout << "serialize std::vector<bool>" << std::endl);
1231+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1232+
ar & v.get_allocator();
1233+
}
11911234
std::size_t n = v.size();
11921235
bool* b = new bool[n];
11931236
for (std::size_t i=0; i<n; ++i) b[i] = v[i];
@@ -1210,6 +1253,11 @@ namespace madness {
12101253
/// \param[out] v The \c vector.
12111254
static void load(const Archive& ar, std::vector<bool, Alloc>& v) {
12121255
MAD_ARCHIVE_DEBUG(std::cout << "deserialize std::vector<bool>" << std::endl);
1256+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1257+
Alloc allocator;
1258+
ar & allocator;
1259+
v = std::vector<bool, Alloc>(allocator);
1260+
}
12131261
std::size_t n = 0ul;
12141262
ar & n;
12151263
if (n != v.size()) {
@@ -1403,6 +1451,9 @@ namespace madness {
14031451
/// \param[in] t The \c map.
14041452
static void store(const Archive& ar, const std::map<T,Q,Compare,Alloc>& t) {
14051453
MAD_ARCHIVE_DEBUG(std::cout << "serialize std::map" << std::endl);
1454+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1455+
ar & t.get_allocator();
1456+
}
14061457
ar << t.size();
14071458
for (auto p = t.begin();
14081459
p != t.end(); ++p) {
@@ -1434,12 +1485,19 @@ namespace madness {
14341485
/// \param[out] t The \c map.
14351486
static void load(const Archive& ar, std::map<T,Q,Compare,Alloc>& t) {
14361487
MAD_ARCHIVE_DEBUG(std::cout << "deserialize std::map" << std::endl);
1488+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1489+
Alloc allocator;
1490+
ar & allocator;
1491+
t = std::map<T,Q,Compare,Alloc>(allocator);
1492+
}
1493+
else
1494+
t.clear();
14371495
std::size_t n = 0;
14381496
ar & n;
14391497
while (n--) {
14401498
std::pair<T,Q> p;
14411499
ar & p;
1442-
t[p.first] = p.second;
1500+
t.emplace(std::move(p.first), std::move(p.second));
14431501
}
14441502
}
14451503
};
@@ -1460,6 +1518,9 @@ namespace madness {
14601518
/// \param[in] s The \c set.
14611519
static inline void store(const Archive& ar, const std::set<T, Compare, Alloc>& s) {
14621520
MAD_ARCHIVE_DEBUG(std::cout << "serialize std::set" << std::endl);
1521+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1522+
ar & s.get_allocator();
1523+
}
14631524
ar << s.size();
14641525
for (const auto &i : s)
14651526
ar << i;
@@ -1481,6 +1542,11 @@ namespace madness {
14811542
/// \param[out] s The \c set.
14821543
static void load(const Archive& ar, std::set<T, Compare, Alloc>& s) {
14831544
MAD_ARCHIVE_DEBUG(std::cout << "deserialize std::set" << std::endl);
1545+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1546+
Alloc allocator;
1547+
ar & allocator;
1548+
s = std::set<T, Compare, Alloc>(allocator);
1549+
}
14841550
std::size_t size;
14851551
ar >> size;
14861552
s.clear();
@@ -1510,6 +1576,9 @@ namespace madness {
15101576
/// \param[in] s The \c list.
15111577
static inline void store(const Archive& ar, const std::list<T, Alloc>& s) {
15121578
MAD_ARCHIVE_DEBUG(std::cout << "serialize std::list" << std::endl);
1579+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1580+
ar & s.get_allocator();
1581+
}
15131582
ar << s.size();
15141583
for (const auto &i : s)
15151584
ar << i;
@@ -1530,6 +1599,11 @@ namespace madness {
15301599
/// \param[out] s The \c list.
15311600
static void load(const Archive& ar, std::list<T, Alloc>& s) {
15321601
MAD_ARCHIVE_DEBUG(std::cout << "deserialize std::list" << std::endl);
1602+
if constexpr (!std::allocator_traits<Alloc>::is_always_equal::value) {
1603+
Alloc allocator;
1604+
ar & allocator;
1605+
s = std::list<T, Alloc>(allocator);
1606+
}
15331607
std::size_t size;
15341608
ar >> size;
15351609
s.clear();

0 commit comments

Comments
 (0)