Files
cppcourse/397/SolutionEx62Bonus2/Main.cpp
T
2026-10-05 11:27:21 +02:00

82 lines
2.1 KiB
C++

#include "Employee.h"
#include "Person.h"
#include <algorithm>
#include <cctype>
#include <iostream>
#include <list>
// header for file streams
#include <fstream>
// include headers that implement a archive in simple text format and XML format
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
// include header that allows serialization of STL list
#include <boost/serialization/export.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/nvp.hpp>
BOOST_CLASS_EXPORT_GUID(Employee, "Employee")
// STL collection of Person*
std::list<Person *> people;
void add(Person *newP) {
// creates a person, collects the information, adds person to list
newP->askForInfo();
people.push_back(newP);
}
void showPerson(Person *p) {
// displays a person
p->display();
}
void display() {
// displays all the people
std::for_each(people.begin(), people.end(), &showPerson);
}
int main() {
// deserialize existing file (if there is one)
try {
// create and open an archive for input
std::ifstream ifs("file.xml");
boost::archive::xml_iarchive ia(ifs);
// read class state from archive
ia >> BOOST_SERIALIZATION_NVP(people);
// archive and stream closed when destructors are called
} catch (...) {
}
char choice = 0;
while (choice != 'X') {
switch (choice) {
case 'P':
add(new Person());
break;
case 'E':
add(new Employee());
break;
case 'D':
display();
break;
}
std::cout << "Enter choice: Add [P]erson, add [E]mployee, [D]isplay, e[X]it: ";
std::cin >> choice;
choice = std::toupper(choice);
}
// save data to archive
std::ofstream ofs("file.xml");
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(people);
// archive and stream closed when destructors are called
return 0;
}