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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <typeinfo>
struct Person {
std::string name;
struct Visitor {virtual ~Visitor() = default;};
Person() = default;
Person (const std::string& newName) : name(newName) {}
virtual ~Person() = default;
virtual bool accept (Visitor&, std::ofstream&) = 0;
virtual bool accept (Visitor&, std::ifstream&) = 0;
};
#define VISITOR_AND_ACCEPT(ClassName) \
struct Visitor { \
virtual ~Visitor() = default; \
virtual bool visit (ClassName*, std::ofstream&) {return false;} /* cannot be pure anymore */ \
virtual bool visit (ClassName*, std::ifstream&) {return false;} /* cannot be pure anymore */ \
}; \
virtual bool accept (Person::Visitor& visitor, std::ofstream& outfile) override { \
ClassName::Visitor* v = dynamic_cast<ClassName::Visitor*>(&visitor); \
return v ? v->visit (this, outfile) : false; \
} \
virtual bool accept (Person::Visitor& visitor, std::ifstream& infile) override { \
ClassName::Visitor* v = dynamic_cast<ClassName::Visitor*>(&visitor); \
return v ? v->visit (this, infile) : false; \
}
struct Guy : Person {
VISITOR_AND_ACCEPT(Guy)
int coolness;
Guy() = default;
Guy (const std::string& name, int a) : Person(name), coolness(a) {}
};
struct Girl : Person {
VISITOR_AND_ACCEPT(Girl)
int beauty, cupsize;
Girl() = default;
Girl (const std::string& name, int a, int b) : Person(name), beauty(a), cupsize(b) {}
};
struct Boxer : Guy {
VISITOR_AND_ACCEPT(Boxer)
int punchingStrength, numKnockouts;
Boxer() = default;
Boxer (const std::string& name, int a, int b, int c) : Guy(name, a), punchingStrength(b), numKnockouts(c) {}
};
struct Cheerleader : Girl {
VISITOR_AND_ACCEPT(Cheerleader)
int cheeringLoudness, batonSkill;
Cheerleader() = default;
Cheerleader (const std::string& name, int a, int b, int c, int d) : Girl(name, a, b),
cheeringLoudness(c), batonSkill(d) {}
};
class System {
public:
struct SaveVisitor;
struct LoadVisitor;
private:
static const std::string PATH, DATABASE, SUFFIX;
static const std::map<std::string, Person*> classMap;
static std::list<Person*> personnel;
public:
static System& GetInstance() {static System* instance = new System; return *instance;}
static inline void initialize();
static inline void loadDatabase();
static inline void saveDatabase();
private:
System() {}; System(const System&); const System& operator = (const System&); ~System() {};
};
std::list<Person*> System::personnel;
const std::string System::PATH = "", System::DATABASE = "Database", System::SUFFIX = ".txt"; // put whatever PATH you want
const std::map<std::string, Person*> System::classMap = {
{typeid(Guy).name(), new Guy}, {typeid(Girl).name(), new Girl}, {typeid(Boxer).name(), new Boxer}, {typeid(Cheerleader).name(), new Cheerleader}
};
struct System::SaveVisitor : Person::Visitor, Guy::Visitor, Girl::Visitor, Boxer::Visitor, Cheerleader::Visitor {
virtual bool visit (Guy* guy, std::ofstream& outfile) override {
outfile << guy->coolness << std::endl;
return true;
}
virtual bool visit (Girl* girl, std::ofstream& outfile) override {
outfile << girl->beauty << std::endl;
outfile << girl->cupsize << std::endl;
return true;
}
virtual bool visit (Boxer* boxer, std::ofstream& outfile) override {
outfile << boxer->coolness << std::endl;
outfile << boxer->punchingStrength << std::endl;
outfile << boxer->numKnockouts << std::endl;
return true;
}
virtual bool visit (Cheerleader* cheerleader, std::ofstream& outfile) override {
outfile << cheerleader->beauty << std::endl;
outfile << cheerleader->cupsize << std::endl;
outfile << cheerleader->cheeringLoudness << std::endl;
outfile << cheerleader->batonSkill << std::endl;
return true;
}
};
struct System::LoadVisitor : Person::Visitor, Guy::Visitor, Girl::Visitor, Boxer::Visitor, Cheerleader::Visitor {
virtual bool visit (Guy* guy, std::ifstream& infile) override {
std::string name;
int coolness;
std::getline (infile, name);
infile >> coolness;
*guy = Guy (name, coolness);
personnel.emplace_back (guy);
return true;
}
virtual bool visit (Girl* girl, std::ifstream& infile) override {
std::string name;
int beauty, cupsize;
std::getline (infile, name);
infile >> beauty;
infile >> cupsize;
*girl = Girl (name, beauty, cupsize);
personnel.emplace_back (girl);
return true;
}
virtual bool visit (Boxer* boxer, std::ifstream& infile) override {
std::string name;
int coolness, punchingStrength, numKnockouts;
std::getline (infile, name);
infile >> coolness;
infile >> punchingStrength;
infile >> numKnockouts;
*boxer = Boxer (name, coolness, punchingStrength, numKnockouts);
personnel.emplace_back (boxer);
return true;
}
virtual bool visit (Cheerleader* cheerleader, std::ifstream& infile) override {
std::string name;
int beauty, cupsize, cheeringLoudness, batonSkill;
std::getline (infile, name);
infile >> beauty; // some repeats here, but whatever
infile >> cupsize;
infile >> cheeringLoudness;
infile >> batonSkill;
*cheerleader = Cheerleader (name, beauty, cupsize, cheeringLoudness, batonSkill);
personnel.emplace_back (cheerleader);
return true;
}
};
inline void System::initialize() {
char yesNo;
std::cout << "New database? (y/n) "; std::cin >> yesNo;
if (yesNo == 'y')
{
std::ofstream file (PATH + DATABASE + SUFFIX);
personnel = {
new Guy ("John Doe", 8), new Girl ("Madame Yes", 9, 3),
new Boxer ("Rocky Balboa", 6, 9, 20), new Cheerleader ("Mary May", 10, 5, 8, 9)
};
}
else
loadDatabase();
}
inline void System::saveDatabase() {
std::ofstream outfile;
outfile.open ((PATH + DATABASE + SUFFIX).c_str());
SaveVisitor saveVisitor;
for (Person* x : personnel)
{
outfile << x->name << std::endl;
std::ofstream profile (x->name + ".txt");
profile << typeid(*x).name() << std::endl;
profile << x->name << std::endl;
x->accept (saveVisitor, profile); // The magical line!
}
}
inline void System::loadDatabase() {
std::ifstream infile (PATH + DATABASE + SUFFIX);
if (infile.fail()) {std::cout << "No such file exists." << std::endl; exit(0);}
std::string name, className;
std::list<std::string> listOfNames;
while (!infile.eof()) {
std::getline (infile, name);
listOfNames.emplace_back(name);
}
listOfNames.remove("");
LoadVisitor loadVisitor;
for (const std::string& x : listOfNames) {
std::ifstream profile (PATH + x + SUFFIX);
if (profile.fail()) {std::cout << PATH + x + SUFFIX << " does not exist." << std::endl; exit(0);}
std::getline (profile, className);
classMap.find(className)->second->accept (loadVisitor, profile); // The magical line!
}
std::cout << "Personnel retrieved:" << std::endl;
for (Person* x : personnel)
std::cout << x->name << ", " << typeid(*x).name() << std::endl;
// Now do whatever with the personnel members, since they have been created of the correct type.
}
int main() {
System::initialize();
System::saveDatabase();
std::cin.get(); std::cin.get();
}
|