最近使用了一次boost::serialization

本文介绍了一种使用Boost库来序列化复杂数据结构(如map<pair<int,int>,vector<vector<int>>>)的方法,以减少程序运行时的初始化时间。通过在程序开始时读取已保存的数据,以及在程序结束时保存更新后的数据,实现了数据的有效管理和持久化。

   在某个项目中,每次运行时要产生一个map<pair<int,int > ,vector<vector<int> > > 东西,十分耗时,自然想到序列化。

程序开始:    std::ifstream ifs("maps.save");
    boost::archive::text_iarchive ia(ifs);
    ia & myMap;
    ifs.close();

程序结尾:    std::ofstream ofs("maps.save");
    boost::archive::text_oarchive oa(ofs);
    oa & myMap;
    ofs.close();

1>F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(86,5): error C2672: “PackageParamsImpl”: 未找到匹配的重载函数 1>(编译源文件“../../../src/others/interprocess/Interface/MediaWindowControllerInterface.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(78,6): 1> 可能是“void InterprocSerialization::PackageParamsImpl(boost::archive::binary_oarchive &,const Tuple &,std::integer_sequence<size_t,_Kx...>)” 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(86,5): 1> “void InterprocSerialization::PackageParamsImpl(boost::archive::binary_oarchive &,const Tuple &,std::integer_sequence<size_t,_Kx...>)”: 无法从“boost::fusion::detail::make_index_sequence<1>”推导出“std::integer_sequence<size_t,_Kx...>”的 模板 参数 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(86,5): 1> 模板实例化上下文(最早的实例化上下文)为 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interface\MediaWindowControllerInterface.cpp(52,29): 1> 查看对正在编译的函数 模板 实例化“void InterprocSerialization::PackageParams<int>(boost::archive::binary_oarchive &,const std::tuple<int> &)”的引用 1>IPCAPPDownloader.cpp 1>IPCAppEnterpriseListContext.cpp 1>F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewViewScaleWidget.h(20,9): warning C4005: “SCALE_IMAGE_PICTUR_MIN_HEIGHT”: 宏重定义 1>(编译源文件“../../../src/others/interprocess/Interproc/TPFunctionBindUtils.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewScaleImageWidget.h(20,9): 1> 参见“SCALE_IMAGE_PICTUR_MIN_HEIGHT”的前一个定义 1>F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewViewScaleWidget.h(21,9): warning C4005: “SCALE_IMAGE_PICTUR_MIN_WIDTH”: 宏重定义 1>(编译源文件“../../../src/others/interprocess/Interproc/TPFunctionBindUtils.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewScaleImageWidget.h(21,9): 1> 参见“SCALE_IMAGE_PICTUR_MIN_WIDTH”的前一个定义 1>F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewViewScaleWidget.h(20,9): warning C4005: “SCALE_IMAGE_PICTUR_MIN_HEIGHT”: 宏重定义 1>(编译源文件“../../../src/others/interprocess/Interproc/TPWindowControllerEntry.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewScaleImageWidget.h(20,9): 1> 参见“SCALE_IMAGE_PICTUR_MIN_HEIGHT”的前一个定义 1>F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewViewScaleWidget.h(21,9): warning C4005: “SCALE_IMAGE_PICTUR_MIN_WIDTH”: 宏重定义 1>(编译源文件“../../../src/others/interprocess/Interproc/TPWindowControllerEntry.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewScaleImageWidget.h(21,9): 1> 参见“SCALE_IMAGE_PICTUR_MIN_WIDTH”的前一个定义 1>IPCAppExcutorBasicCloud.cpp 1>F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewViewScaleWidget.h(20,9): warning C4005: “SCALE_IMAGE_PICTUR_MIN_HEIGHT”: 宏重定义 1>(编译源文件“../../../src/context/IPCAppContext.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewScaleImageWidget.h(20,9): 1> 参见“SCALE_IMAGE_PICTUR_MIN_HEIGHT”的前一个定义 1>F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewViewScaleWidget.h(21,9): warning C4005: “SCALE_IMAGE_PICTUR_MIN_WIDTH”: 宏重定义 1>(编译源文件“../../../src/context/IPCAppContext.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\vms-client\SurveillanceSystem\SurveillanceSystem\widget\preview\PreviewScaleImageWidget.h(21,9): 1> 参见“SCALE_IMAGE_PICTUR_MIN_WIDTH”的前一个定义 1>F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(57,61): error C2672: “GetTuple”: 未找到匹配的重载函数 1>(编译源文件“../../../src/others/interprocess/Interproc/TPFunctionBindUtils.cpp”) 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(28,7): 1> 可能是“Tuple InterprocSerialization::GetTuple(boost::archive::binary_iarchive *,std::integer_sequence<size_t,_Kx...>)” 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(57,61): 1> “Tuple InterprocSerialization::GetTuple(boost::archive::binary_iarchive *,std::integer_sequence<size_t,_Kx...>)”: 无法从“boost::fusion::detail::make_index_sequence<1>”推导出“std::integer_sequence<size_t,_Kx...>”的 模板 参数 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(57,61): 1> 模板实例化上下文(最早的实例化上下文)为 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\TPFunctionBindUtils.cpp(55,23): 1> 查看对正在编译的函数 模板 实例化“InterprocError InterprocMsgManager::Subscribe<void(int)>(const std::string,R (__cdecl *),const std::string)”的引用 1> with 1> [ 1> R=void (int) 1> ] 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocMsgManager.h(22,16): 1> 查看对正在编译的函数 模板 实例化“long InterprocListener::RegisterCallback<R>(std::string,R (__cdecl *))”的引用 1> with 1> [ 1> R=void (int) 1> ] 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocListener.h(26,27): 1> 查看对正在编译的函数 模板 实例化“void InterprocSerialization::Bind<R>(std::map<std::string,std::function<void (boost::archive::binary_oarchive *,boost::archive::binary_iarchive *)>,std::less<std::string>,std::allocator<std::pair<const std::string,std::function<void (boost::archive::binary_oarchive *,boost::archive::binary_iarchive *)>>>> &,std::string,R (__cdecl *))”的引用 1> with 1> [ 1> R=void (int) 1> ] 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(73,34): 1> 查看对正在编译的函数 模板 实例化“void InterprocSerialization::CallProxy<R>(R (__cdecl *),boost::archive::binary_oarchive *,boost::archive::binary_iarchive *)”的引用 1> with 1> [ 1> R=void (int) 1> ] 1> F:\updateVPC\vpc2_newest\vms\vms-public\src\others\interprocess\Interproc\InterprocSerialization.h(65,5): 1> 查看对正在编译的函数 模板 实例化“void InterprocSerialization::CallProxy_<void,int>(R (__cdecl *)(int),boost::archive::binary_oarchive *,boost::archive::binary_iarchive *)”的引用 1> with 1> [ 1> R=void 1> ] 这是我的源码: #pragma once #include <map> #include <string> #include <functional> #include <boost/archive/binary_iarchive.hpp> #include <boost/archive/binary_oarchive.hpp> #include <boost/fusion/support/detail/index_sequence.hpp> #include <tuple> #include <boost/serialization/vector.hpp> namespace InterprocSerialization { template<typename T> struct type_transfer { typedef T type; }; template<> struct type_transfer<void> { typedef int type; }; template< typename ... T > using index_sequence_for = boost::fusion::detail::make_index_sequence< sizeof ... (T) >; template<typename... Params> std::tuple<typename std::decay<Params>::type...> MakeTuple(Params... ps) { return std::make_tuple(ps...); } template<typename Tuple, std::size_t... I> Tuple GetTuple(boost::archive::binary_iarchive* ia, boost::fusion::detail::index_sequence<I...>) { Tuple t; std::initializer_list<int>{ ((*ia >> std::get<I>(t)), 0)... }; return t; } template<typename R, typename... Params, typename Tuple, std::size_t... Index > typename R invoke(R(*f)(Params...), Tuple&& t, boost::fusion::detail::index_sequence<Index...>) { return f(std::get<Index>(std::forward<Tuple>(t))...); } template<typename R, typename F, typename ArgsTuple> typename std::enable_if<std::is_same<R, void>::value, typename type_transfer<R>::type>::type CallHelper(F f, ArgsTuple args) { invoke(f, args, boost::fusion::detail::make_index_sequence<std::tuple_size<typename std::decay<ArgsTuple>::type>::value>{}); return 0; } template<typename R, typename F, typename ArgsTuple> typename std::enable_if<!std::is_same<R, void>::value, typename type_transfer<R>::type>::type CallHelper(F f, ArgsTuple args) { return invoke(f, args, boost::fusion::detail::make_index_sequence<std::tuple_size<typename std::decay<ArgsTuple>::type>::value>{}); } template<typename R, typename... Params> void CallProxy_(R(*f)(Params... ps), boost::archive::binary_oarchive* oa, boost::archive::binary_iarchive* ia) { std::tuple<typename std::decay<Params>::type...> args = GetTuple<std::tuple<typename std::decay<Params>::type...> >(ia, boost::fusion::detail::make_index_sequence<sizeof...(Params)>()); typename type_transfer<R>::type ret = CallHelper<R>(f, args); } template<typename R> void CallProxy(R(*f), boost::archive::binary_oarchive* oa, boost::archive::binary_iarchive* ia) { CallProxy_(f, oa, ia); } template<typename R> void Bind(std::map<std::string, std::function<void(boost::archive::binary_oarchive*, boost::archive::binary_iarchive*)> > &handlers, std::string fname, R(*f)) { // todo ����У�� handlers[fname] = std::bind(&CallProxy<R>, f, std::placeholders::_1, std::placeholders::_2); } template<typename Tuple, std::size_t... Is> void PackageParamsImpl(boost::archive::binary_oarchive& oa, const Tuple& t, boost::fusion::detail::index_sequence<Is...>) { std::initializer_list<int>{ ((oa << std::get<Is>(t)), 0)... }; } template<typename... Args> void PackageParams(boost::archive::binary_oarchive& oa, const std::tuple<Args...>& t) { PackageParamsImpl(oa, t, index_sequence_for<Args...>{}); } } // namespace InterProSerialization 为什么会报错???
最新发布
11-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值