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
15#ifndef LLVM_ADT_ILISTNODE_H
16#define LLVM_ADT_ILISTNODE_H
17
18namespace llvm {
19
20template<typename NodeTy>
21struct ilist_traits;
22
23/// ilist_half_node - Base class that provides prev services for sentinels.
24///
25template<typename NodeTy>
26class ilist_half_node {
27  friend struct ilist_traits<NodeTy>;
28  NodeTy *Prev;
29protected:
30  NodeTy *getPrev() { return Prev; }
31  const NodeTy *getPrev() const { return Prev; }
32  void setPrev(NodeTy *P) { Prev = P; }
33  ilist_half_node() : Prev(nullptr) {}
34};
35
36template<typename NodeTy>
37struct ilist_nextprev_traits;
38
39/// ilist_node - Base class that provides next/prev services for nodes
40/// that use ilist_nextprev_traits or ilist_default_traits.
41///
42template<typename NodeTy>
43class ilist_node : private ilist_half_node<NodeTy> {
44  friend struct ilist_nextprev_traits<NodeTy>;
45  friend struct ilist_traits<NodeTy>;
46  NodeTy *Next;
47  NodeTy *getNext() { return Next; }
48  const NodeTy *getNext() const { return Next; }
49  void setNext(NodeTy *N) { Next = N; }
50protected:
51  ilist_node() : Next(nullptr) {}
52
53public:
54  /// @name Adjacent Node Accessors
55  /// @{
56
57  /// \brief Get the previous node, or 0 for the list head.
58  NodeTy *getPrevNode() {
59    NodeTy *Prev = this->getPrev();
60
61    // Check for sentinel.
62    if (!Prev->getNext())
63      return nullptr;
64
65    return Prev;
66  }
67
68  /// \brief Get the previous node, or 0 for the list head.
69  const NodeTy *getPrevNode() const {
70    const NodeTy *Prev = this->getPrev();
71
72    // Check for sentinel.
73    if (!Prev->getNext())
74      return nullptr;
75
76    return Prev;
77  }
78
79  /// \brief Get the next node, or 0 for the list tail.
80  NodeTy *getNextNode() {
81    NodeTy *Next = getNext();
82
83    // Check for sentinel.
84    if (!Next->getNext())
85      return nullptr;
86
87    return Next;
88  }
89
90  /// \brief Get the next node, or 0 for the list tail.
91  const NodeTy *getNextNode() const {
92    const NodeTy *Next = getNext();
93
94    // Check for sentinel.
95    if (!Next->getNext())
96      return nullptr;
97
98    return Next;
99  }
100
101  /// @}
102};
103
104} // End llvm namespace
105
106#endif
107