TargetList.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #if !defined(__INCLUDE_TargetListh_INCLUDE__)
00022 #define __INCLUDE_TargetListh_INCLUDE__
00023 
00024 #include <memory.h>
00025 
00026 class TargetList
00027 {
00028 public:
00029         TargetList() : objects_(0)
00030         {
00031                 clear();
00032         }
00033         ~TargetList()
00034         {
00035                 clear();
00036         }
00037 
00038         void clear()
00039         {
00040                 delete [] objects_;
00041                 objects_ = 0;
00042                 lastObject_ = 0;
00043                 capacity_ = 0;
00044                 count_ = 0;
00045         }
00046 
00047         bool empty() 
00048         {
00049                 return (count_ == 0);
00050         }
00051 
00052         void reset() 
00053         {
00054                 count_ = 0;
00055                 lastObject_ = objects_;
00056         }
00057 
00058         void setCapacity(int capacity)
00059         {
00060                 clear();
00061                 capacity_ = capacity;
00062                 objects_ = lastObject_ = new void*[capacity_];
00063                 reset();
00064         }
00065 
00066         void **add(void *obj)
00067         {
00068                 if (count_ == capacity_)
00069                 {
00070                         if (capacity_ == 0) capacity_ = 128;
00071                         else capacity_ *= 2;
00072 
00073                         void **newObjects = new void*[capacity_];
00074                         memset(newObjects, 0, sizeof(void*) * capacity_);
00075                         if (objects_)
00076                         {
00077                                 memcpy(newObjects, objects_, sizeof(void *) * count_);
00078                                 delete [] objects_;
00079                         }
00080                         objects_ = newObjects;
00081                         lastObject_ = &newObjects[count_];
00082                 }
00083 
00084                 *lastObject_ = obj;
00085                 count_++;
00086                 lastObject_++;
00087 
00088                 return (lastObject_ - 1);
00089         }
00090 
00091         int getObjectCount() { return count_; }
00092         void **getObjects() { return objects_; }
00093 
00094 private:
00095         int count_, capacity_;
00096         void **objects_, **lastObject_;
00097 };
00098 
00099 class TargetListIterator
00100 {
00101 public:
00102         TargetListIterator(): 
00103           list_(0), i_(0), current_(0)
00104         {
00105 
00106         }
00107 
00108         TargetListIterator(TargetList &list)
00109         {
00110                 init(list);
00111         }
00112 
00113         void init(TargetList &list)
00114         {
00115                 list_ = &list;
00116                 current_ = list_->getObjects();
00117                 i_ = 0;
00118         }
00119 
00120         void *getNext()
00121         {
00122                 if (i_ == list_->getObjectCount()) return 0;
00123                 void *result = *current_;
00124                 i_++;
00125                 current_++;
00126                 return result;
00127         }
00128 
00129 private:
00130         int i_;
00131         void **current_;
00132         TargetList *list_;
00133 };
00134 
00135 #endif // __INCLUDE_TargetListh_INCLUDE__

Generated on Mon Feb 16 15:14:47 2009 for Scorched3D by  doxygen 1.5.3