Hierarchy.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #pragma once
  6. #include <AnKi/Util/Assert.h>
  7. #include <AnKi/Util/List.h>
  8. #include <AnKi/Util/StdTypes.h>
  9. #include <algorithm>
  10. namespace anki {
  11. /// @addtogroup util_patterns
  12. /// @{
  13. /// A hierarchical object
  14. template<typename T, typename TMemoryPool>
  15. class Hierarchy
  16. {
  17. public:
  18. using Value = T;
  19. using Container = List<Value*, TMemoryPool>;
  20. Hierarchy(const TMemoryPool& pool = TMemoryPool())
  21. : m_children(pool)
  22. {
  23. }
  24. Hierarchy(const Hierarchy&) = delete; // Non-copyable
  25. ~Hierarchy()
  26. {
  27. destroy();
  28. }
  29. Hierarchy& operator=(const Hierarchy&) = delete; // Non-copyable
  30. void destroy();
  31. const Value* getParent() const
  32. {
  33. return m_parent;
  34. }
  35. Value* getParent()
  36. {
  37. return m_parent;
  38. }
  39. Container& getChildren()
  40. {
  41. return m_children;
  42. }
  43. const Container& getChildren() const
  44. {
  45. return m_children;
  46. }
  47. Value& getChild(U32 i)
  48. {
  49. return *(*(m_children.getBegin() + i));
  50. }
  51. const Value& getChild(U32 i) const
  52. {
  53. return *(*(m_children.getBegin() + i));
  54. }
  55. Bool hasChildren() const
  56. {
  57. return !m_children.isEmpty();
  58. }
  59. /// Add a new child.
  60. void addChild(Value* child);
  61. /// Remove a child.
  62. void removeChild(Value* child);
  63. void setParent(Value* parent)
  64. {
  65. ANKI_ASSERT(parent);
  66. parent->addChild(static_cast<Value*>(this));
  67. }
  68. void removeParent()
  69. {
  70. if(m_parent)
  71. {
  72. m_parent->removeChild(this);
  73. }
  74. }
  75. /// Visit the children and the children's children. Use it with lambda
  76. template<typename TVisitorFunc>
  77. FunctorContinue visitChildren(TVisitorFunc vis);
  78. /// Visit this object and move to the children. Use it with lambda
  79. template<typename TVisitorFunc>
  80. void visitThisAndChildren(TVisitorFunc vis);
  81. /// Visit the whole tree. Use it with lambda
  82. template<typename TVisitorFunc>
  83. void visitTree(TVisitorFunc vis);
  84. /// Visit the children and limit the depth. Use it with lambda.
  85. template<typename TVisitorFunc>
  86. FunctorContinue visitChildrenMaxDepth(I maxDepth, TVisitorFunc vis);
  87. private:
  88. Value* m_parent = nullptr; ///< May be nullptr
  89. Container m_children;
  90. /// Cast the Hierarchy to the given type
  91. Value* getSelf()
  92. {
  93. return static_cast<Value*>(this);
  94. }
  95. /// Find the child
  96. typename Container::Iterator findChild(Value* child)
  97. {
  98. typename Container::Iterator it = m_children.find(child);
  99. return it;
  100. }
  101. };
  102. /// @}
  103. } // end namespace anki
  104. #include <AnKi/Util/Hierarchy.inl.h>