-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialiser.h
More file actions
103 lines (91 loc) · 1.95 KB
/
Serialiser.h
File metadata and controls
103 lines (91 loc) · 1.95 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
#pragma once
#include "all.h"
using std::string;
using std::vector;
template<typename T>
class Serialiser {
private:
void getbuff();
void pushbuff();
char type_;
std::fstream io;
string fileName;
std::vector<T> buffer;
int last, numInBuff;
void update_buff();
public:
Serialiser(int, string);
void openInMode(char, int = 1);
bool empty();
T get();
void push(T);
void close();
~Serialiser();
};
template<typename T>
void Serialiser<T>::getbuff() {
numInBuff = 0;
last = 0;
T tmp;
while (numInBuff < (int)buffer.size() && (io >> tmp)) {
buffer[numInBuff++] = tmp;
}
}
template<typename T>
Serialiser<T>::~Serialiser() {
close();
std::remove(fileName.c_str());
}
template<typename T>
void Serialiser<T>::pushbuff() {
for (int i = 0; i < last; ++i)
io << buffer[i] << ' ';
numInBuff = 0;
last = 0;
}
template<typename T>
Serialiser<T>::Serialiser(int n, string s) {
fileName = s + "/" + std::to_string(n);
}
template<typename T>
void Serialiser<T>::openInMode(char mode, int buffSize) {
buffer.resize(buffSize);
if (mode == 'r') io.open(fileName, std::ios_base::in);
if (mode == 'w') io.open(fileName, std::ios_base::out);
type_ = mode;
assert(io.is_open());
last = 0;
numInBuff = 0;
}
template<typename T>
bool Serialiser<T>::empty() {
update_buff();
return last >= numInBuff;
}
template<typename T>
void Serialiser<T>::update_buff() {
if (last >= numInBuff) getbuff();
}
template<typename T>
T Serialiser<T>::get() {
assert(!empty());
update_buff();
return buffer[last++];
}
template<typename T>
void Serialiser<T>::push(T arg) {
if (last >= (int)buffer.size()) pushbuff();
buffer[last++] = arg;
}
template<typename T>
void Serialiser<T>::close() {
if (type_ == 'r') {
io.close();
return;
}
if (type_ == 'w') {
pushbuff();
io.close();
return;
}
}