forked from CodeWithKartik/BookMyShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersistentObjectUtility.cpp
32 lines (31 loc) · 1.17 KB
/
PersistentObjectUtility.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
#include "PersistentObjectUtility.h"
#include "UserDatabase.h"
#include <typeinfo>
#include <cstring>
#include <fstream>
template <class DataType> PersistentObjectUtility<DataType>::PersistentObjectUtility(DataType &obj):
filename(std::strcat((std::strcpy((new char[strlen(typeid(obj).name())+strlen(FileNameSuffix)+1]), typeid(obj).name())), FileNameSuffix)) {
database_object = &obj;
}
template <class DataType> PersistentObjectUtility<DataType>::~PersistentObjectUtility() {
delete[] filename;
}
template <class DataType> void PersistentObjectUtility<DataType>::ReadFromFile(std::vector<char> &file_array) {
std::ifstream persistent_file;
persistent_file.open(filename);
if (persistent_file.is_open()) {
char c;
while (persistent_file.get(c)) {
file_array.push_back(c);
}
}
persistent_file.close();
}
template <class DataType> void PersistentObjectUtility<DataType>::WriteToFile() {
std::ofstream persistent_file;
persistent_file.open(filename);
persistent_file<<*database_object;
persistent_file.close();
}
//Template types to be used in this project
template class PersistentObjectUtility<UserDatabase>;