废话
发现一个比较好玩的东西,boost::serialization序列化操作,简单来说感觉像ofstream和ifstream的升级版,Boost.Serialization 库能够将c++项目中的对象转换为一序列的比特(bytes),用来保存和加载还原对象。
在ORBSLAM3保存和加载地图的时候好像就是采用的这种方法,后面需要再深入研究一下。
实战
参考链接:https://www.jianshu.com/p/c2f830cee98c
感觉这个大佬写的很不错,参考了一下他的分享自己写了一个简单的小例子,设计了一个简单的相机信息类CameraInfo
。然后对其进行序列化和反序列化操作,也就是对象的写入和加载。
代码文件:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <boost/archive/binary_iarchive.hpp> //二进制序列化
#include <boost/archive/binary_oarchive.hpp> //二进制序列化
#include <boost/serialization/vector.hpp> //序列化STL容器要导入
#include <boost/serialization/string.hpp>
using namespace std;
class CameraInfo{
public:
CameraInfo(){};
CameraInfo(const int _fx, const int _fy, const int _cx, const int _cy, const vector<float> &_vDistortion, string &_CamName);
~CameraInfo(){}
int getFx(){return fx;}
int getFy(){return fy;}
int getCx(){return cx;}
int getCy(){return cy;}
vector<float> getDistortion(){return mvDistortion;}
string getCamName(){return CamName;}
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar &fx;
ar &fy;
ar &cx;
ar &cy;
ar &mvDistortion;
ar &CamName;
}
int fx,fy,cx,cy;
vector<float> mvDistortion;
string CamName;
};
CameraInfo::CameraInfo(const int _fx, const int _fy, const int _cx, const int _cy, const vector<float> &_vDistortion, string &_CamName)
:fx(_fx),fy(_fy),cx(_cx),cy(_cy),mvDistortion(_vDistortion),CamName(_CamName)
{}
void SaveFileTest()
{
// 文本写入测试
vector<float> dist = {0.001, -0.03, 0.0004, 0.004};
string camName = "realsense";
CameraInfo cam(222,222,230,230,dist,camName);
ofstream fout("testfile", ios::binary);
if(fout.is_open())
{
boost::archive::binary_oarchive oa(fout);
oa << cam;
fout.close();
}
else{
cout<<"open file failed."<<endl;
exit(-1);
}
}
void LoadFileTest()
{
// 文本读取测试
CameraInfo cam2;
ifstream fin("testfile",ios::in|ios::binary);
fin.seekg(0, ios::end);
streampos fp = fin.tellg();
if(fin.is_open() && fp)
{
fin.seekg(0);
boost::archive::binary_iarchive ia(fin);
ia >> cam2;
printf("fx = %d, fy=%d, cx=%d, cy=%d\r\n",cam2.getFx(),cam2.getFy(),cam2.getCx(),cam2.getCy());
vector<float> distortion = cam2.getDistortion();
printf("Distortion = [%f, %f, %f, %f]\r\n",distortion[0],distortion[1],distortion[2],distortion[3]);
printf("CamName = %s\r\n",cam2.getCamName().c_str());
}
else{
cout<<"open file failed."<<endl;
exit(-1);
}
}
int main(int argc, char** argv)
{
// 文本写入测试
SaveFileTest();
// 文本读取测试
LoadFileTest();
return 0;
}
CMakeLists.txt文件
cmake_minimum_required(VERSION 3.10)
project(testSerialize)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -march=native")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
add_executable(testSerialize testSerialize.cpp)
target_link_libraries(testSerialize -lboost_serialization)
输出结果: