-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
80 lines (58 loc) · 1.9 KB
/
test.cpp
File metadata and controls
80 lines (58 loc) · 1.9 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
#include <iostream>
#include <sstream>
#include <fstream>
#include <cereal/cereal.hpp>
#include "types.h"
#include "Tuple.h"
#include "IPluggableSerializer.h"
#include "CerealSerializer.h"
void writeToFile(std::string fname, std::stringstream &ss){
std::ofstream myfile;
myfile.open (fname, std::ios_base::app);
myfile << ss.str();
myfile.close();
}
void readFromFile(std::string fname, std::stringstream &ss){
std::ifstream myfile( fname );
if ( myfile ) {
ss << myfile.rdbuf();
myfile.close();
}
}
int main() {
////////////////////////
//* Sending a tuple *//
//////////////////////
// sstream: Will contain the serialization of a tuple
std::stringstream os;
// Elements
std::shared_ptr<Element> eInt(new Int(15));
std::shared_ptr<Element> eDouble(new Double(3.14159));
std::shared_ptr<Element> eString(new String("Jordi"));
// Tuple
Tuple tuple;
tuple.Set("Worker", eString);
tuple.Set("Salary", eInt);
tuple.Set("Phi", eDouble);
// Serializer
IPluggableSerializer *CSerializer = new CerealSerializer();
tuple.serialize(CSerializer, os);
writeToFile("serialize.out", os);
std::cout << "Cross your fingers" << std::endl;
//////////////////////////
//* Recieving a tuple *//
////////////////////////
std::stringstream is;
readFromFile("serialize.out", is);
Tuple new_tuple;
new_tuple.deserialize(CSerializer, is);
auto Salary =
std::static_pointer_cast<Int>(new_tuple.Get("Salary"));
auto Phi =
std::static_pointer_cast<Double>(new_tuple.Get("Phi"));
auto Worker =
std::static_pointer_cast<String>(new_tuple.Get("Worker"));
std::cout << "Salary: " << Salary->getValue() << std::endl;
std::cout << "Phi: " << Phi->getValue() << std::endl;
std::cout << "Worker: " << Worker->getValue() << std::endl;
}