GObject.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "GObject.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include <AK/Assertions.h>
  5. GObject::GObject(GObject* parent)
  6. : m_parent(parent)
  7. {
  8. if (m_parent)
  9. m_parent->addChild(*this);
  10. }
  11. GObject::~GObject()
  12. {
  13. if (m_parent)
  14. m_parent->removeChild(*this);
  15. auto childrenToDelete = move(m_children);
  16. for (auto* child : childrenToDelete)
  17. delete child;
  18. }
  19. void GObject::event(GEvent& event)
  20. {
  21. switch (event.type()) {
  22. case GEvent::Timer:
  23. return timerEvent(static_cast<GTimerEvent&>(event));
  24. case GEvent::DeferredDestroy:
  25. delete this;
  26. break;
  27. case GEvent::Invalid:
  28. ASSERT_NOT_REACHED();
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. void GObject::addChild(GObject& object)
  35. {
  36. m_children.append(&object);
  37. }
  38. void GObject::removeChild(GObject& object)
  39. {
  40. for (unsigned i = 0; i < m_children.size(); ++i) {
  41. if (m_children[i] == &object) {
  42. m_children.remove(i);
  43. return;
  44. }
  45. }
  46. }
  47. void GObject::timerEvent(GTimerEvent&)
  48. {
  49. }
  50. void GObject::startTimer(int ms)
  51. {
  52. if (m_timerID) {
  53. dbgprintf("GObject{%p} already has a timer!\n", this);
  54. ASSERT_NOT_REACHED();
  55. }
  56. }
  57. void GObject::stopTimer()
  58. {
  59. if (!m_timerID)
  60. return;
  61. m_timerID = 0;
  62. }
  63. void GObject::delete_later()
  64. {
  65. GEventLoop::main().post_event(this, make<GEvent>(GEvent::DeferredDestroy));
  66. }