Object.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <algorithm>
  2. #include <boost/foreach.hpp>
  3. #include "anki/core/Object.h"
  4. #include "anki/util/Assert.h"
  5. #include "anki/util/Exception.h"
  6. namespace anki {
  7. //==============================================================================
  8. // Constructor =
  9. //==============================================================================
  10. Object::Object(Object* parent):
  11. objParent(NULL)
  12. {
  13. if(parent != NULL)
  14. {
  15. parent->addChild(this);
  16. }
  17. }
  18. //==============================================================================
  19. // Destructor =
  20. //==============================================================================
  21. Object::~Object()
  22. {
  23. if(objParent != NULL)
  24. {
  25. objParent->removeChild(this);
  26. }
  27. // delete all children
  28. BOOST_REVERSE_FOREACH(Object* child, objChilds)
  29. {
  30. delete child;
  31. }
  32. }
  33. //==============================================================================
  34. // addChild =
  35. //==============================================================================
  36. void Object::addChild(Object* child)
  37. {
  38. ANKI_ASSERT(child != NULL);
  39. ANKI_ASSERT(child->objParent == NULL); // Child already has parent
  40. child->objParent = this;
  41. objChilds.push_back(child);
  42. }
  43. //==============================================================================
  44. // removeChild =
  45. //==============================================================================
  46. void Object::removeChild(Object* child)
  47. {
  48. ANKI_ASSERT(child != NULL);
  49. Container::iterator it = std::find(objChilds.begin(), objChilds.end(),
  50. child);
  51. if(it == objChilds.end())
  52. {
  53. throw ANKI_EXCEPTION("Internal error");
  54. }
  55. objChilds.erase(it);
  56. child->objParent = NULL;
  57. }
  58. } // end namespace