objiter.h

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2000  Jeffrey S. Freedman
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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  *  Want to make all object iterators modification-safe.  For now, just
00035  *  trying to detect modification during iteration.
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  *  Iterate through list of objects.
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;    // Next to return.
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  *  Iterate through a chunk's nonflat objects.
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  *  Iterate through a chunk's flat objects.
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;    // Next to return.
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  *  Iterate backwards through list of objects.
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;    // Return prev. to this.
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  *  Iterate through a list of objects (recursively).
00163  */
00164 template<class D> class D_Recursive_object_iterator
00165   {
00166           // Child we're going through, or 0.
00167   D_Recursive_object_iterator<D> *child;
00168   D elems;      // Goes through our elements.
00169 public:
00170   D_Recursive_object_iterator(Object_list& objs)
00171     : elems(objs), child(0)
00172     {  }
00173           // Start at given object.
00174   D_Recursive_object_iterator(Game_object *obj);
00175   Game_object *get_next();  // Get next, going into containers.
00176   };
00177 
00178 /*
00179  *  Iterate forwards/backwards through a list of objects (recursively).
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

Generated on Mon Jul 9 14:42:49 2007 for ExultEngine by  doxygen 1.5.1