Hierarchy.inl.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. namespace anki {
  6. template<typename T>
  7. template<typename TAllocator>
  8. void Hierarchy<T>::destroy(TAllocator alloc)
  9. {
  10. if(m_parent != nullptr)
  11. {
  12. m_parent->removeChild(alloc, getSelf());
  13. m_parent = nullptr;
  14. }
  15. // Remove all children (fast version)
  16. auto it = m_children.getBegin();
  17. auto end = m_children.getEnd();
  18. for(; it != end; ++it)
  19. {
  20. Value* child = *it;
  21. child->m_parent = nullptr;
  22. }
  23. m_children.destroy(alloc);
  24. }
  25. template<typename T>
  26. template<typename TAllocator>
  27. void Hierarchy<T>::addChild(TAllocator alloc, Value* child)
  28. {
  29. ANKI_ASSERT(child != nullptr && "Null arg");
  30. ANKI_ASSERT(child != getSelf() && "Cannot put itself");
  31. ANKI_ASSERT(child->m_parent == nullptr && "Child already has parent");
  32. ANKI_ASSERT(child->findChild(getSelf()) == child->m_children.getEnd() && "Cyclic add");
  33. ANKI_ASSERT(findChild(child) == m_children.getEnd() && "Already has that child");
  34. child->m_parent = getSelf();
  35. m_children.emplaceBack(alloc, child);
  36. }
  37. template<typename T>
  38. template<typename TAllocator>
  39. void Hierarchy<T>::removeChild(TAllocator alloc, Value* child)
  40. {
  41. ANKI_ASSERT(child != nullptr && "Null arg");
  42. ANKI_ASSERT(child->m_parent == getSelf() && "Child has other parent");
  43. typename Container::Iterator it = findChild(child);
  44. ANKI_ASSERT(it != m_children.getEnd() && "Child not found");
  45. m_children.erase(alloc, it);
  46. child->m_parent = nullptr;
  47. }
  48. template<typename T>
  49. template<typename VisitorFunc>
  50. Error Hierarchy<T>::visitChildren(VisitorFunc vis)
  51. {
  52. Error err = Error::NONE;
  53. typename Container::Iterator it = m_children.getBegin();
  54. for(; it != m_children.getEnd() && !err; it++)
  55. {
  56. err = vis(*(*it));
  57. if(!err)
  58. {
  59. err = (*it)->visitChildren(vis);
  60. }
  61. }
  62. return err;
  63. }
  64. template<typename T>
  65. template<typename VisitorFunc>
  66. Error Hierarchy<T>::visitThisAndChildren(VisitorFunc vis)
  67. {
  68. Error err = vis(*getSelf());
  69. if(!err)
  70. {
  71. err = visitChildren(vis);
  72. }
  73. return err;
  74. }
  75. template<typename T>
  76. template<typename VisitorFunc>
  77. Error Hierarchy<T>::visitTree(VisitorFunc vis)
  78. {
  79. // Move to root
  80. Value* root = getSelf();
  81. while(root->m_parent != nullptr)
  82. {
  83. root = root->m_parent;
  84. }
  85. return root->visitThisAndChildren(vis);
  86. }
  87. template<typename T>
  88. template<typename VisitorFunc>
  89. Error Hierarchy<T>::visitChildrenMaxDepth(I maxDepth, VisitorFunc vis)
  90. {
  91. ANKI_ASSERT(maxDepth >= 0);
  92. Error err = Error::NONE;
  93. --maxDepth;
  94. typename Container::Iterator it = m_children.getBegin();
  95. for(; it != m_children.getEnd() && !err; ++it)
  96. {
  97. err = vis(*(*it));
  98. if(!err && maxDepth >= 0)
  99. {
  100. err = (*it)->visitChildrenMaxDepth(maxDepth, vis);
  101. }
  102. }
  103. return err;
  104. }
  105. } // end namespace anki