ScummVM API documentation
list_intern.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_LIST_INTERN_H
23 #define COMMON_LIST_INTERN_H
24 
25 #include "common/scummsys.h"
26 
27 namespace Common {
28 
29 template<typename T> class List;
30 
31 
32 namespace ListInternal {
33  struct NodeBase {
34  NodeBase *_prev = nullptr;
35  NodeBase *_next = nullptr;
36 
37  constexpr NodeBase() = default;
38  constexpr NodeBase(NodeBase *prev, NodeBase *next) : _prev(prev), _next(next) {}
39  };
40 
41  template<typename T>
42  struct Node : public NodeBase {
43  T _data;
44 
45  Node(const T &x) : _data(x) {}
46  };
47 
48  template<typename T> struct ConstIterator;
49 
50  template<typename T>
51  struct Iterator {
52  typedef Iterator<T> Self;
53  typedef Node<T> * NodePtr;
54  typedef T & ValueRef;
55  typedef T * ValuePtr;
56  typedef T ValueType;
57 
58  NodeBase *_node;
59 
60  Iterator() : _node(nullptr) {}
61  explicit Iterator(NodeBase *node) : _node(node) {}
62 
63  // Prefix inc
64  Self &operator++() {
65  if (_node)
66  _node = _node->_next;
67  return *this;
68  }
69  // Postfix inc
70  Self operator++(int) {
71  Self tmp(_node);
72  ++(*this);
73  return tmp;
74  }
75  // Prefix dec
76  Self &operator--() {
77  if (_node)
78  _node = _node->_prev;
79  return *this;
80  }
81  // Postfix dec
82  Self operator--(int) {
83  Self tmp(_node);
84  --(*this);
85  return tmp;
86  }
87  ValueRef operator*() const {
88  assert(_node);
89  return static_cast<NodePtr>(_node)->_data;
90  }
91  ValuePtr operator->() const {
92  return &(operator*());
93  }
94 
95  bool operator==(const Self &x) const {
96  return _node == x._node;
97  }
98 
99  bool operator!=(const Self &x) const {
100  return _node != x._node;
101  }
102  };
103 
104  template<typename T>
105  struct ConstIterator {
106  typedef ConstIterator<T> Self;
107  typedef const Node<T> * NodePtr;
108  typedef const T & ValueRef;
109  typedef const T * ValuePtr;
110 
111  const NodeBase *_node;
112 
113  ConstIterator() : _node(nullptr) {}
114  explicit ConstIterator(const NodeBase *node) : _node(node) {}
115  ConstIterator(const Iterator<T> &x) : _node(x._node) {}
116 
117  // Prefix inc
118  Self &operator++() {
119  if (_node)
120  _node = _node->_next;
121  return *this;
122  }
123  // Postfix inc
124  Self operator++(int) {
125  Self tmp(_node);
126  ++(*this);
127  return tmp;
128  }
129  // Prefix dec
130  Self &operator--() {
131  if (_node)
132  _node = _node->_prev;
133  return *this;
134  }
135  // Postfix dec
136  Self operator--(int) {
137  Self tmp(_node);
138  --(*this);
139  return tmp;
140  }
141  ValueRef operator*() const {
142  assert(_node);
143  return static_cast<NodePtr>(_node)->_data;
144  }
145  ValuePtr operator->() const {
146  return &(operator*());
147  }
148 
149  bool operator==(const Self &x) const {
150  return _node == x._node;
151  }
152 
153  bool operator!=(const Self &x) const {
154  return _node != x._node;
155  }
156  };
157 
158 
159  template<typename T>
160  bool operator==(const Iterator<T>& a, const ConstIterator<T>& b) {
161  return a._node == b._node;
162  }
163 
164  template<typename T>
165  bool operator!=(const Iterator<T>& a, const ConstIterator<T>& b) {
166  return a._node != b._node;
167  }
168 }
169 
170 
171 } // End of namespace Common
172 
173 #endif
Definition: list_intern.h:33
Definition: algorithm.h:29
Definition: list_intern.h:48
Definition: list_intern.h:51
Definition: list_intern.h:42