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/util.h"
26
27namespace Common {
28
29template<typename T> class List;
30
31
32namespace 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 Node(T &&x) : _data(Common::move(x)) {}
47 template<class... TArgs>
48 Node(TArgs&&... args) : _data(Common::forward<TArgs>(args)...) {}
49 };
50
51 template<typename T> struct ConstIterator;
52
53 template<typename T>
54 struct Iterator {
55 typedef Iterator<T> Self;
56 typedef Node<T> * NodePtr;
57 typedef T & ValueRef;
58 typedef T * ValuePtr;
59 typedef T ValueType;
60
61 NodeBase *_node;
62
63 Iterator() : _node(nullptr) {}
64 explicit Iterator(NodeBase *node) : _node(node) {}
65
66 // Prefix inc
67 Self &operator++() {
68 if (_node)
69 _node = _node->_next;
70 return *this;
71 }
72 // Postfix inc
73 Self operator++(int) {
74 Self tmp(_node);
75 ++(*this);
76 return tmp;
77 }
78 // Prefix dec
79 Self &operator--() {
80 if (_node)
81 _node = _node->_prev;
82 return *this;
83 }
84 // Postfix dec
85 Self operator--(int) {
86 Self tmp(_node);
87 --(*this);
88 return tmp;
89 }
90 ValueRef operator*() const {
91 assert(_node);
92 return static_cast<NodePtr>(_node)->_data;
93 }
94 ValuePtr operator->() const {
95 return &(operator*());
96 }
97
98 bool operator==(const Self &x) const {
99 return _node == x._node;
100 }
101
102 bool operator!=(const Self &x) const {
103 return _node != x._node;
104 }
105 };
106
107 template<typename T>
109 typedef ConstIterator<T> Self;
110 typedef const Node<T> * NodePtr;
111 typedef const T & ValueRef;
112 typedef const T * ValuePtr;
113
114 const NodeBase *_node;
115
116 ConstIterator() : _node(nullptr) {}
117 explicit ConstIterator(const NodeBase *node) : _node(node) {}
118 ConstIterator(const Iterator<T> &x) : _node(x._node) {}
119
120 // Prefix inc
121 Self &operator++() {
122 if (_node)
123 _node = _node->_next;
124 return *this;
125 }
126 // Postfix inc
127 Self operator++(int) {
128 Self tmp(_node);
129 ++(*this);
130 return tmp;
131 }
132 // Prefix dec
133 Self &operator--() {
134 if (_node)
135 _node = _node->_prev;
136 return *this;
137 }
138 // Postfix dec
139 Self operator--(int) {
140 Self tmp(_node);
141 --(*this);
142 return tmp;
143 }
144 ValueRef operator*() const {
145 assert(_node);
146 return static_cast<NodePtr>(_node)->_data;
147 }
148 ValuePtr operator->() const {
149 return &(operator*());
150 }
151
152 bool operator==(const Self &x) const {
153 return _node == x._node;
154 }
155
156 bool operator!=(const Self &x) const {
157 return _node != x._node;
158 }
159 };
160
161
162 template<typename T>
163 bool operator==(const Iterator<T>& a, const ConstIterator<T>& b) {
164 return a._node == b._node;
165 }
166
167 template<typename T>
168 bool operator!=(const Iterator<T>& a, const ConstIterator<T>& b) {
169 return a._node != b._node;
170 }
171}
172
173
174} // End of namespace Common
175
176#endif
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Definition: algorithm.h:29
Definition: list_intern.h:108
Definition: list_intern.h:54
Definition: list_intern.h:33
Definition: list_intern.h:42