-
Notifications
You must be signed in to change notification settings - Fork 0
/
CitizenLoader.cpp
61 lines (58 loc) · 1.44 KB
/
CitizenLoader.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
#include "CitizenLoader.h"
#define rcastc reinterpret_cast<char*>
#define rcastcc reinterpret_cast<const char*>
namespace elc {
Citizen* CitizenLoader::load(istream& in)
{
CITIZEN type;
in.read(rcastc(&type), sizeof(type));
switch (type)
{
case CITIZEN::Regular:
try {
return new Citizen(in);
break;
}
catch (bad_alloc& ex) {
cout << ex.what() << endl;
exit(1);
}
case CITIZEN::Representative:
int did, pid;
in.read(reinterpret_cast<char*>(&did), sizeof(did));
in.read(reinterpret_cast<char*>(&pid), sizeof(pid));
try{
Representative* rp = new Representative(in);
rp->setDistrictID(did);
rp->setPartyID(pid);
return rp;
break;
}
//catch (invalid_argument& ex) {
// throw invalid_argument(ex);
// cout << " --------------------------------------- " << endl;
// cout << ex.what() << endl;
// cout << " --------------------------------------- " << endl;
//}
catch (bad_alloc& ex) {
cout << ex.what() << endl;
exit(1);
}
}
}
void CitizenLoader::save(Citizen* c,ostream& out)
{
CITIZEN type;
Representative* r = dynamic_cast<Representative*>(c);
if (r == nullptr) {
type = CITIZEN::Regular;
out.write(rcastcc(&type), sizeof(type));
c->save(out);
}
else {
type = CITIZEN::Representative;
out.write(rcastcc(&type), sizeof(type));
r->save(out);
}
}
}