00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef INCL_OBJITER
00026 #define INCL_OBJITER 1
00027
00028 #include "objlist.h"
00029
00030 class Map_chunk;
00031 class Game_object;
00032
00033
00034
00035
00036
00037 template<class T>
00038 class T_Safe_object_iterator
00039 {
00040 T_Object_list<T>& list;
00041 public:
00042 T_Safe_object_iterator(T_Object_list<T>& l) : list(l)
00043 { list.add_iterator(); }
00044 ~T_Safe_object_iterator()
00045 { list.remove_iterator(); }
00046 };
00047
00048
00049
00050
00051 template<class T>
00052 class T_Object_iterator : public T_Safe_object_iterator<T>
00053 {
00054 protected:
00055 T first;
00056 T stop;
00057 T cur;
00058 public:
00059 void reset()
00060 { cur = first; stop = 0; }
00061 T_Object_iterator(T_Object_list<T>& objects)
00062 : T_Safe_object_iterator<T>(objects), first(objects.get_first())
00063 { reset(); }
00064 T get_next()
00065 {
00066 if (cur == stop)
00067 return 0;
00068 T ret = cur;
00069 cur = cur->next;
00070 stop = first;
00071 return ret;
00072 }
00073 };
00074
00075 typedef T_Object_iterator<Game_object *> Object_iterator;
00076
00077
00078
00079
00080 template<class T, class L>
00081 class T_Nonflat_object_iterator : public T_Object_iterator<T>
00082 {
00083 T nonflats;
00084 public:
00085 void reset()
00086 { this->cur = nonflats; this->stop = 0; }
00087 T_Nonflat_object_iterator(L chunk)
00088 : T_Object_iterator<T>(chunk->get_objects()), nonflats(chunk->get_first_nonflat())
00089 { reset(); }
00090 };
00091
00092 typedef T_Nonflat_object_iterator<Game_object *, Map_chunk *> Nonflat_object_iterator;
00093
00094
00095
00096
00097 template<class T, class L>
00098 class T_Flat_object_iterator : public T_Safe_object_iterator<T>
00099 {
00100 T first;
00101 T stop;
00102 T cur;
00103 T stop_at;
00104 public:
00105 void reset()
00106 { cur = first; stop = 0; }
00107 T_Flat_object_iterator(L chunk)
00108 : T_Safe_object_iterator<T>(chunk->get_objects())
00109 {
00110 first = chunk->get_objects().get_first() == chunk->get_first_nonflat() ? 0 :
00111 chunk->get_objects().get_first();
00112 stop_at = chunk->get_first_nonflat() ? chunk->get_first_nonflat()
00113 : chunk->get_objects().get_first();
00114 reset();
00115 }
00116 T get_next()
00117 {
00118 if (cur == stop)
00119 return 0;
00120 T ret = cur;
00121 cur = cur->get_next();
00122 stop = stop_at;
00123 return ret;
00124 }
00125 };
00126
00127 typedef T_Flat_object_iterator<Game_object *, Map_chunk *> Flat_object_iterator;
00128
00129
00130
00131
00132 template<class T, class L>
00133 class T_Object_iterator_backwards : public T_Safe_object_iterator<T>
00134 {
00135 T first;
00136 T stop;
00137 T cur;
00138 public:
00139 void reset()
00140 { cur = first; stop = 0; }
00141 T_Object_iterator_backwards(L chunk)
00142 : T_Safe_object_iterator<T>(chunk->get_objects()),
00143 first(chunk->get_objects().get_first())
00144 { reset(); }
00145 T_Object_iterator_backwards(T_Object_list<T>& objects)
00146 : T_Safe_object_iterator<T>(objects),
00147 first(objects.get_first())
00148 { reset(); }
00149 T get_next()
00150 {
00151 if (cur == stop)
00152 return 0;
00153 cur = cur->prev;
00154 stop = first;
00155 return cur;
00156 }
00157 };
00158
00159 typedef T_Object_iterator_backwards<Game_object *, Map_chunk *> Object_iterator_backwards;
00160
00161
00162
00163
00164 template<class D> class D_Recursive_object_iterator
00165 {
00166
00167 D_Recursive_object_iterator<D> *child;
00168 D elems;
00169 public:
00170 D_Recursive_object_iterator(Object_list& objs)
00171 : elems(objs), child(0)
00172 { }
00173
00174 D_Recursive_object_iterator(Game_object *obj);
00175 Game_object *get_next();
00176 };
00177
00178
00179
00180
00181 typedef D_Recursive_object_iterator<Object_iterator_backwards>
00182 Recursive_object_iterator_backwards;
00183 typedef D_Recursive_object_iterator<Object_iterator>
00184 Recursive_object_iterator;
00185
00186 #endif