//: C09:DynaTrash.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt //{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 "Trash.h" #include "fillBin.h" #include "sumValue.h" #include "../purge.h" #include #include #include #include #include using namespace std; ofstream out("DynaTrash.out"); // Must adapt from type_info in Standard C++, // since type_info is too restrictive: template // 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, vector >, public Fillable { public: // Satisfies the Fillable interface: void addTrash(Trash* t) { (*this)[TypeInfo(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); } ///:~