-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSavable.h
61 lines (52 loc) · 1.28 KB
/
Savable.h
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
/*
* Created: Before
* Last Modified: 8/16
* Last Successfully Comiled: NA
*/
#ifndef SAVABLE
#define SAVABLE
#include <stdio.h>
#include <string>
template <class T>
struct Saver{
bool (*save)(FILE* stream, T& obj) = NULL;
T* (*load)(FILE* stream) = NULL;
};
class Savable{
public:
virtual bool saveToStream(FILE* stream) = 0;
bool saveToFile(std::string filename){
FILE* fp=fopen(filename.c_str(), "w");
if(fp==NULL)
return false;
bool saved=saveToStream(fp);
fclose(fp);
return saved;
}
bool saveToFile(char* filename){
FILE* fp=fopen(filename, "w");
if(fp==NULL)
return false;
bool saved=saveToStream(fp);
fclose(fp);
return saved;
}
virtual bool loadFromStream(FILE* stream) = 0;
bool loadFromFile(std::string filename){
FILE* fp=fopen(filename.c_str(), "r");
if(fp==NULL)
return false;
bool loaded=loadFromStream(fp);
fclose(fp);
return loaded;
}
bool loadFromFile(char* filename){
FILE* fp=fopen(filename, "r");
if(fp==NULL)
return false;
bool loaded=loadFromStream(fp);
fclose(fp);
return loaded;
}
};
#endif