1
0
Fork 0
forked from lthn/blockchain
blockchain/src/common/boost_serialization_helper.h
Snider 06e7780df5 updates licence
adds this to the bottom of whats there:
// Copyright (c) 2017-2025 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// You may obtain a copy of the licence at:
//
//     https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
//
// The EUPL is a copyleft licence that is compatible with the MIT/X11
// licence used by the original projects; the MIT terms are therefore
// considered “grandfathered” under the EUPL for this code.
//
// SPDX‑License‑Identifier: EUPL-1.2
//
2025-09-25 18:18:41 +01:00

116 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) 2014-2018 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Copyright (c) 2012-2013 The Boolberry developers
// Copyright (c) 2017-2025 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// You may obtain a copy of the licence at:
//
// https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
//
// The EUPL is a copyleft licence that is compatible with the MIT/X11
// licence used by the original projects; the MIT terms are therefore
// considered “grandfathered” under the EUPL for this code.
//
// SPDXLicenseIdentifier: EUPL-1.2
//
#pragma once
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <eos/portable_archive.hpp>
#define CHECK_PROJECT_NAME() std::string project_name = CURRENCY_NAME; ar & project_name; if(!(project_name == CURRENCY_NAME) ) {throw std::runtime_error(std::string("wrong storage file: project name in file: ") + project_name + ", expected: " + CURRENCY_NAME );}
namespace tools
{
template<class t_object>
bool serialize_obj_to_file(t_object& obj, const std::string& file_path)
{
TRY_ENTRY();
boost::filesystem::ofstream data_file;
data_file.open( epee::string_encoding::utf8_to_wstring(file_path) , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
if(data_file.fail())
return false;
boost::archive::binary_oarchive a(data_file);
a << obj;
return !data_file.fail();
CATCH_ENTRY_L0("serialize_obj_to_file: could not serialize into " << file_path, false);
}
template<class t_object>
bool serialize_obj_to_buff(t_object& obj, std::string& buff)
{
TRY_ENTRY();
std::stringstream data_buff;
boost::archive::binary_oarchive a(data_buff);
a << obj;
buff = data_buff.str();
return !data_buff.fail();
CATCH_ENTRY_L0("serialize_obj_to_buff", false);
}
template<class t_object, class t_stream>
bool portble_serialize_obj_to_stream(t_object& obj, t_stream& stream)
{
TRY_ENTRY();
eos::portable_oarchive a(stream);
a << obj;
return !stream.fail();
CATCH_ENTRY_L0("portble_serialize_obj_to_stream", false);
}
template<class t_object>
bool unserialize_obj_from_file(t_object& obj, const std::string& file_path)
{
TRY_ENTRY();
boost::filesystem::ifstream data_file;
data_file.open( epee::string_encoding::utf8_to_wstring(file_path), std::ios_base::binary | std::ios_base::in);
if(data_file.fail())
return false;
boost::archive::binary_iarchive a(data_file);
a >> obj;
return !data_file.fail();
CATCH_ENTRY_L0("unserialize_obj_from_file: could not load " << file_path, false);
}
template<class t_object>
bool unserialize_obj_from_buff(t_object& obj, const std::string& buff)
{
TRY_ENTRY();
std::stringstream ss(buff);
boost::archive::binary_iarchive a(ss);
a >> obj;
return !ss.fail();
CATCH_ENTRY_L0("unserialize_obj_from_buff", false);
}
template<class t_object, class t_stream>
bool portable_unserialize_obj_from_stream(t_object& obj, t_stream& stream)
{
TRY_ENTRY();
eos::portable_iarchive a(stream);
a >> obj;
return !stream.fail();
CATCH_ENTRY_L0("portable_unserialize_obj_from_stream", false);
}
}