Merge pull request #2733 from joakim-hove/serialize-variant

Serialize variant
This commit is contained in:
Bård Skaflestad 2020-08-25 23:10:18 +02:00 committed by GitHub
commit 9829bfd61a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,7 +97,7 @@ public:
}
//! \brief Handler for std::variant<> with three types
//! \brief Handler for std::variant<> with four types
/*
The std::variant<> serialization is a first attempt and *not* particularly
@ -147,6 +147,52 @@ public:
}
//! \brief Handler for std::variant<> with two fundamental types
/*
This std::variant serialization is highly specialized:
1. It is hardcoded to take exactly two types T0 and T1.
2. Both T0 and T1 must be basic types where Mpi::pack(T, ...) overloads
must exist.
*/
template<class T0, class T1>
void variant(std::variant<T0,T1>& data)
{
auto pack_size = [&](auto& d) {
m_packSize += Mpi::packSize(d, m_comm);
};
auto pack = [&](auto& d) {
Mpi::pack(d, m_buffer, m_position, m_comm);
};
if (m_op == Operation::PACKSIZE) {
m_packSize += Mpi::packSize(data.index(), m_comm);
std::visit( [&] (auto& arg) { pack_size(arg); }, data);
} else if (m_op == Operation::PACK) {
Mpi::pack(data.index(), m_buffer, m_position, m_comm);
std::visit([&](auto& arg) { pack(arg); }, data);
} else if (m_op == Operation::UNPACK) {
size_t index;
Mpi::unpack(index, m_buffer, m_position, m_comm);
if (index == 0) {
T0 t0;
Mpi::unpack(t0, m_buffer, m_position, m_comm);
data = t0;
} else if (index == 1) {
T1 t1;
Mpi::unpack(t1, m_buffer, m_position, m_comm);
data = t1;
} else
throw std::logic_error("Internal meltdown in std::variant<T0,T1> unpack loaded index=" + std::to_string(index) + " allowed range: [0,1]");
}
}
//! \brief Handler for maps.
//! \tparam Map map type
//! \tparam complexType Whether or not Data in map is a complex type