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
|
#include <armadillo>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/void_cast_fwd.hpp>
#include <boost/serialization/binary_object.hpp>
#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>
#include <vector>
class Engine
{
public:
Engine () {}
Engine (const int cyl) : _cyl ( cyl ) {}
int getCyl () const { return _cyl; }
private:
int _cyl;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & _cyl;
}
};
class Car {
public:
virtual char const* type() const = 0;
virtual void printMember () const = 0;
virtual void save (const std::string file) const { std::cout << "file" << std::endl; };
virtual ~Car() = default;
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {};
};
class Audi : public Car, public std::enable_shared_from_this<Car> {
public:
Audi() {};
Audi(const std::string owner, const int hp,const unsigned int cyl)
: _owner ( owner ),
_hp ( hp ),
_eng ( std::make_shared<Engine>(cyl) )
{ }
char const* type() const override { return "Audi"; }
void printMember () const override
{
std::cout
<< this->type() << ":\n"
<< "owner: " << _owner
<< " hp: " << _hp
<< " engine: " << _eng->getCyl()
<< std::endl;
}
void save (const std::string file) const {
std::ofstream of(file, std::ofstream::binary);
std::stringstream strs;
boost::archive::binary_oarchive ar(of);
std::shared_ptr<const Car> audi = shared_from_this();
boost::serialization::make_binary_object(&audi, sizeof(audi));
}
private:
std::string _owner;
int _hp;
std::shared_ptr<Engine> _eng;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & boost::serialization::base_object<Car>(*this);
ar & _owner;
ar & _hp;
ar & _eng;
}
};
Audi& loadAudi (const std::string file_name) {
std::cout << "Loading " << file_name << std::endl;
std::ifstream in_f(file_name, std::ifstream::binary);
std::cout << "1" << std::endl;
std::shared_ptr<Car> audi; //= std::make_unique<Audi>();
std::cout << "2" << std::endl;
boost::archive::binary_iarchive ar(in_f);
std::cout << "3" << std::endl;
ar& audi;
std::cout << "4" << std::endl;
return dynamic_cast<Audi &>(*audi);
};
BOOST_CLASS_EXPORT(Audi);
BOOST_CLASS_EXPORT(Engine);
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Car); //Tell Boost that Car is abstract
int main() {
std::string save_file = "test.dat";
std::ofstream of(save_file, std::ofstream::binary);
std::ifstream in_f(save_file, std::ifstream::binary);
{
std::shared_ptr<Car> audi = std::make_shared<Audi>("Wilma", 3, 16);
audi->printMember();
std::stringstream strs;
boost::archive::binary_oarchive ar(of);
ar& audi;
// audi_save_dir = strs.str();
std::cout << "Count of audi:" << audi.use_count() << std::endl;
ar << boost::serialization::make_binary_object(&audi, sizeof(audi));
}
{
std::shared_ptr<Car> audi; //= std::make_unique<Audi>();
std::cout << "Deserialize: Count of audi:" << audi.use_count() << std::endl;
//std::stringstream strs(f);
boost::archive::binary_iarchive ar(in_f);
ar& audi;
Audi& d = dynamic_cast<Audi &>(*audi);
std::cout << "Deserialize: Count of audi:" << audi.use_count() << std::endl;
std::cout << "Print Audi:" << std::endl;
d.printMember();
}
std::shared_ptr<Car> audi = std::make_shared<Audi>("Daniel", 200, 8);
audi->printMember();
std::string test_dat = "my-new-audi.dat";
audi->save(test_dat);
Audi& d = loadAudi(test_dat);
d.printMember();
return 0;
}
| |