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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_LIST_INTERN_H
24 #define COMMON_LIST_INTERN_H
25 
26 #include "common/scummsys.h"
27 
28 namespace Common {
29 
30 template<typename T> class List;
31 
32 
33 namespace ListInternal {
34 	struct NodeBase {
35 		NodeBase *_prev;
36 		NodeBase *_next;
37 	};
38 
39 	template<typename T>
40 	struct Node : public NodeBase {
41 		T _data;
42 
NodeNode43 		Node(const T &x) : _data(x) {}
44 	};
45 
46 	template<typename T> struct ConstIterator;
47 
48 	template<typename T>
49 	struct Iterator {
50 		typedef Iterator<T>	Self;
51 		typedef Node<T> *	NodePtr;
52 		typedef T &			ValueRef;
53 		typedef T *			ValuePtr;
54 		typedef T			ValueType;
55 
56 		NodeBase *_node;
57 
IteratorIterator58 		Iterator() : _node(nullptr) {}
IteratorIterator59 		explicit Iterator(NodeBase *node) : _node(node) {}
60 
61 		// Prefix inc
62 		Self &operator++() {
63 			if (_node)
64 				_node = _node->_next;
65 			return *this;
66 		}
67 		// Postfix inc
68 		Self operator++(int) {
69 			Self tmp(_node);
70 			++(*this);
71 			return tmp;
72 		}
73 		// Prefix dec
74 		Self &operator--() {
75 			if (_node)
76 				_node = _node->_prev;
77 			return *this;
78 		}
79 		// Postfix dec
80 		Self operator--(int) {
81 			Self tmp(_node);
82 			--(*this);
83 			return tmp;
84 		}
85 		ValueRef operator*() const {
86 			assert(_node);
87 			return static_cast<NodePtr>(_node)->_data;
88 		}
89 		ValuePtr operator->() const {
90 			return &(operator*());
91 		}
92 
93 		bool operator==(const Self &x) const {
94 			return _node == x._node;
95 		}
96 
97 		bool operator!=(const Self &x) const {
98 			return _node != x._node;
99 		}
100 	};
101 
102 	template<typename T>
103 	struct ConstIterator {
104 		typedef ConstIterator<T>	Self;
105 		typedef const Node<T> *	NodePtr;
106 		typedef const T &		ValueRef;
107 		typedef const T *		ValuePtr;
108 
109 		const NodeBase *_node;
110 
ConstIteratorConstIterator111 		ConstIterator() : _node(nullptr) {}
ConstIteratorConstIterator112 		explicit ConstIterator(const NodeBase *node) : _node(node) {}
ConstIteratorConstIterator113 		ConstIterator(const Iterator<T> &x) : _node(x._node) {}
114 
115 		// Prefix inc
116 		Self &operator++() {
117 			if (_node)
118 				_node = _node->_next;
119 			return *this;
120 		}
121 		// Postfix inc
122 		Self operator++(int) {
123 			Self tmp(_node);
124 			++(*this);
125 			return tmp;
126 		}
127 		// Prefix dec
128 		Self &operator--() {
129 			if (_node)
130 				_node = _node->_prev;
131 			return *this;
132 		}
133 		// Postfix dec
134 		Self operator--(int) {
135 			Self tmp(_node);
136 			--(*this);
137 			return tmp;
138 		}
139 		ValueRef operator*() const {
140 			assert(_node);
141 			return static_cast<NodePtr>(_node)->_data;
142 		}
143 		ValuePtr operator->() const {
144 			return &(operator*());
145 		}
146 
147 		bool operator==(const Self &x) const {
148 			return _node == x._node;
149 		}
150 
151 		bool operator!=(const Self &x) const {
152 			return _node != x._node;
153 		}
154 	};
155 
156 
157 	template<typename T>
158 	bool operator==(const Iterator<T>& a, const ConstIterator<T>& b) {
159 		return a._node == b._node;
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 
168 
169 } // End of namespace Common
170 
171 #endif
172