Loading...
Searching...
No Matches
Serializer.hpp
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef SERIALIZER_HPP
#define SERIALIZER_HPP
#include <algorithm>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>
#if HAVE_DUNE_COMMON
namespace Dune { template<typename,int> class FieldVector; }
#endif
#if HAVE_DUNE_ISTL
namespace Dune { template<typename,typename> class BlockVector; }
#endif
namespace Opm {
namespace detail {
template<typename ...Ts>
struct MakeVariantImpl
{
template<std::size_t Index, typename, typename ...Rest>
static decltype(auto) make_variant(std::size_t index)
{
if(Index == index)
return std::variant<Ts...>{std::in_place_index_t<Index>{}};
if constexpr(sizeof...(Rest) != 0)
return make_variant<Index + 1, Rest...>(index);
else
throw std::runtime_error("Invalid variant index");
}
};
template<typename ...Ts>
decltype(auto) make_variant(std::size_t index)
{
return detail::MakeVariantImpl<Ts...>::template make_variant<0, Ts...>(index);
}
template<class T>
using remove_cvr_t = std::remove_cv_t<std::remove_reference_t<T>>;
template <typename T>
struct is_unique_ptr : std::false_type {};
template <typename T>
struct is_unique_ptr<std::unique_ptr<T>> : std::true_type {};
template <typename T>
constexpr bool is_pod_v = std::is_standard_layout_v<T> && std::is_trivial_v<T>;
} // namespace detail
template<class Packer>
class Serializer {
public:
explicit Serializer(const Packer& packer) :
m_packer(packer)
{}
template<class T>
void operator()(const T& data)
{
if constexpr (is_ptr<T>::value) {
if constexpr (detail::is_unique_ptr<T>::value) {
unique_ptr(data);
} else {
shared_ptr(data);
}
} else if constexpr (is_pair_or_tuple<T>::value) {
tuple(data);
} else if constexpr (is_variant<T>::value) {
variant(data);
} else if constexpr (is_optional<T>::value) {
optional(data);
} else if constexpr (is_vector<T>::value) {
vector(data);
} else if constexpr (is_map<T>::value) {
map(data);
} else if constexpr (is_array<T>::value) {
array(data);
} else if constexpr (is_set<T>::value) {
set(data);
} else if constexpr (has_serializeOp<detail::remove_cvr_t<T>>::value) {
const_cast<T&>(data).serializeOp(*this);
} else {
if (m_op == Operation::PACKSIZE)
m_packSize += m_packer.packSize(data);
else if (m_op == Operation::PACK)
m_packer.pack(data, m_buffer, m_position);
else if (m_op == Operation::UNPACK)
m_packer.unpack(const_cast<T&>(data), m_buffer, m_position);
}
}
template<class T>
void pack(const T& data)
{
m_ptrmap.clear();
m_op = Operation::PACKSIZE;
m_packSize = 0;
(*this)(data);
m_position = 0;
m_buffer.resize(m_packSize);
m_ptrmap.clear();
m_op = Operation::PACK;
(*this)(data);
m_ptrmap.clear();
}
template<class... Args>
void pack(const Args&... data)
{
m_ptrmap.clear();
m_op = Operation::PACKSIZE;
m_packSize = 0;
variadic_call(data...);
m_position = 0;
m_buffer.resize(m_packSize);
m_ptrmap.clear();
m_op = Operation::PACK;
variadic_call(data...);
m_ptrmap.clear();
}
template<class T>
void unpack(T& data)
{
m_position = 0;
m_ptrmap.clear();
m_op = Operation::UNPACK;
(*this)(data);
m_ptrmap.clear();
}
template<class... Args>
void unpack(Args&... data)
{
m_position = 0;
m_ptrmap.clear();
m_op = Operation::UNPACK;
variadic_call(data...);
m_ptrmap.clear();
}
size_t position() const
{
return m_position;
}
bool isSerializing() const
{
return m_op != Operation::UNPACK;
}
protected:
template <typename Vector>
void vector(const Vector& data)
{
if constexpr (detail::is_pod_v<typename Vector::value_type>) {
if (m_op == Operation::PACKSIZE) {
(*this)(data.size());
if (data.size() > 0) {
m_packSize += m_packer.packSize(data.data(), data.size());
}
} else if (m_op == Operation::PACK) {
(*this)(data.size());
if (data.size() > 0) {
m_packer.pack(data.data(), data.size(), m_buffer, m_position);
}
} else if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
auto& data_mut = const_cast<Vector&>(data);
data_mut.resize(size);
if (size > 0) {
m_packer.unpack(data_mut.data(), size, m_buffer, m_position);
}
}
} else {
if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(size);
auto& data_mut = const_cast<Vector&>(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));
}
}
}
void vector(const std::vector<bool>& data)
{
if (m_op == Operation::UNPACK) {
std::size_t size = 0;
(*this)(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_mut.push_back(entry);
}
} else {
(*this)(data.size());
for (const auto entry : data) { // Not a reference: vector<bool> range
bool b = entry;
(*this)(b);
}
}
}
template <class Array>
void array(const Array& data)
{
using T = typename Array::value_type;
if constexpr (detail::is_pod_v<T>) {
if (m_op == Operation::PACKSIZE)
m_packSize += m_packer.packSize(data.data(), data.size());
else if (m_op == Operation::PACK)
m_packer.pack(data.data(), data.size(), m_buffer, m_position);
else if (m_op == Operation::UNPACK) {
auto& data_mut = const_cast<Array&>(data);
m_packer.unpack(data_mut.data(), data_mut.size(), m_buffer, m_position);
}
} else {
std::for_each(data.begin(), data.end(), std::ref(*this));
}
}
template<class... Args>
void variant(const std::variant<Args...>& data)
{
if (m_op == Operation::UNPACK) {
std::size_t index = 0;
(*this)(index);
auto& data_mut = const_cast<std::variant<Args...>&>(data);
data_mut = detail::make_variant<Args...>(index);
std::visit(std::ref(*this), data_mut);
} else {
(*this)(data.index());
std::visit(std::ref(*this), data);
}
}
template<class T>
void optional(const std::optional<T>& data)
{
if (m_op == Operation::UNPACK) {
bool has = false;
(*this)(has);
if (has) {
T res{};
(*this)(res);
const_cast<std::optional<T>&>(data) = res;
} else {
const_cast<std::optional<T>&>(data) = std::nullopt;
}
} else {
(*this)(data.has_value());
if (data.has_value()) {
(*this)(*data);
}
}
}
template<class Tuple>
void tuple(const Tuple& data)
{
tuple_call(data);
}
template<class Map>
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_mut.insert(std::move(entry));
}
} else {
(*this)(data.size());
std::for_each(data.begin(), data.end(), std::ref(*this));
}
}
template<class Set>
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_mut.insert(entry);
}
} else {
(*this)(data.size());
std::for_each(data.begin(), data.end(), std::ref(*this));
}
}
template<typename T, typename... Args>
void variadic_call(T& first,
Args&&... args)
{
(*this)(first);
if constexpr (sizeof...(args) > 0)
variadic_call(std::forward<Args>(args)...);
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I == std::tuple_size<Tuple>::value, void>::type
tuple_call(const Tuple&)
{
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I != std::tuple_size<Tuple>::value, void>::type
tuple_call(const Tuple& tuple)
{
(*this)(std::get<I>(tuple));
tuple_call<I+1>(tuple);
}
enum class Operation {
PACKSIZE,
PACK,
UNPACK
};
template<class T>
struct is_vector {
constexpr static bool value = false;
};
template<class T1, class Allocator>
struct is_vector<std::vector<T1,Allocator>> {
constexpr static bool value = true;
};
#if HAVE_DUNE_ISTL
template<class T1, class Allocator>
struct is_vector<Dune::BlockVector<T1,Allocator>> {
constexpr static bool value = true;
};
#endif
template<class T>
struct is_variant {
constexpr static bool value = false;
};
template<class... Ts>
struct is_variant<std::variant<Ts...>> {
constexpr static bool value = true;
};
template<class T>
struct is_pair_or_tuple {
constexpr static bool value = false;
};
template<class... Ts>
struct is_pair_or_tuple<std::tuple<Ts...>> {
constexpr static bool value = true;
};
template<class T1, class T2>
struct is_pair_or_tuple<std::pair<T1,T2>> {
constexpr static bool value = true;
};
template<class T>
struct is_ptr {
constexpr static bool value = false;
};
template<class T1>
struct is_ptr<std::shared_ptr<T1>> {
constexpr static bool value = true;
};
template<class T1, class Deleter>
struct is_ptr<std::unique_ptr<T1, Deleter>> {
constexpr static bool value = true;
};
template<class T>
struct is_optional {
constexpr static bool value = false;
};
template<class T1>
struct is_optional<std::optional<T1>> {
constexpr static bool value = true;
};
template<class T>
struct is_map {
constexpr static bool value = false;
};
template<class Key, class T, class Compare, class Allocator>
struct is_map<std::map<Key,T,Compare,Allocator>> {
constexpr static bool value = true;
};
template<class Key, class T, class Hash, class KeyEqual, class Allocator>
struct is_map<std::unordered_map<Key,T,Hash,KeyEqual,Allocator>> {
constexpr static bool value = true;
};
template<class T>
struct is_set {
constexpr static bool value = false;
};
template<class Key, class Compare, class Allocator>
struct is_set<std::set<Key,Compare,Allocator>> {
constexpr static bool value = true;
};
template<class Key, class Hash, class KeyEqual, class Allocator>
struct is_set<std::unordered_set<Key,Hash,KeyEqual,Allocator>> {
constexpr static bool value = true;
};
template<class T>
struct is_array {
constexpr static bool value = false;
};
template<class T, std::size_t N>
struct is_array<std::array<T,N>> {
constexpr static bool value = true;
};
#if HAVE_DUNE_COMMON
template<class T, int N>
struct is_array<Dune::FieldVector<T,N>> {
constexpr static bool value = true;
};
#endif
template <typename, class = void>
struct has_serializeOp : public std::false_type {};
template <typename T>
struct has_serializeOp<
T, std::void_t<decltype(std::declval<T>().serializeOp(std::declval<Serializer<Packer>&>()))>
> : public std::true_type {};
template<class PtrType>
void shared_ptr(const PtrType& data)
{
using T1 = typename PtrType::element_type;
std::uintptr_t data_ptr = reinterpret_cast<std::uintptr_t>(data.get());
(*this)(data_ptr);
if (!data_ptr)
return;
if (m_op == Operation::PACK || m_op == Operation::PACKSIZE) {
if (m_ptrmap.count(data_ptr) == 0) {
(*this)(*data);
m_ptrmap[data_ptr].reset();
}
} else { // m_op == Operation::UNPACK
if (m_ptrmap.count(data_ptr) == 0) {
const_cast<PtrType&>(data) = std::make_shared<T1>();
m_ptrmap[data_ptr] = std::static_pointer_cast<void>(data);
(*this)(*data);
} else {
const_cast<PtrType&>(data) = std::static_pointer_cast<T1>(m_ptrmap[data_ptr]);
}
}
}
template<class PtrType>
void unique_ptr(const PtrType& data)
{
using T1 = typename PtrType::element_type;
if (m_op != Operation::UNPACK) {
(*this)(data ? 1 : 0);
if (data) {
(*this)(*data);
}
} else {
int ptr = 0;
(*this)(ptr);
if (ptr == 1) {
const_cast<PtrType&>(data) = std::make_unique<T1>();
(*this)(*data);
}
}
}
const Packer& m_packer;
Operation m_op = Operation::PACKSIZE;
size_t m_packSize = 0;
size_t m_position = 0;
std::vector<char> m_buffer;
std::map<std::uintptr_t, std::shared_ptr<void>> m_ptrmap;
};
}
#endif
Dune::FieldVector
Definition SymmTensor.hpp:24
Opm::Serializer::m_position
size_t m_position
Current position in buffer.
Definition Serializer.hpp:604
Opm::Serializer::array
void array(const Array &data)
Handler for arrays.
Definition Serializer.hpp:280
Opm::Serializer::operator()
void operator()(const T &data)
Applies current serialization op to the passed data.
Definition Serializer.hpp:104
Opm::Serializer::isSerializing
bool isSerializing() const
Returns true if we are currently doing a serialization operation.
Definition Serializer.hpp:207
Opm::Serializer::m_packer
const Packer & m_packer
Packer to use.
Definition Serializer.hpp:601
Opm::Serializer::tuple
void tuple(const Tuple &data)
Handler for std::tuple.
Definition Serializer.hpp:342
Opm::Serializer::set
void set(const Set &data)
Handler for sets.
Definition Serializer.hpp:372
Opm::Serializer::Operation
Operation
Enumeration of operations.
Definition Serializer.hpp:413
Opm::Serializer::Operation::UNPACK
@ UNPACK
Performing de-serialization.
Definition Serializer.hpp:416
Opm::Serializer::Operation::PACKSIZE
@ PACKSIZE
Calculating serialization buffer size.
Definition Serializer.hpp:414
Opm::Serializer::Operation::PACK
@ PACK
Performing serialization.
Definition Serializer.hpp:415
Opm::Serializer::unpack
void unpack(T &data)
Call this to de-serialize data.
Definition Serializer.hpp:178
Opm::Serializer::shared_ptr
void shared_ptr(const PtrType &data)
Handler for shared pointers.
Definition Serializer.hpp:558
Opm::Serializer::position
size_t position() const
Returns current position in buffer.
Definition Serializer.hpp:201
Opm::Serializer::m_packSize
size_t m_packSize
Required buffer size after PACKSIZE has been done.
Definition Serializer.hpp:603
Opm::Serializer::pack
void pack(const Args &... data)
Call this to serialize data.
Definition Serializer.hpp:160
Opm::Serializer::unpack
void unpack(Args &... data)
Call this to de-serialize data.
Definition Serializer.hpp:191
Opm::Serializer::vector
void vector(const Vector &data)
Handler for vectors.
Definition Serializer.hpp:217
Opm::Serializer::vector
void vector(const std::vector< bool > &data)
Handler for bool vectors.
Definition Serializer.hpp:255
Opm::Serializer::Serializer
Serializer(const Packer &packer)
Constructor.
Definition Serializer.hpp:98
Opm::Serializer::variant
void variant(const std::variant< Args... > &data)
Handler for std::variant.
Definition Serializer.hpp:301
Opm::Serializer::map
void map(const Map &data)
Handler for maps.
Definition Serializer.hpp:351
Opm::Serializer::pack
void pack(const T &data)
Call this to serialize data.
Definition Serializer.hpp:142
Opm::Serializer::m_op
Operation m_op
Current operation.
Definition Serializer.hpp:602
Opm::Serializer::optional
void optional(const std::optional< T > &data)
Handler for std::optional.
Definition Serializer.hpp:319
Opm::Serializer::m_buffer
std::vector< char > m_buffer
Buffer for serialized data.
Definition Serializer.hpp:605
Opm::Serializer::m_ptrmap
std::map< std::uintptr_t, std::shared_ptr< void > > m_ptrmap
Map to keep track of which pointer data has been serialized and actual pointers during unpacking.
Definition Serializer.hpp:606
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Opm::has
bool has(const std::string &name) const
This method checks if there is something with name name in the parameter gropup.
Opm::Serializer::has_serializeOp
Detect existence of serializeOp member function.
Definition Serializer.hpp:545
Opm::Serializer::is_array
Predicate for arrays.
Definition Serializer.hpp:525
Opm::Serializer::is_map
Predicate for maps.
Definition Serializer.hpp:493
Opm::Serializer::is_optional
Predicate for std::optional.
Definition Serializer.hpp:482
Opm::Serializer::is_pair_or_tuple
Predicate for detecting pairs and tuples.
Definition Serializer.hpp:450
Opm::Serializer::is_ptr
Predicate for smart pointers.
Definition Serializer.hpp:466
Opm::Serializer::is_set
Predicate for sets.
Definition Serializer.hpp:509
Opm::Serializer::is_variant
Predicate for detecting variants.
Definition Serializer.hpp:439
Opm::Serializer::is_vector
Predicate for detecting vectors.
Definition Serializer.hpp:421
Opm::detail::MakeVariantImpl
Definition Serializer.hpp:52
Opm::detail::is_unique_ptr
Definition Serializer.hpp:78