Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
//: C25:DynaTrash.cpp //{L} TrashPrototypeInit //{L} FillBin Trash TrashStatics // Using a map of vectors and RTTI // to automatically sort Trash into // vectors. This solution, despite the // use of RTTI, is extensible. #include <iostream> #include <fstream> #include <vector> #include <map> #include <typeinfo> #include "Trash.h" #include "fillBin.h" #include "sumValue.h" #include "../purge.h" using namespace std; ofstream out("DynaTrash.out"); // Must adapt from type_info in Standard C++, // since type_info is too restrictive: template<class T> // T should be a base class class TypeInfo { string id; public: TypeInfo(T* t) : id(typeid(*t).name()) {} const string& name() { return id; } friend bool operator<(const TypeInfo& lv, const TypeInfo& rv){ return lv.id < rv.id; } }; class TypeMap : public map<TypeInfo<Trash>, vector<Trash*> >, public Fillable { public: // Satisfy the Fillable interface: void addTrash(Trash* t) { (*this)[TypeInfo<Trash>(t)].push_back(t); } ~TypeMap() { for(iterator it = begin(); it != end(); it++) purge((*it).second); } }; int main() { TypeMap bin; fillBin("Trash.dat", bin); // Sorting happens TypeMap::iterator it; for(it = bin.begin(); it != bin.end(); it++) sumValue((*it).second); } ///:~