ilist_node.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //==-- llvm/ADT/ilist_node.h - Intrusive Linked List Helper ------*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the ilist_node class template, which is a convenient
  11. // base class for creating classes that can be used with ilists.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ADT_ILIST_NODE_H
  15. #define LLVM_ADT_ILIST_NODE_H
  16. namespace llvm {
  17. template<typename NodeTy>
  18. struct ilist_traits;
  19. /// ilist_half_node - Base class that provides prev services for sentinels.
  20. ///
  21. template<typename NodeTy>
  22. class ilist_half_node {
  23. friend struct ilist_traits<NodeTy>;
  24. NodeTy *Prev;
  25. protected:
  26. NodeTy *getPrev() { return Prev; }
  27. const NodeTy *getPrev() const { return Prev; }
  28. void setPrev(NodeTy *P) { Prev = P; }
  29. ilist_half_node() : Prev(nullptr) {}
  30. };
  31. template<typename NodeTy>
  32. struct ilist_nextprev_traits;
  33. /// ilist_node - Base class that provides next/prev services for nodes
  34. /// that use ilist_nextprev_traits or ilist_default_traits.
  35. ///
  36. template<typename NodeTy>
  37. class ilist_node : private ilist_half_node<NodeTy> {
  38. friend struct ilist_nextprev_traits<NodeTy>;
  39. friend struct ilist_traits<NodeTy>;
  40. NodeTy *Next;
  41. NodeTy *getNext() { return Next; }
  42. const NodeTy *getNext() const { return Next; }
  43. void setNext(NodeTy *N) { Next = N; }
  44. protected:
  45. ilist_node() : Next(nullptr) {}
  46. public:
  47. /// @name Adjacent Node Accessors
  48. /// @{
  49. /// \brief Get the previous node, or 0 for the list head.
  50. NodeTy *getPrevNode() {
  51. NodeTy *Prev = this->getPrev();
  52. // Check for sentinel.
  53. if (!Prev->getNext())
  54. return nullptr;
  55. return Prev;
  56. }
  57. /// \brief Get the previous node, or 0 for the list head.
  58. const NodeTy *getPrevNode() const {
  59. const NodeTy *Prev = this->getPrev();
  60. // Check for sentinel.
  61. if (!Prev->getNext())
  62. return nullptr;
  63. return Prev;
  64. }
  65. /// \brief Get the next node, or 0 for the list tail.
  66. NodeTy *getNextNode() {
  67. NodeTy *Next = getNext();
  68. // Check for sentinel.
  69. if (!Next->getNext())
  70. return nullptr;
  71. return Next;
  72. }
  73. /// \brief Get the next node, or 0 for the list tail.
  74. const NodeTy *getNextNode() const {
  75. const NodeTy *Next = getNext();
  76. // Check for sentinel.
  77. if (!Next->getNext())
  78. return nullptr;
  79. return Next;
  80. }
  81. /// @}
  82. };
  83. } // End llvm namespace
  84. #endif