1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include "stdafx.h"
#include <cstddef> // NULL
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <boost/timer.hpp>
#include <boost/archive/tmpdir.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/thread/thread.hpp>
using namespace std;
class gps_position
{
public:
friend std::ostream & operator<<(std::ostream &os, const gps_position &gp);
friend class boost::serialization::access;
int degrees;
int minutes;
float seconds;
std::string myString;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & BOOST_SERIALIZATION_NVP(degrees);
ar & BOOST_SERIALIZATION_NVP(minutes);
ar & BOOST_SERIALIZATION_NVP(seconds);
ar & BOOST_SERIALIZATION_NVP(myString);
//ar & degrees & minutes & seconds;
}
//template<class Archive>
// void save(Archive & ar, const unsigned int version) const
// {
// // note, version is always the latest when saving
// ar & degrees;
// ar & minutes;
// ar & seconds;
// ar & myString;
// }
// template<class Archive>
// void load(Archive & ar, const unsigned int version)
// {
// ar & degrees;
// ar & minutes;
// ar & seconds;
// ar & myString;
// }
// BOOST_SERIALIZATION_SPLIT_MEMBER()
public:
// every serializable class needs a constructor
gps_position(){};
gps_position(int _d, int _m, float _s,std::string mystring) :
degrees(_d), minutes(_m), seconds(_s)
{myString =mystring;}
};
class MapPoints
{
public:
int nbObjects;
std::list<gps_position> vectPoints;
MapPoints(int maxObjct):nbObjects(maxObjct){};
MapPoints(){vectPoints ; };
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & BOOST_SERIALIZATION_NVP(vectPoints);
//ar & degrees & minutes & seconds;
}
//template<class Archive>
// void save(Archive & ar, const unsigned int version) const
// {
// // note, version is always the latest when saving
// ar & vectPoints;
// //ar & nbObjects;
// }
// template<class Archive>
// void load(Archive & ar, const unsigned int version)
// {
// ar & vectPoints;
// //ar & nbObjects;
// }
// BOOST_SERIALIZATION_SPLIT_MEMBER()
void buildMap()
{
std::string s= "AAAA";
for(int i(0);i<nbObjects;++i)
{
//s+="BBB";
gps_position points (i,i,i,""); //= new gps_position(i,i,i,"");
vectPoints.push_back(points);
}
}
};
std::ostream & operator<<(std::ostream &os, const gps_position &gp)
{
return os << ' ' << gp.degrees << (unsigned char)186 << gp.minutes << '\'' << gp.seconds << '"';
}
void save_Position(const gps_position &s, const char * filename){
// make an archive
std::ofstream ofs(filename);
assert(ofs.good());
boost::archive::binary_oarchive oa(ofs);
using boost::serialization::make_nvp;
oa <<BOOST_SERIALIZATION_NVP(s);
}
void
restore_Position(gps_position &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
assert(ifs.good());
boost::archive::binary_iarchive ia(ifs);
using boost::serialization::make_nvp;
ia >>BOOST_SERIALIZATION_NVP(s);
}
void save_Position(const MapPoints &s, const char * filename){
// make an archive
std::ofstream ofs(filename);
assert(ofs.good());
boost::archive::binary_oarchive oa(ofs);
using boost::serialization::make_nvp;
oa <<BOOST_SERIALIZATION_NVP(s);
}
void restore_Position(MapPoints &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
assert(ifs.good());
boost::archive::binary_iarchive ia(ifs);
using boost::serialization::make_nvp;
ia >>BOOST_SERIALIZATION_NVP(s);
}
int main(int argc, char *argv[])
{
try {
MapPoints mapPoint (30);
mapPoint.buildMap();
std::string filename = "C:\\temp\\TEST.bin";
save_Position(mapPoint,filename.c_str());
MapPoints mapPoint2;
restore_Position(mapPoint2,filename.c_str());
}
catch(std::exception const& e)
{
std::cout<<"Error"<<e.what()<<std::endl;
}
catch(...)
{
std::cout<<"Error"<<std::endl;
}
return 0;
} |
Partager