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
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <iterator>
#include <algorithm>
#include <type_traits>
struct Thing {
std::string data;
void save (std::ostream& os) const {os << data << '\n';}
void load (std::istream& is) {std::string str; while (std::getline (is, data) && data.empty());}
};
class System {
private:
static std::map<Thing*, int> ThingMap;
static int seq_number;
public:
static const std::map<Thing*, int>& getThingMap() {return ThingMap;}
static inline void insertInThingMap (Thing*);
template <typename T, typename... Containers> static void save (std::ostream& os, Containers&... containers) {
saveMap (os, ThingMap);
SaveContainers<T, Containers...>()(os, ThingMap, containers...);
}
template <typename T, typename... Containers> static void load (std::istream& is, Containers&... containers) {
std::map<int, T> inverseMap = loadMap (is, ThingMap);
LoadContainers<T, Containers...>()(is, inverseMap, containers...);
}
static void clearThingMap() {ThingMap.clear();}
static void resetSequenceNumber() {seq_number = 0;}
private:
template <typename, typename...> struct SaveContainers;
template <typename, typename...> struct LoadContainers;
template <typename T> static inline void saveMap (std::ostream&, const std::map<T, int>&);
template <typename T> static inline std::map<int, T> loadMap (std::istream&, std::map<T, int>&);
template <typename T, typename Container> static void addToContainer (Container& container, const std::map<int, T*>& inverseMap,
const std::list<int>& sequenceNumbers, std::true_type) {
for (int x : sequenceNumbers) container.emplace (inverseMap.find(x)->second);
}
template <typename T, typename Container> static void addToContainer (Container& container, const std::map<int, T*>& inverseMap,
const std::list<int>& sequenceNumbers, std::false_type) {
for (int x : sequenceNumbers) container.emplace_back (inverseMap.find(x)->second);
}
};
std::map<Thing*, int> System::ThingMap;
int System::seq_number = 0;
inline void System::insertInThingMap (Thing* x) {
const auto it = ThingMap.find(x);
if (it == ThingMap.end())
ThingMap.emplace (x, seq_number++);
}
template <typename T>
inline void System::saveMap (std::ostream& os, const std::map<T, int>& map) {
os << map.size() << '\n';
for (const std::pair<T, int>& x : map) {
os << x.second << '\n';
x.first->save (os);
}
}
template <typename T>
inline std::map<int, T> System::loadMap (std::istream& is, std::map<T, int>& map) {
std::map<int, T> InverseMap;
int MapSize, sequenceNumber;
is >> MapSize;
for (int i = 0; i < MapSize; i++) {
is >> sequenceNumber;
typename std::remove_cv<T>::type t = new typename std::remove_cv<typename std::remove_reference<typename std::remove_pointer<T>::type>::type>::type;
t->load(is);
map.emplace (t, sequenceNumber);
InverseMap.emplace (sequenceNumber, t);
}
return InverseMap;
}
template <typename T>
struct System::SaveContainers<T> {
void operator()(std::ostream&, const std::map<T, int>&) const {}
};
template <typename T, typename First, typename... Rest>
struct System::SaveContainers<T, First, Rest...> : System::SaveContainers<T, Rest...>{
void operator()(std::ostream& os, const std::map<T, int>& map, const First& first, const Rest&... rest) const {
for (const T& t : first)
os << map.find(t)->second << ' ';
os << '\n';
System::SaveContainers<T, Rest...>::operator()(os, map, rest...);
}
};
template <typename T> struct HasInsert: public std::false_type {};
template<typename T, typename A>
struct HasInsert<std::set<T, A>> : public std::true_type {};
template<typename T, typename A>
struct HasInsert<std::map<T, A>> : public std::true_type {}; // What about unordered_map, multimap, unordered_set, multiset ??? Need to improve on this!
template <typename T>
struct System::LoadContainers<T> {
void operator()(std::istream&, const std::map<int, T>&) const {}
};
template <typename T, typename First, typename... Rest>
struct System::LoadContainers<T, First, Rest...> : System::LoadContainers<T, Rest...> {
void operator()(std::istream& is, const std::map<int, T>& inverseMap, First& first, Rest&... rest) const {
std::list<int> sequenceNumbers;
std::string stringOfSequenceNumbers;
while (std::getline (is, stringOfSequenceNumbers) && stringOfSequenceNumbers.empty());
std::istringstream line (stringOfSequenceNumbers);
std::copy (std::istream_iterator<int>(line), std::istream_iterator<int>(), std::back_inserter(sequenceNumbers));
addToContainer (first, inverseMap, sequenceNumbers, HasInsert<First>());
System::LoadContainers<T, Rest...>::operator()(is, inverseMap, rest...);
}
};
int main() {
std::vector<Thing> master { {"zero"}, {"one"}, {"two"}, {"three"}, {"zero"}, {"two"} };
Thing* p = std::addressof (master.front());
std::vector<Thing*> vector = { new Thing{"four"}, p, p+2, p+5, new Thing{"two"} };
std::list<Thing*> list = { new Thing{"one"}, p+3, p, p+2, new Thing{"two"} };
std::set<Thing*> set = { new Thing{"two"}, p, p+5, new Thing{"three"} };
// Populate System::ThingMap.
for (Thing* x : vector)
System::insertInThingMap(x);
for (Thing* x : list)
System::insertInThingMap(x);
for (Thing* x : set)
System::insertInThingMap(x);
std::cout << "Contents of System::getThingMap() are: \n";
for (const std::pair<const Thing*, int>& x : System::getThingMap())
std::cout << '#' << x.second << ' ' << x.first << ' ' << x.first->data << '\n';
std::cout << "\nContents of vector are: \n";
for (const Thing* x : vector)
std::cout << " " << x->data << ' ' << x << std::endl;
std::cout << "\nContents of list are: \n";
for (const Thing* x : list)
std::cout << " " << x->data << ' ' << x << std::endl;
std::cout << "\nContents of set are: \n";
for (const Thing* x : set)
std::cout << " " << x->data << ' ' << x << std::endl;
// Saving:
std::cout << "\n\nSaving...\n";
std::string fileName = "Save.txt";
{
std::ofstream outfile (fileName);
System::save<Thing*> (outfile, vector, list, set);
vector.clear(); list.clear(); set.clear();
System::clearThingMap(); System::resetSequenceNumber();
std::cout << "Everything cleared and all Thing objects destroyed as the program exits...\n\n";
std::cout << "---------------------------------------------\n";
}
// Loading:
std::cout << "\nLoading...\n";
{
std::ifstream infile (fileName);
System::load<Thing*> (infile, vector, list, set);
}
std::cout << "\nContents of the loaded System::getThingMap() are: \n";
for (const std::pair<const Thing*, int>& x : System::getThingMap())
std::cout << '#' << x.second << ' ' << x.first << ' ' << x.first->data << '\n';
std::cout << "\nContents of the loaded vector are: \n";
for (const Thing* x : vector)
std::cout << " " << x->data << ' ' << x << std::endl;
std::cout << "\nContents of the loaded list are: \n";
for (const Thing* x : list)
std::cout << " " << x->data << ' ' << x << std::endl;
std::cout << "\nContents of the loaded set are: \n";
for (const Thing* x : set)
std::cout << " " << x->data << ' ' << x << std::endl;
}
|