changed: remove unused Serialization class

This commit is contained in:
Arne Morten Kvarving
2022-09-07 09:53:41 +02:00
parent 1573ffb8da
commit ada6853a61
3 changed files with 0 additions and 202 deletions

View File

@@ -475,7 +475,6 @@ if(ENABLE_ECL_OUTPUT)
tests/test_RFT.cpp
tests/test_rst.cpp
tests/test_Solution.cpp
tests/test_Serializer.cpp
tests/test_Inplace.cpp
tests/test_Summary.cpp
tests/test_Summary_Group.cpp
@@ -631,7 +630,6 @@ list( APPEND PUBLIC_HEADER_FILES
opm/common/OpmLog/OpmLog.hpp
opm/common/OpmLog/StreamLog.hpp
opm/common/OpmLog/TimerLog.hpp
opm/common/utility/Serializer.hpp
opm/common/utility/ActiveGridCells.hpp
opm/common/utility/FileSystem.hpp
opm/common/utility/OpmInputError.hpp

View File

@@ -1,136 +0,0 @@
/*
Copyright 2020 Equinor ASA.
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 OPM_SERIALIZER_HPP
#define OPM_SERIALIZER_HPP
#include <cstring>
#include <string>
#include <unordered_map>
#include <vector>
namespace Opm {
/*
This is a very basic serialization class used to support serialization of
small state objects from opm common. The main serialization code used in
opm/flow is initiated and controlled from the restart code, and therefor
slightly cumbersome to use for objects which should be serialized not as part
of the restart code.
*/
class Serializer {
public:
Serializer() = default;
explicit Serializer(const std::vector<char>& buffer_arg) :
buffer(buffer_arg)
{}
template <typename T>
void put(const T& value) {
this->pack(std::addressof(value), sizeof(T));
}
template <typename T>
void put(const T* ) {
throw std::logic_error("Serializer can not pack pointers");
}
template <typename T>
T get() {
T value;
std::memcpy(&value, &this->buffer[this->read_pos], sizeof(T));
this->read_pos += sizeof(T);
return value;
}
template<typename T>
void put_vector(const std::vector<T>& values) {
this->put(values.size());
this->pack(values.data(), values.size() * sizeof(T));
}
template<typename T>
std::vector<T> get_vector() {
std::size_t size = this->get<std::size_t>();
std::vector<T> values(size);
for (std::size_t index=0; index < size; index++)
values[index] = this->get<T>();
return values;
}
template<typename K, typename T>
void put_map(const std::unordered_map<K,T>& values) {
this->put(values.size());
for (const auto& value_pair : values) {
this->put(value_pair.first);
this->put(value_pair.second);
}
}
template<typename K, typename T>
std::unordered_map<K,T> get_map() {
std::unordered_map<K,T> values;
auto size = this->get<std::size_t>();
for (std::size_t index = 0; index < size; index++) {
auto key = this->get<K>();
auto value = this->get<T>();
values.insert( std::make_pair(key,value) );
}
return values;
}
std::vector<char> buffer;
private:
void pack(const void * ptr, std::size_t value_size) {
std::size_t write_pos = this->buffer.size();
std::size_t new_size = write_pos + value_size;
this->buffer.resize( new_size );
std::memcpy(&this->buffer[write_pos], ptr, value_size);
}
std::size_t read_pos = 0;
};
template <>
void inline Serializer::put(const std::string& value) {
this->put(value.size());
if (value.empty())
return;
this->pack(value.c_str(), value.size());
}
template<>
std::string inline Serializer::get<std::string>() {
std::string::size_type length = this->get<std::string::size_type>();
if (length == 0)
return std::string{};
this->read_pos += length;
return {std::addressof(this->buffer[this->read_pos - length]), length};
}
}
#endif

View File

@@ -1,64 +0,0 @@
/*
Copyright 2020 Statoil ASA.
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/>.
*/
#define BOOST_TEST_MODULE "Serializer"
#include <boost/test/unit_test.hpp>
#include <string>
#include <unordered_map>
#include <opm/common/utility/Serializer.hpp>
BOOST_AUTO_TEST_CASE(SERIALIZER) {
Opm::Serializer ser;
int int_value = 100;
double double_value = 3.14;
std::string string_value = "String";
std::unordered_map<std::string, int> m = {{"A", 1}, {"B", 2}, {"C", 3}};
std::vector<int> v = {1,2,3,4,5,6,7,8,9,10};
ser.put(int_value);
ser.put(double_value);
ser.put(string_value);
ser.put_map(m);
ser.put_vector(v);
Opm::Serializer ser2(ser.buffer);
BOOST_CHECK_EQUAL(ser2.get<int>(), int_value);
BOOST_CHECK_EQUAL(ser2.get<double>(), double_value);
BOOST_CHECK_EQUAL(ser2.get<std::string>(), string_value);
std::unordered_map<std::string, int> m2 = ser2.get_map<std::string,int>();
BOOST_CHECK(m2 == m);
std::vector<int> v2 = ser2.get_vector<int>();
BOOST_CHECK(v2 == v);
}
BOOST_AUTO_TEST_CASE(EMPTY_STRING) {
Opm::Serializer ser;
ser.put(std::string{});
BOOST_CHECK_THROW( ser.put(""), std::logic_error);
Opm::Serializer ser2(ser.buffer);
BOOST_CHECK_EQUAL(ser2.get<std::string>(), "");
}