Merge pull request #2574 from joakim-hove/serialize-optional

Add support for serializing std::optional
This commit is contained in:
Joakim Hove 2020-04-29 10:48:43 +02:00 committed by GitHub
commit ec647bcc16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -79,6 +79,17 @@ std::size_t packSize(const std::pair<T1,T2>& data, Dune::MPIHelper::MPICommunica
return packSize(data.first, comm) + packSize(data.second, comm);
}
template<class T>
std::size_t packSize(const std::optional<T>& data, Dune::MPIHelper::MPICommunicator comm)
{
bool has_value = data.has_value();
std::size_t pack_size = packSize(has_value, comm);
if (has_value)
pack_size += packSize(*data, comm);
return pack_size;
}
template<class T, class A>
std::size_t packSize(const std::vector<T,A>& data, Dune::MPIHelper::MPICommunicator comm)
{
@ -282,6 +293,17 @@ void pack(const std::pair<T1,T2>& data, std::vector<char>& buffer, int& position
pack(data.second, buffer, position, comm);
}
template<class T>
void pack(const std::optional<T>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm)
{
bool has_value = data.has_value();
pack(has_value, buffer, position, comm);
if (has_value)
pack(*data, buffer, position, comm);
}
template<class T, class A>
void pack(const std::vector<T, A>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm)
@ -510,6 +532,19 @@ void unpack(std::pair<T1,T2>& data, std::vector<char>& buffer, int& position,
unpack(data.second, buffer, position, comm);
}
template<class T>
void unpack(std::optional<T>&data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm)
{
bool has_value;
unpack(has_value, buffer, position, comm);
if (has_value)
unpack(*data, buffer, position, comm);
else
data.reset();
}
template<class T, class A>
void unpack(std::vector<T,A>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm)
@ -764,6 +799,7 @@ INSTANTIATE_PACK(std::array<bool,3>)
INSTANTIATE_PACK(std::array<int,3>)
INSTANTIATE_PACK(unsigned char)
INSTANTIATE_PACK(std::map<std::pair<int,int>,std::pair<bool,double>>)
INSTANTIATE_PACK(std::optional<std::pair<double,double>>)
INSTANTIATE_PACK(std::map<std::string,std::vector<int>>)
INSTANTIATE_PACK(std::map<std::string,std::map<std::pair<int,int>,int>>)
INSTANTIATE_PACK(std::map<std::string,int>)

View File

@ -29,11 +29,12 @@
#include <dune/common/parallel/mpihelper.hh>
#include <optional>
#include <map>
#include <set>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <vector>
namespace Opm
{
@ -81,6 +82,9 @@ std::size_t packSize(const T& data, Dune::MPIHelper::MPICommunicator comm)
template<class T1, class T2>
std::size_t packSize(const std::pair<T1,T2>& data, Dune::MPIHelper::MPICommunicator comm);
template<class T>
std::size_t packSize(const std::optional<T>& data, Dune::MPIHelper::MPICommunicator comm);
template<class T, class A>
std::size_t packSize(const std::vector<T,A>& data, Dune::MPIHelper::MPICommunicator comm);
@ -158,6 +162,10 @@ template<class T1, class T2>
void pack(const std::pair<T1,T2>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm);
template<class T>
void pack(const std::optional<T>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm);
template<class T, class A>
void pack(const std::vector<T,A>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm);
@ -243,6 +251,10 @@ template<class T1, class T2>
void unpack(std::pair<T1,T2>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm);
template<class T>
void unpack(std::optional<T>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm);
template<class T, class A>
void unpack(std::vector<T,A>& data, std::vector<char>& buffer, int& position,
Dune::MPIHelper::MPICommunicator comm);