Camera例子
cpp代码
#include
#include
#include
#include
#include
#include
#include
#include
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;
}
CMakeList.txt
cmake_minimum_required(VERSION 3.10)
project(testSerialize)
set(CMAKE_BUILD_TYPE "Release")
add_compile_options(-std=c++11)
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)
输出
官方demo1
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class gps_position
{
friend std::ostream & operator<<(std::ostream &os, const gps_position &gp);
friend class boost::serialization::access;
int degrees;
int minutes;
float seconds;
template<class Archive>
void serialize(Archive & ar, const unsigned int ){
ar & degrees & minutes & seconds;
}
public:
gps_position(){};
gps_position(int _d, int _m, float _s) :
degrees(_d), minutes(_m), seconds(_s)
{}
};
std::ostream & operator<<(std::ostream &os, const gps_position &gp)
{
return os << ' ' << gp.degrees << (unsigned char)186 << gp.minutes << '\'' << gp.seconds << '"';
}
class bus_stop
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
virtual std::string description() const = 0;
gps_position latitude;
gps_position longitude;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & latitude;
ar & longitude;
}
protected:
bus_stop(const gps_position & _lat, const gps_position & _long) :
latitude(_lat), longitude(_long)
{}
public:
bus_stop(){}
virtual ~bus_stop(){}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(bus_stop)
std::ostream & operator<<(std::ostream &os, const bus_stop &bs)
{
return os << bs.latitude << bs.longitude << ' ' << bs.description();
}
class bus_stop_corner : public bus_stop
{
friend class boost::serialization::access;
std::string street1;
std::string street2;
virtual std::string description() const
{
return street1 + " and " + street2;
}
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<bus_stop>(*this);
ar & street1 & street2;
}
public:
bus_stop_corner(){}
bus_stop_corner(const gps_position & _lat, const gps_position & _long,
const std::string & _s1, const std::string & _s2
) :
bus_stop(_lat, _long), street1(_s1), street2(_s2)
{
}
};
class bus_stop_destination : public bus_stop
{
friend class boost::serialization::access;
std::string name;
virtual std::string description() const
{
return name;
}
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<bus_stop>(*this) & name;
}
public:
bus_stop_destination(){}
bus_stop_destination(
const gps_position & _lat, const gps_position & _long, const std::string & _name
) :
bus_stop(_lat, _long), name(_name)
{
}
};
class bus_route
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
typedef bus_stop * bus_stop_pointer;
std::list<bus_stop_pointer> stops;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar.register_type(static_cast<bus_stop_corner *>(NULL));
ar.register_type(static_cast<bus_stop_destination *>(NULL));
ar & stops;
}
public:
bus_route(){}
void append(bus_stop *_bs)
{
stops.insert(stops.end(), _bs);
}
};
std::ostream & operator<<(std::ostream &os, const bus_route &br)
{
std::list<bus_stop *>::const_iterator it;
for(it = br.stops.begin(); it != br.stops.end(); it++){
os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
}
return os;
}
class bus_schedule
{
public:
struct trip_info
{
template<class Archive>
void serialize(Archive &ar, const unsigned int file_version)
{
if(file_version >= 2)
ar & driver;
ar & hour & minute;
}
int hour;
int minute;
std::string driver;
trip_info(){}
trip_info(int _h, int _m, const std::string &_d) :
hour(_h), minute(_m), driver(_d)
{}
};
private:
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const bus_schedule &bs);
friend std::ostream & operator<<(std::ostream &os, const bus_schedule::trip_info &ti);
std::list<std::pair<trip_info, bus_route *> > schedule;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & schedule;
}
public:
void append(const std::string &_d, int _h, int _m, bus_route *_br)
{
schedule.insert(schedule.end(), std::make_pair(trip_info(_h, _m, _d), _br));
}
bus_schedule(){}
};
BOOST_CLASS_VERSION(bus_schedule::trip_info, 2)
std::ostream & operator<<(std::ostream &os, const bus_schedule::trip_info &ti)
{
return os << '\n' << ti.hour << ':' << ti.minute << ' ' << ti.driver << ' ';
}
std::ostream & operator<<(std::ostream &os, const bus_schedule &bs)
{
std::list<std::pair<bus_schedule::trip_info, bus_route *> >::const_iterator it;
for(it = bs.schedule.begin(); it != bs.schedule.end(); it++){
os << it->first << *(it->second);
}
return os;
}
void save_schedule(const bus_schedule &s, const char * filename){
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
void
restore_schedule(bus_schedule &s, const char * filename)
{
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
ia >> s;
}
int main(int argc, char *argv[])
{
bus_schedule original_schedule;
bus_stop *bs0 = new bus_stop_corner(
gps_position(34, 135, 52.560f),
gps_position(134, 22, 78.30f),
"24th Street", "10th Avenue"
);
bus_stop *bs1 = new bus_stop_corner(
gps_position(35, 137, 23.456f),
gps_position(133, 35, 54.12f),
"State street", "Cathedral Vista Lane"
);
bus_stop *bs2 = new bus_stop_destination(
gps_position(35, 136, 15.456f),
gps_position(133, 32, 15.300f),
"White House"
);
bus_stop *bs3 = new bus_stop_destination(
gps_position(35, 134, 48.789f),
gps_position(133, 32, 16.230f),
"Lincoln Memorial"
);
bus_route route0;
route0.append(bs0);
route0.append(bs1);
route0.append(bs2);
original_schedule.append("bob", 6, 24, &route0);
original_schedule.append("bob", 9, 57, &route0);
original_schedule.append("alice", 11, 02, &route0);
bus_route route1;
route1.append(bs3);
route1.append(bs2);
route1.append(bs1);
original_schedule.append("ted", 7, 17, &route1);
original_schedule.append("ted", 9, 38, &route1);
original_schedule.append("alice", 11, 47, &route1);
std::cout << "original schedule";
std::cout << original_schedule;
std::string filename(boost::archive::tmpdir());
filename += "/demofile.txt";
save_schedule(original_schedule, filename.c_str());
bus_schedule new_schedule;
restore_schedule(new_schedule, filename.c_str());
std::cout << "\nrestored schedule";
std::cout << new_schedule;
delete bs0;
delete bs1;
delete bs2;
delete bs3;
return 0;
}
输出