danicoin/tests/CoreTests/BoostSerializationHelper.h

51 lines
1.1 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-05-27 12:08:46 +00:00
#pragma once
#include <fstream>
2015-08-05 13:09:05 +00:00
#include <memory>
#include <string>
2015-05-27 12:08:46 +00:00
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
2015-08-05 13:09:05 +00:00
namespace Tools {
2015-05-27 12:08:46 +00:00
2015-08-05 13:09:05 +00:00
template <class t_object>
bool serialize_obj_to_file(t_object& obj, const std::string& file_path) {
try {
std::ofstream file(file_path);
boost::archive::binary_oarchive a(file);
a << obj;
if (file.fail()) {
2015-05-27 12:08:46 +00:00
return false;
}
2015-08-05 13:09:05 +00:00
file.flush();
return true;
} catch (std::exception&) {
return false;
}
}
2015-05-27 12:08:46 +00:00
2015-08-05 13:09:05 +00:00
template <class t_object>
bool unserialize_obj_from_file(t_object& obj, const std::string& file_path) {
try {
std::ifstream dataFile;
dataFile.open(file_path, std::ios_base::binary | std::ios_base::in);
if (dataFile.fail()) {
2015-05-27 12:08:46 +00:00
return false;
}
2015-08-05 13:09:05 +00:00
boost::archive::binary_iarchive a(dataFile);
a >> obj;
return !dataFile.fail();
} catch (std::exception&) {
return false;
2015-05-27 12:08:46 +00:00
}
}
2015-08-05 13:09:05 +00:00
}