1 //==-- llvm/ADT/ilist_node.h - Intrusive Linked List Helper ------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_ILIST_NODE_H
16 #define LLVM_ADT_ILIST_NODE_H
20 template<typename NodeTy>
22 template <typename NodeTy> struct ilist_embedded_sentinel_traits;
23 template <typename NodeTy> struct ilist_half_embedded_sentinel_traits;
25 /// ilist_half_node - Base class that provides prev services for sentinels.
27 template<typename NodeTy>
28 class ilist_half_node {
29 friend struct ilist_traits<NodeTy>;
30 friend struct ilist_half_embedded_sentinel_traits<NodeTy>;
33 NodeTy *getPrev() { return Prev; }
34 const NodeTy *getPrev() const { return Prev; }
35 void setPrev(NodeTy *P) { Prev = P; }
36 ilist_half_node() : Prev(nullptr) {}
39 template<typename NodeTy>
40 struct ilist_nextprev_traits;
42 template <typename NodeTy> class ilist_iterator;
44 /// ilist_node - Base class that provides next/prev services for nodes
45 /// that use ilist_nextprev_traits or ilist_default_traits.
47 template<typename NodeTy>
48 class ilist_node : private ilist_half_node<NodeTy> {
49 friend struct ilist_nextprev_traits<NodeTy>;
50 friend struct ilist_traits<NodeTy>;
51 friend struct ilist_half_embedded_sentinel_traits<NodeTy>;
52 friend struct ilist_embedded_sentinel_traits<NodeTy>;
54 NodeTy *getNext() { return Next; }
55 const NodeTy *getNext() const { return Next; }
56 void setNext(NodeTy *N) { Next = N; }
58 ilist_node() : Next(nullptr) {}
61 ilist_iterator<NodeTy> getIterator() {
62 // FIXME: Stop downcasting to create the iterator (potential UB).
63 return ilist_iterator<NodeTy>(static_cast<NodeTy *>(this));
65 ilist_iterator<const NodeTy> getIterator() const {
66 // FIXME: Stop downcasting to create the iterator (potential UB).
67 return ilist_iterator<const NodeTy>(static_cast<const NodeTy *>(this));
70 /// @name Adjacent Node Accessors
73 /// \brief Get the previous node, or 0 for the list head.
74 NodeTy *getPrevNode() {
75 NodeTy *Prev = this->getPrev();
77 // Check for sentinel.
84 /// \brief Get the previous node, or 0 for the list head.
85 const NodeTy *getPrevNode() const {
86 const NodeTy *Prev = this->getPrev();
88 // Check for sentinel.
95 /// \brief Get the next node, or 0 for the list tail.
96 NodeTy *getNextNode() {
97 NodeTy *Next = getNext();
99 // Check for sentinel.
100 if (!Next->getNext())
106 /// \brief Get the next node, or 0 for the list tail.
107 const NodeTy *getNextNode() const {
108 const NodeTy *Next = getNext();
110 // Check for sentinel.
111 if (!Next->getNext())
120 } // End llvm namespace