Speech bubbles can point down right.
[scummvm-innocent.git] / common / list_intern.h
blob11d03fd6ff8bde1589c475ef3f149de5f3ade670
1 /* ScummVM - Graphic Adventure Engine
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.
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.
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.
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.
21 * $URL$
22 * $Id$
25 #ifndef COMMON_LIST_INTERN_H
26 #define COMMON_LIST_INTERN_H
28 #include "common/scummsys.h"
30 namespace Common {
32 template<typename T> class List;
35 namespace ListInternal {
36 struct NodeBase {
37 NodeBase *_prev;
38 NodeBase *_next;
41 template <typename T>
42 struct Node : public NodeBase {
43 T _data;
45 Node(const T &x) : _data(x) {}
48 template<typename T> struct ConstIterator;
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;
57 NodeBase *_node;
59 Iterator() : _node(0) {}
60 explicit Iterator(NodeBase *node) : _node(node) {}
62 // Prefix inc
63 Self &operator++() {
64 if (_node)
65 _node = _node->_next;
66 return *this;
68 // Postfix inc
69 Self operator++(int) {
70 Self tmp(_node);
71 ++(*this);
72 return tmp;
74 // Prefix dec
75 Self &operator--() {
76 if (_node)
77 _node = _node->_prev;
78 return *this;
80 // Postfix dec
81 Self operator--(int) {
82 Self tmp(_node);
83 --(*this);
84 return tmp;
86 ValueRef operator*() const {
87 assert(_node);
88 return static_cast<NodePtr>(_node)->_data;
90 ValuePtr operator->() const {
91 return &(operator*());
94 bool operator==(const Self &x) const {
95 return _node == x._node;
98 bool operator!=(const Self &x) const {
99 return _node != x._node;
103 template<typename T>
104 struct ConstIterator {
105 typedef ConstIterator<T> Self;
106 typedef const Node<T> * NodePtr;
107 typedef const T & ValueRef;
108 typedef const T * ValuePtr;
110 const NodeBase *_node;
112 ConstIterator() : _node(0) {}
113 explicit ConstIterator(const NodeBase *node) : _node(node) {}
114 ConstIterator(const Iterator<T> &x) : _node(x._node) {}
116 // Prefix inc
117 Self &operator++() {
118 if (_node)
119 _node = _node->_next;
120 return *this;
122 // Postfix inc
123 Self operator++(int) {
124 Self tmp(_node);
125 ++(*this);
126 return tmp;
128 // Prefix dec
129 Self &operator--() {
130 if (_node)
131 _node = _node->_prev;
132 return *this;
134 // Postfix dec
135 Self operator--(int) {
136 Self tmp(_node);
137 --(*this);
138 return tmp;
140 ValueRef operator*() const {
141 assert(_node);
142 return static_cast<NodePtr>(_node)->_data;
144 ValuePtr operator->() const {
145 return &(operator*());
148 bool operator==(const Self &x) const {
149 return _node == x._node;
152 bool operator!=(const Self &x) const {
153 return _node != x._node;
158 template<typename T>
159 bool operator==(const Iterator<T>& a, const ConstIterator<T>& b) {
160 return a._node == b._node;
163 template<typename T>
164 bool operator!=(const Iterator<T>& a, const ConstIterator<T>& b) {
165 return a._node != b._node;
170 } // End of namespace Common
172 #endif