Loading...
Searching...
No Matches
MemPacker.hpp
/*
Copyright 2019 Equinor AS.
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/>.
*/
#ifndef MEM_PACKER_HPP
#define MEM_PACKER_HPP
#include <opm/common/utility/TimeService.hpp>
#include <bitset>
#include <cstddef>
#include <cstring>
#include <string>
#include <vector>
namespace Opm {
namespace Serialization {
namespace detail {
template <bool pod, class T>
struct Packing
{
static std::size_t packSize(const T&);
static void pack(const T&, std::vector<char>&, std::size_t&);
static void unpack(T&, const std::vector<char>&, std::size_t&);
};
template<class T>
struct Packing<true,T>
{
static std::size_t packSize(const T& data)
{
return packSize(&data, 1);
}
static std::size_t packSize(const T*, std::size_t n)
{
return n*sizeof(T);
}
static void pack(const T& data,
std::vector<char>& buffer,
std::size_t& position)
{
pack(&data, 1, buffer, position);
}
static void pack(const T* data,
std::size_t n,
std::vector<char>& buffer,
std::size_t& position)
{
std::memcpy(buffer.data() + position, data, n*sizeof(T));
position += n*sizeof(T);
}
static void unpack(T& data,
const std::vector<char>& buffer,
std::size_t& position)
{
unpack(&data, 1, buffer, position);
}
static void unpack(T* data,
std::size_t n,
const std::vector<char>& buffer,
std::size_t& position)
{
std::memcpy(data, buffer.data() + position, n*sizeof(T));
position += n*sizeof(T);
}
};
template<class T>
struct Packing<false,T>
{
static std::size_t packSize(const T&)
{
static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
return 0;
}
static void pack(const T&, std::vector<char>&, std::size_t&)
{
static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
}
static void unpack(T&, const std::vector<char>&, std::size_t&)
{
static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
}
};
template <std::size_t Size>
struct Packing<false,std::bitset<Size>>
{
static std::size_t packSize(const std::bitset<Size>& data);
static void pack(const std::bitset<Size>& data,
std::vector<char>& buffer, std::size_t& position);
static void unpack(std::bitset<Size>& data,
const std::vector<char>& buffer, std::size_t& position);
};
template<>
struct Packing<false,std::string>
{
static std::size_t packSize(const std::string& data);
static void pack(const std::string& data,
std::vector<char>& buffer, std::size_t& position);
static void unpack(std::string& data, const std::vector<char>& buffer, std::size_t& position);
};
template<>
struct Packing<false,time_point>
{
static std::size_t packSize(const time_point&);
static void pack(const time_point& data,
std::vector<char>& buffer, std::size_t& position);
static void unpack(time_point& data, const std::vector<char>& buffer, std::size_t& position);
};
}
struct MemPacker {
template<class T>
std::size_t packSize(const T& data) const
{
return detail::Packing<std::is_pod_v<T>,T>::packSize(data);
}
template<class T>
std::size_t packSize(const T* data, std::size_t n) const
{
static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
return detail::Packing<true,T>::packSize(data, n);
}
template<class T>
void pack(const T& data,
std::vector<char>& buffer,
std::size_t& position) const
{
detail::Packing<std::is_pod_v<T>,T>::pack(data, buffer, position);
}
template<class T>
void pack(const T* data,
std::size_t n,
std::vector<char>& buffer,
std::size_t& position) const
{
static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
detail::Packing<true,T>::pack(data, n, buffer, position);
}
template<class T>
void unpack(T& data,
const std::vector<char>& buffer,
std::size_t& position) const
{
detail::Packing<std::is_pod_v<T>,T>::unpack(data, buffer, position);
}
template<class T>
void unpack(T* data,
std::size_t n,
const std::vector<char>& buffer,
std::size_t& position) const
{
static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
detail::Packing<true,T>::unpack(data, n, buffer, position);
}
};
} // end namespace Serialization
} // end namespace Opm
#endif // MEM_PACKER_HPP
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Opm::Serialization::MemPacker
Struct handling packing of serialization to a memory buffer.
Definition MemPacker.hpp:172
Opm::Serialization::MemPacker::unpack
void unpack(T *data, std::size_t n, const std::vector< char > &buffer, std::size_t &position) const
Unpack an array.
Definition MemPacker.hpp:242
Opm::Serialization::MemPacker::packSize
std::size_t packSize(const T *data, std::size_t n) const
Calculates the pack size for an array.
Definition MemPacker.hpp:187
Opm::Serialization::MemPacker::pack
void pack(const T *data, std::size_t n, std::vector< char > &buffer, std::size_t &position) const
Pack an array.
Definition MemPacker.hpp:213
Opm::Serialization::MemPacker::unpack
void unpack(T &data, const std::vector< char > &buffer, std::size_t &position) const
Unpack a variable.
Definition MemPacker.hpp:228
Opm::Serialization::MemPacker::packSize
std::size_t packSize(const T &data) const
Calculates the pack size for a variable.
Definition MemPacker.hpp:177
Opm::Serialization::MemPacker::pack
void pack(const T &data, std::vector< char > &buffer, std::size_t &position) const
Pack a variable.
Definition MemPacker.hpp:199
Opm::Serialization::detail::Packing< true, T >::packSize
static std::size_t packSize(const T &data)
Calculates the pack size for a POD.
Definition MemPacker.hpp:49
Opm::Serialization::detail::Packing< true, T >::pack
static void pack(const T &data, std::vector< char > &buffer, std::size_t &position)
Pack a POD.
Definition MemPacker.hpp:66
Opm::Serialization::detail::Packing< true, T >::pack
static void pack(const T *data, std::size_t n, std::vector< char > &buffer, std::size_t &position)
Pack an array of POD.
Definition MemPacker.hpp:78
Opm::Serialization::detail::Packing< true, T >::unpack
static void unpack(T &data, const std::vector< char > &buffer, std::size_t &position)
Unpack a POD.
Definition MemPacker.hpp:91
Opm::Serialization::detail::Packing< true, T >::packSize
static std::size_t packSize(const T *, std::size_t n)
Calculates the pack size for an array of POD.
Definition MemPacker.hpp:57
Opm::Serialization::detail::Packing< true, T >::unpack
static void unpack(T *data, std::size_t n, const std::vector< char > &buffer, std::size_t &position)
Unpack an array of POD.
Definition MemPacker.hpp:103
Opm::Serialization::detail::Packing
Abstract struct for packing which is (partially) specialized for specific types.
Definition MemPacker.hpp:37