Hierarchy.inl.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2009-present, 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, typename TMemoryPool>
  7. void Hierarchy<T, TMemoryPool>::destroy()
  8. {
  9. if(m_parent != nullptr)
  10. {
  11. m_parent->removeChild(getSelf());
  12. m_parent = nullptr;
  13. }
  14. // Remove all children (fast version)
  15. auto it = m_children.getBegin();
  16. auto end = m_children.getEnd();
  17. for(; it != end; ++it)
  18. {
  19. Value* child = *it;
  20. child->m_parent = nullptr;
  21. }
  22. m_children.destroy();
  23. }
  24. template<typename T, typename TMemoryPool>
  25. void Hierarchy<T, TMemoryPool>::addChild(Value* child)
  26. {
  27. ANKI_ASSERT(child != nullptr && "Null arg");
  28. ANKI_ASSERT(child != getSelf() && "Cannot put itself");
  29. ANKI_ASSERT(child->m_parent == nullptr && "Child already has parent");
  30. ANKI_ASSERT(child->findChild(getSelf()) == child->m_children.getEnd() && "Cyclic add");
  31. ANKI_ASSERT(findChild(child) == m_children.getEnd() && "Already has that child");
  32. child->m_parent = getSelf();
  33. m_children.emplaceBack(child);
  34. }
  35. template<typename T, typename TMemoryPool>
  36. void Hierarchy<T, TMemoryPool>::removeChild(Value* child)
  37. {
  38. ANKI_ASSERT(child != nullptr && "Null arg");
  39. ANKI_ASSERT(child->m_parent == getSelf() && "Child has other parent");
  40. typename Container::Iterator it = findChild(child);
  41. ANKI_ASSERT(it != m_children.getEnd() && "Child not found");
  42. m_children.erase(it);
  43. child->m_parent = nullptr;
  44. }
  45. template<typename T, typename TMemoryPool>
  46. template<typename TVisitorFunc>
  47. FunctorContinue Hierarchy<T, TMemoryPool>::visitChildren(TVisitorFunc vis)
  48. {
  49. auto it = m_children.getBegin();
  50. FunctorContinue cont = FunctorContinue::kContinue;
  51. for(; it != m_children.getEnd() && cont == FunctorContinue::kContinue; it++)
  52. {
  53. cont = vis(*(*it));
  54. if(cont == FunctorContinue::kContinue)
  55. {
  56. cont = (*it)->visitChildren(vis);
  57. }
  58. }
  59. return cont;
  60. }
  61. template<typename T, typename TMemoryPool>
  62. template<typename TVisitorFunc>
  63. void Hierarchy<T, TMemoryPool>::visitThisAndChildren(TVisitorFunc vis)
  64. {
  65. const FunctorContinue cont = vis(*getSelf());
  66. if(cont == FunctorContinue::kContinue)
  67. {
  68. visitChildren(vis);
  69. }
  70. }
  71. template<typename T, typename TMemoryPool>
  72. template<typename TVisitorFunc>
  73. void Hierarchy<T, TMemoryPool>::visitTree(TVisitorFunc vis)
  74. {
  75. // Move to root
  76. Value* root = getSelf();
  77. while(root->m_parent != nullptr)
  78. {
  79. root = root->m_parent;
  80. }
  81. root->visitThisAndChildren(vis);
  82. }
  83. template<typename T, typename TMemoryPool>
  84. template<typename TVisitorFunc>
  85. FunctorContinue Hierarchy<T, TMemoryPool>::visitChildrenMaxDepth(I maxDepth, TVisitorFunc vis)
  86. {
  87. ANKI_ASSERT(maxDepth >= 0);
  88. --maxDepth;
  89. FunctorContinue cont = FunctorContinue::kContinue;
  90. auto it = m_children.getBegin();
  91. for(; it != m_children.getEnd() && cont == FunctorContinue::kContinue; ++it)
  92. {
  93. cont = vis(*(*it));
  94. if(cont == FunctorContinue::kContinue && maxDepth >= 0)
  95. {
  96. cont = (*it)->visitChildrenMaxDepth(maxDepth, vis);
  97. }
  98. }
  99. return cont;
  100. }
  101. } // end namespace anki