-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspdf.cpp
114 lines (94 loc) · 2.65 KB
/
spdf.cpp
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
#include <iostream>
#include <string>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
template <class Lattice>
class serializable
{
public:
typedef boost::archive::text_oarchive OutArchive;
typedef boost::archive::text_iarchive InArchive;
Lattice* _lattice;
serializable(Lattice* that) : _lattice(that)
{
}
template <typename Archive>
void serialize(Archive& archive, const unsigned int version = 0)
{
static_cast<Lattice*>(_lattice)->serialize(archive);
}
template <typename Archive>
void deserialize(Archive& archive, const unsigned int version = 0)
{
static_cast<Lattice*>(this)->deseralize(archive);
}
};
class Lattice : public serializable<Lattice>
{
public:
int a;
Lattice() : serializable<Lattice> (this) {}
Lattice(int _a) : serializable<Lattice> (this)
{
a = _a;
}
void str(std::string indent=" ")
{
std::cout << "val :" << indent << a << std::endl;
}
template <typename Archive>
void serialize(Archive& archive, const unsigned int version = 0)
{
static_cast<Lattice*>(_lattice)->save(archive);
}
template<typename Archive>
void deserialize(Archive& archive, const unsigned int version = 0)
{
static_cast<Lattice*>(_lattice)->load(archive);
}
virtual void save(OutArchive& archive, const unsigned int version = 0)
{
archive & a;
}
virtual void load(InArchive& archive, const unsigned int version = 0)
{
archive >> a;
}
};
class FiniteLattice: public Lattice
{
public:
float _fa;
FiniteLattice() { }
FiniteLattice(float _that) : Lattice(), _fa(_that) { }
void save(OutArchive& archive, const unsigned int version = 0)
{
//static_cast<Lattice*>(_lattice)->save(archive);
archive & _fa;
}
void load(InArchive& archive, const unsigned int version = 0)
{
//static_cast<Lattice*>(_lattice)->load(archive);
archive & _fa;
}
void str(std::string indent=" ")
{
std::cout << "fval:" << indent << _fa << std::endl;
}
};
int main()
{
std::stringstream rw_stream;
Lattice* lattice = new Lattice(10);
FiniteLattice* flattice = new FiniteLattice(2.1);
Lattice::OutArchive out_archive(rw_stream);
lattice->serialize(out_archive);
flattice->serialize(out_archive);
std::cout << rw_stream.str() << std::endl;
Lattice::InArchive in_archive(rw_stream);
Lattice* rflattice = new FiniteLattice();
rflattice->deserialize(in_archive);
dynamic_cast<FiniteLattice*>(rflattice)->str();
return 0;
}