-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspi.cpp
More file actions
130 lines (101 loc) · 3.15 KB
/
Copy pathspi.cpp
File metadata and controls
130 lines (101 loc) · 3.15 KB
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
#include <iostream>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
namespace boost {
namespace serialization {
class access;
}
}
class Lattice
{
public:
int _ival;
Lattice() { }
Lattice(int ival) : _ival(ival) { }
friend class boost::serialization::access;
// NOTE: cannot be made virtual due to templated definition for serialize
template <class Archive>
void serialize(Archive& ar, const unsigned version = 0)
{
ar & _ival;
}
virtual void str(std::string indent=" ")
{
std::cout << "val:" << indent << _ival << std::endl;
}
};
class FiniteLattice: public Lattice
{
public:
float _fval;
FiniteLattice() { }
FiniteLattice(int ival, float fval) : Lattice(ival), _fval(fval) { }
friend class boost::serialization::access;
// NOTE: cannot be made virtual due to templated definition for serialize
template <class Archive>
void serialize(Archive& ar, const unsigned version = 0)
{
ar & _ival & _fval;
}
virtual void str(std::string indent=" ")
{
std::cout << "val:" << indent << this->_ival << "," << indent << _fval << std::endl;
}
};
class IntLattice
{
public:
typedef boost::archive::text_oarchive OutArchive;
typedef boost::archive::text_iarchive InArchive;
int val;
IntLattice() { }
IntLattice(int _val) : val(_val) { }
// boost looks for this method to serialize an object
// NOTE: Method can be templatized to have both saving/loading using '&' operator
void serialize(OutArchive& archive, const int version=0)
{
archive << val;
}
// NOTE: If not templatized, serialize() method should exist
// for both text_oarchive and text_iarchive for save/load
void serialize(InArchive& archive, const int version=0)
{
archive >> val;
}
};
int main()
{
// objects to serialize
Lattice* bw_lattice = new Lattice(10);
Lattice* dw_lattice = new FiniteLattice(13, 15.2);
// FiniteLattice* dw_lattice = new FiniteLattice(13, 15.2);
// objects to de-serialize into
Lattice* br_lattice = new Lattice();
Lattice* dr_lattice = new FiniteLattice();
// stream to serialize/de-serialize
std::stringstream rw_stream;
boost::archive::text_oarchive oarchive(rw_stream);
// serialization
// serialize method for Lattice will be generated
oarchive & *bw_lattice & *dw_lattice;
std::cout << rw_stream.str() << std::endl; // invoked the base class serialize method
// only base class properties are deserialized due to object slicing
boost::archive::text_iarchive iarchive(rw_stream);
// de-serialization
iarchive & *br_lattice & *dr_lattice;
br_lattice->str();
dr_lattice->str();
// testing IntLattice
{
std::stringstream stream;
boost::archive::text_oarchive outArchive(stream);
IntLattice latticea(20);
outArchive << latticea;
IntLattice latticeb;
boost::archive::text_iarchive inArchive(stream);
inArchive >> latticeb;
std::cout << latticeb.val << std::endl;
}
return 0;
}