#include #include #include #include #include "object.h" #include "heap.h" Heap::~Heap() { for (map::iterator i = my_objects.begin(); i != my_objects.end(); i++) delete i->second; } void Heap::add(Object* obj, string name) { if (name == "") { // TODO: make this more C++-ish char buf[256]; sprintf(buf, "__%u", my_temp_counter); my_temp_counter++; name = buf; } if (my_objects.find(name) != my_objects.end()) return; my_objects.insert(make_pair(name, obj)); } // I'm not sure that "forget" is a good idea void Heap::forget(const string& name) { my_objects.erase(name); } void Heap::forget(Object* obj) { my_objects.erase(obj->name()); } void Heap::del(const string& name) { delete (*this)[name]; forget(name); } void Heap::del(Object* obj) { forget(obj); delete obj; } // Return the name that matches the object const string& Heap::name(const Object* obj) { for (map::iterator i = my_objects.begin(); i != my_objects.end(); i++) if (i->second == obj) return i->first; } // Define the global heap Heap heap;