#include #include #include "event.h" #include "action.h" #include "object.h" void Object::rename(const string& name) { heap.forget(this); heap.add(this, name); } // This shouldn't be called, I think Object& Object::operator<< (SmartPtr rhs) { return *this; } // Why not define this in the header file? Environment::Environment(const string& name = ""): my_event_lock(false), Object(name) {} // Queue an event Environment& Environment::operator<< (SmartPtr rhs) { my_events.push(rhs); if (my_event_lock) return *this; // Process all queued events my_event_lock = true; while (!my_events.empty()) { SmartPtr ev = my_events.front(); my_events.pop(); try {ev->throw_self();} /* catch (AddObj x) { Pack()(x.obj(), this); } catch (MAddObj x) { for_each(x.obj().begin(), x.obj().end(), bind2nd(Pack(), this)); } catch (DelObj x) { Kill()(x.obj(), this); } catch (MDelObj x) { for_each(x.obj().begin(), x.obj().end(), bind2nd(Kill(), this)); } catch (DelAllObj) { for_each(my_objects.begin(), my_objects.end(), bind2nd(Kill(), this)); }*/ catch (Event) { for_each(my_objects.begin(), my_objects.end(), bind2nd(Send(), ev)); } } my_event_lock = false; return *this; } Environment& Environment::add(Object* obj) { Pack()(obj, this); return *this; } Environment& Environment::del(Object* obj) { Kill()(obj, this); return *this; } // Add an object to my list void Environment::Pack::operator() (Object* obj, Environment* env) const { env->my_objects.push_back(obj); obj->set_env(env); } // Remove an object from my list void Environment::Kill::operator() (Object* obj, Environment* env) const { // TODO: support (simultaneous?) removal from heap remove(env->my_objects.begin(), env->my_objects.end(), obj); } // Send an event to an object void Environment::Send::operator() (Object* obj, SmartPtr ev) const { *obj << ev; }