diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/boost_serialization_helper.h | 46 | ||||
-rw-r--r-- | src/common/unordered_containers_boost_serialization.h | 37 | ||||
-rw-r--r-- | src/common/util.h | 2 |
3 files changed, 80 insertions, 5 deletions
diff --git a/src/common/boost_serialization_helper.h b/src/common/boost_serialization_helper.h index 74016ae75..0bf924802 100644 --- a/src/common/boost_serialization_helper.h +++ b/src/common/boost_serialization_helper.h @@ -14,15 +14,55 @@ namespace tools bool serialize_obj_to_file(t_object& obj, const std::string& file_path) { TRY_ENTRY(); +#if defined(_MSC_VER) + // Need to know HANDLE of file to call FlushFileBuffers + HANDLE data_file_handle = ::CreateFile(file_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (INVALID_HANDLE_VALUE == data_file_handle) + return false; + + int data_file_descriptor = _open_osfhandle((intptr_t)data_file_handle, 0); + if (-1 == data_file_descriptor) + { + ::CloseHandle(data_file_handle); + return false; + } + + FILE* data_file_file = _fdopen(data_file_descriptor, "wb"); + if (0 == data_file_file) + { + // Call CloseHandle is not necessary + _close(data_file_descriptor); + return false; + } + + // HACK: undocumented constructor, this code may not compile + std::ofstream data_file(data_file_file); + if (data_file.fail()) + { + // Call CloseHandle and _close are not necessary + fclose(data_file_file); + return false; + } +#else std::ofstream data_file; - data_file.open( file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc); - if(data_file.fail()) + data_file.open(file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc); + if (data_file.fail()) return false; +#endif boost::archive::binary_oarchive a(data_file); a << obj; + if (data_file.fail()) + return false; - return !data_file.fail(); + data_file.flush(); +#if defined(_MSC_VER) + // To make sure the file is fully stored on disk + ::FlushFileBuffers(data_file_handle); + fclose(data_file_file); +#endif + + return true; CATCH_ENTRY_L0("serialize_obj_to_file", false); } diff --git a/src/common/unordered_containers_boost_serialization.h b/src/common/unordered_containers_boost_serialization.h index a29896ee7..84fa73b92 100644 --- a/src/common/unordered_containers_boost_serialization.h +++ b/src/common/unordered_containers_boost_serialization.h @@ -2,7 +2,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#pragma once +#pragma once #include <boost/serialization/split_free.hpp> #include <unordered_map> @@ -41,6 +41,35 @@ namespace boost } + template <class Archive, class h_key, class hval> + inline void save(Archive &a, const std::unordered_multimap<h_key, hval> &x, const boost::serialization::version_type ver) + { + size_t s = x.size(); + a << s; + BOOST_FOREACH(auto& v, x) + { + a << v.first; + a << v.second; + } + } + + template <class Archive, class h_key, class hval> + inline void load(Archive &a, std::unordered_multimap<h_key, hval> &x, const boost::serialization::version_type ver) + { + x.clear(); + size_t s = 0; + a >> s; + for(size_t i = 0; i != s; i++) + { + h_key k; + hval v; + a >> k; + a >> v; + x.emplace(k, v); + } + } + + template <class Archive, class hval> inline void save(Archive &a, const std::unordered_set<hval> &x, const boost::serialization::version_type ver) { @@ -73,6 +102,12 @@ namespace boost split_free(a, x, ver); } + template <class Archive, class h_key, class hval> + inline void serialize(Archive &a, std::unordered_multimap<h_key, hval> &x, const boost::serialization::version_type ver) + { + split_free(a, x, ver); + } + template <class Archive, class hval> inline void serialize(Archive &a, std::unordered_set<hval> &x, const boost::serialization::version_type ver) { diff --git a/src/common/util.h b/src/common/util.h index af92adf94..8a1f4b041 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -52,7 +52,7 @@ namespace tools private: #if defined(WIN32) - static BOOL win_handler(DWORD type) + static BOOL WINAPI win_handler(DWORD type) { if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) { |