20 #ifndef OPM_SERIALIZER_HPP
21 #define OPM_SERIALIZER_HPP
25 #include <unordered_map>
41 explicit Serializer(
const std::vector<char>& buffer_arg) :
47 void put(
const T& value) {
48 this->pack(std::addressof(value),
sizeof(T));
53 throw std::logic_error(
"Serializer can not pack pointers");
59 std::memcpy(&value, &this->buffer[this->read_pos],
sizeof(T));
60 this->read_pos +=
sizeof(T);
65 void put_vector(
const std::vector<T>& values) {
66 this->put(values.size());
67 this->pack(values.data(), values.size() *
sizeof(T));
73 std::vector<T> get_vector() {
74 std::size_t size = this->get<std::size_t>();
75 std::vector<T> values(size);
76 for (std::size_t index=0; index < size; index++)
77 values[index] = this->get<T>();
82 template<
typename K,
typename T>
83 void put_map(
const std::unordered_map<K,T>& values) {
84 this->put(values.size());
85 for (
const auto& value_pair : values) {
86 this->put(value_pair.first);
87 this->put(value_pair.second);
91 template<
typename K,
typename T>
92 std::unordered_map<K,T> get_map() {
93 std::unordered_map<K,T> values;
94 auto size = this->get<std::size_t>();
95 for (std::size_t index = 0; index < size; index++) {
96 auto key = this->get<K>();
97 auto value = this->get<T>();
98 values.insert( std::make_pair(key,value) );
104 std::vector<char> buffer;
106 void pack(
const void * ptr, std::size_t value_size) {
107 std::size_t write_pos = this->buffer.size();
108 std::size_t new_size = write_pos + value_size;
109 this->buffer.resize( new_size );
110 std::memcpy(&this->buffer[write_pos], ptr, value_size);
113 std::size_t read_pos = 0;
117 void inline Serializer::put(
const std::string& value) {
118 this->put(value.size());
122 this->pack(value.c_str(), value.size());
126 std::string
inline Serializer::get<std::string>() {
127 std::string::size_type length = this->get<std::string::size_type>();
129 return std::string{};
131 this->read_pos += length;
132 return {std::addressof(this->buffer[this->read_pos - length]), length};
Definition: Serializer.hpp:38
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29