Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
Iterator.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__ITERATOR_H
13#define TT_INCLUDE__ITERATOR_H
14
15
16
17#include "SList.h"
18#include "engine_vector.h"
19
20#define TT_FOREACH(iterator, collection) for (Iterator<decltype(collection)&> iterator(collection); iterator; ++iterator)
21#define TT_CONST_FOREACH(iterator, collection) for (Iterator<const decltype(collection)&> iterator(collection); iterator; ++iterator)
22
23
24
25template<class T> class Iterator;
26
27
28
29template<class T> class Iterator<SList<T>&>
30{
31
32 SLNode<T>* node;
33
34public:
35
36 inline Iterator(SList<T>& list) { node = list.Head(); }
37 inline operator bool() { return node != NULL; }
38 inline operator T*() { return node->Data(); }
39 inline T* operator ->() { return (T*)*this; }
40 inline Iterator& operator ++() { node = node->Next(); return *this; }
41
42};
43
44
45
46template<class T> class Iterator<const SList<T>&>
47{
48
49 const SLNode<T>* node;
50
51public:
52
53 inline Iterator(const SList<T>& list) { node = list.Head(); }
54 inline operator bool() { return node != NULL; }
55 inline operator const T*() { return node->Data(); }
56 inline const T* operator ->() { return (T*)*this; }
57 inline Iterator& operator ++() { node = node->Next(); return *this; }
58
59};
60
61
62
63template<class T> class Iterator<VectorClass<T>&>
64{
65
66 T* current;
67 T* end;
68
69public:
70
71 inline Iterator(VectorClass<T>& vector) { current = &vector[0]; end = &vector[vector.Length()]; }
72 inline operator bool() { return current != end; }
73 inline operator T*() { return current; }
74 inline T* operator ->() { return (T*)*this; }
75 inline Iterator& operator ++() { ++current; return *this; }
76
77};
78
79
80
81template<class T> class Iterator<const VectorClass<T>&>
82{
83
84 const T* current;
85 const T* end;
86
87public:
88
89 inline Iterator(const VectorClass<T>& vector) { current = &vector[0]; end = &vector[vector.Length()]; }
90 inline operator bool() { return current != end; }
91 inline operator const T*() { return current; }
92 inline const T* operator ->() { return (const T*)*this; }
93 inline Iterator& operator ++() { ++current; return *this; }
94
95};
96
97
98
99template<class T> class Iterator<DynamicVectorClass<T>&>
100{
101
102 T* current;
103 T* end;
104
105public:
106
107 inline Iterator(DynamicVectorClass<T>& vector) { current = &vector[0]; end = &vector[vector.Count()]; }
108 inline operator bool() { return current != end; }
109 inline operator T*() { return current; }
110 inline T* operator ->() { return (T*)*this; }
111 inline Iterator& operator ++() { ++current; return *this; }
112
113};
114
115
116
117template<class T> class Iterator<const DynamicVectorClass<T>&>
118{
119
120 const T* current;
121 const T* end;
122
123public:
124
125 inline Iterator(const DynamicVectorClass<T>& vector) { current = &vector[0]; end = &vector[vector.Count()]; }
126 inline operator bool() { return current != end; }
127 inline operator const T*() { return current; }
128 inline const T* operator ->() { return (const T*)*this; }
129 inline Iterator& operator ++() { ++current; return *this; }
130
131};
132
133
134
135template<class T> class Iterator<SimpleVecClass<T>&>
136{
137
138 T* current;
139 T* end;
140
141public:
142
143 inline Iterator(SimpleVecClass<T>& vector) { current = &vector[0]; end = &vector[vector.Length()]; }
144 inline operator bool() { return current != end; }
145 inline operator T*() { return current; }
146 inline T* operator ->() { return (T*)*this; }
147 inline Iterator& operator ++() { ++current; return *this; }
148
149};
150
151
152
153template<class T> class Iterator<const SimpleVecClass<T>&>
154{
155
156 const T* current;
157 const T* end;
158
159public:
160
161 inline Iterator(const SimpleVecClass<T>& vector) { current = &vector[0]; end = &vector[vector.Length()]; }
162 inline operator bool() { return current != end; }
163 inline operator const T*() { return current; }
164 inline const T* operator ->() { return (const T*)*this; }
165 inline Iterator& operator ++() { ++current; return *this; }
166
167};
168
169
170
171template<class T> class Iterator<SimpleDynVecClass<T>&>
172{
173
174 T* current;
175 T* end;
176
177public:
178
179 inline Iterator(SimpleDynVecClass<T>& vector) { current = &vector[0]; end = &vector[vector.Count()]; }
180 inline operator bool() { return current != end; }
181 inline operator T*() { return current; }
182 inline T* operator ->() { return (T*)*this; }
183 inline Iterator& operator ++() { ++current; return *this; }
184
185};
186
187
188
189template<class T> class Iterator<const SimpleDynVecClass<T>&>
190{
191
192 const T* current;
193 const T* end;
194
195public:
196
197 inline Iterator(const SimpleDynVecClass<T>& vector) { current = &vector[0]; end = &vector[vector.Count()]; }
198 inline operator bool() { return current != end; }
199 inline operator const T*() { return current; }
200 inline const T* operator ->() { return (const T*)*this; }
201 inline Iterator& operator ++() { ++current; return *this; }
202
203};
204
205
206#endif