StdVectorPtr.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(AFX_StdVector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00022 #define AFX_StdVector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_
00023 
00024 #include <vector>
00025 #include <stdexcept>
00026 
00027 // same as std::vector regarding the interface, but handles pointers.
00028 template <class T>
00029 class StdVectorPtr
00030 {
00031  public:
00032         StdVectorPtr(size_t capacity = 0) : data(capacity) {}
00033         ~StdVectorPtr() { clear(); }
00034 
00035         void resize(size_t newsize) 
00036         {
00037                 if (newsize < size()) 
00038                 {
00039                         for (size_t i = newsize; i < size(); ++i) 
00040                         {
00041                                 delete data[i];
00042                                 // set to zero, because if resize throws an exception,
00043                                 // objects could get destructed twice
00044                                 data[i] = 0;
00045                         }
00046                 }
00047                 data.resize(newsize);
00048         }
00049         size_t size() const { return data.size(); }
00050         size_t capacity() const { return data.capacity(); }
00051         void clear() 
00052         {
00053                 for (size_t i = 0; i < size(); i++) 
00054                 {
00055                         delete data[i];
00056                         // set to zero, because if clear throws an exception,
00057                         // objects could get destructed twice
00058                         data[i] = 0;
00059                 }
00060                 data.clear();
00061         }
00062 
00063         // exception safe, so first create space, then store
00064         void push_back(std::auto_ptr<T> ptr) 
00065         {
00066                 data.push_back(0);
00067                 data.back() = ptr.release();
00068         }
00069 
00070         T* const& operator[](size_t n) const { return data[n]; }
00071         T* const& at(size_t n) const { return data.at(n); }
00072 
00073         void reset(size_t n, T* ptr) { delete data[n]; data[n] = ptr; }
00074         bool empty() const { return data.empty(); }
00075 
00076  protected:
00077         std::vector<T*> data;
00078 
00079  private:
00080         StdVectorPtr(const StdVectorPtr& );
00081         StdVectorPtr& operator= (const StdVectorPtr& );
00082 };
00083 
00084 
00085 #endif // !defined(AFX_StdVector4_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00086 

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