eclmpiserializer: consistently make immutable access the exception

by nature of the operation, the data should be immutable on sender and
mutable on receiver. this way we atleast keep data immutable where we can,
and immutable access is explicitly marked by casts.
This commit is contained in:
Arne Morten Kvarving
2022-09-08 10:21:55 +02:00
parent d31620b38b
commit 62f22062dd

View File

@@ -94,13 +94,13 @@ public:
} else if constexpr (is_optional<T>::value) {
optional(data);
} else if constexpr (is_vector<T>::value) {
vector(const_cast<T&>(data));
vector(data);
} else if constexpr (is_map<T>::value) {
map(const_cast<T&>(data));
map(data);
} else if constexpr (is_array<T>::value) {
array(const_cast<T&>(data));
array(data);
} else if constexpr (is_set<T>::value) {
set(const_cast<T&>(data));
set(data);
} else if constexpr (has_serializeOp<detail::remove_cvr_t<T>>::value) {
const_cast<T&>(data).serializeOp(*this);
} else {
@@ -117,7 +117,7 @@ public:
//! \tparam T Type for vector elements
//! \param data The vector to (de-)serialize
template <typename T>
void vector(std::vector<T>& data)
void vector(const std::vector<T>& data)
{
if constexpr (std::is_pod_v<T>) {
if (m_op == Operation::PACKSIZE) {
@@ -129,15 +129,17 @@ public:
} else if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
data.resize(size);
Mpi::Packer::unpack(data.data(), size, m_buffer, m_position, m_comm);
auto& data_mut = const_cast<std::vector<T>&>(data);
data_mut.resize(size);
Mpi::Packer::unpack(data_mut.data(), size, m_buffer, m_position, m_comm);
}
} else {
if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
data.resize(size);
std::for_each(data.begin(), data.end(), std::ref(*this));
auto& data_mut = const_cast<std::vector<T>&>(data);
data_mut.resize(size);
std::for_each(data_mut.begin(), data_mut.end(), std::ref(*this));
} else {
(*this)(data.size());
std::for_each(data.begin(), data.end(), std::ref(*this));
@@ -147,17 +149,18 @@ public:
//! \brief Handler for bool vectors.
//! \param data The vector to (de-)serialize
void vector(std::vector<bool>& data)
void vector(const std::vector<bool>& data)
{
if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
data.clear();
data.reserve(size);
auto& data_mut = const_cast<std::vector<bool>&>(data);
data_mut.clear();
data_mut.reserve(size);
for (size_t i = 0; i < size; ++i) {
bool entry = false;
(*this)(entry);
data.push_back(entry);
data_mut.push_back(entry);
}
} else {
(*this)(data.size());
@@ -171,7 +174,7 @@ public:
//! \brief Handler for arrays.
//! \param data The array to (de-)serialize
template <class Array>
void array(Array& data)
void array(const Array& data)
{
using T = typename Array::value_type;
@@ -180,8 +183,10 @@ public:
m_packSize += Mpi::Packer::packSize(data.data(), data.size(), m_comm);
else if (m_op == Operation::PACK)
Mpi::Packer::pack(data.data(), data.size(), m_buffer, m_position, m_comm);
else if (m_op == Operation::UNPACK)
Mpi::Packer::unpack(data.data(), data.size(), m_buffer, m_position, m_comm);
else if (m_op == Operation::UNPACK) {
auto& data_mut = const_cast<Array&>(data);
Mpi::Packer::unpack(data_mut.data(), data_mut.size(), m_buffer, m_position, m_comm);
}
} else {
std::for_each(data.begin(), data.end(), std::ref(*this));
}
@@ -238,15 +243,16 @@ public:
//! \tparam Map map type
//! \param map The map to (de-)serialize
template<class Map>
void map(Map& data)
void map(const Map& data)
{
if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
auto& data_mut = const_cast<Map&>(data);
for (size_t i = 0; i < size; ++i) {
typename Map::value_type entry;
(*this)(entry);
data.insert(entry);
data_mut.insert(entry);
}
} else {
(*this)(data.size());
@@ -258,15 +264,16 @@ public:
//! \tparam Set set type
//! \param data The set to (de-)serialize
template<class Set>
void set(Set& data)
void set(const Set& data)
{
if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
auto& data_mut = const_cast<Set&>(data);
for (size_t i = 0; i < size; ++i) {
typename Set::value_type entry;
(*this)(entry);
data.insert(entry);
data_mut.insert(entry);
}
} else {
(*this)(data.size());
@@ -278,7 +285,7 @@ public:
//! \tparam T Type of class to serialize
//! \param data Class to serialize
template<class T>
void pack(T& data)
void pack(const T& data)
{
m_op = Operation::PACKSIZE;
m_packSize = 0;