CrystalSpace

Public API Reference

csutil/parray.h

Go to the documentation of this file.
00001 /*
00002   Crystal Space Pointer Array
00003   Copyright (C) 2003 by Jorrit Tyberghein
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Library General Public
00007   License as published by the Free Software Foundation; either
00008   version 2 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Library General Public License for more details.
00014 
00015   You should have received a copy of the GNU Library General Public
00016   License along with this library; if not, write to the Free
00017   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_PTRARR_H__
00021 #define __CS_PTRARR_H__
00022 
00027 //-----------------------------------------------------------------------------
00028 // Note *1*: The explicit "this->" is needed by modern compilers (such as gcc
00029 // 3.4.x) which distinguish between dependent and non-dependent names in
00030 // templates.  See: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
00031 //-----------------------------------------------------------------------------
00032 
00033 #include "csextern.h"
00034 #include "csutil/array.h"
00035 
00036 template <class T>
00037 class csPDelArrayElementHandler : public csArrayElementHandler<T>
00038 {
00039 public:
00040   static void Construct (T* address, T const& src)
00041   {
00042     *address = src;
00043   }
00044 
00045   static void Destroy (T* address)
00046   {
00047     delete *address;
00048   }
00049 
00050   static void InitRegion (T* address, size_t count)
00051   {
00052     memset (address, 0, count*sizeof (T));
00053   }
00054 };
00055 
00063 template <class T>
00064 class csPDelArray : public csArray<T*, csPDelArrayElementHandler<T*> >
00065 {
00066   typedef csArray<T*, csPDelArrayElementHandler<T*> > superclass;
00067 
00068 private:
00069   csPDelArray (const csPDelArray&);            // Illegal; unimplemented.
00070   csPDelArray& operator= (const csPDelArray&); // Illegal; unimplemented.
00071 
00072 public:
00077   csPDelArray (int ilimit = 0, int ithreshold = 0) :
00078     csArray<T*, csPDelArrayElementHandler<T*> > (ilimit, ithreshold) {}
00079 
00085   T* GetAndClear (size_t n)
00086   {
00087     T* ret = this->Get (n); // see *1*
00088     this->InitRegion (n, 1);
00089     return ret;
00090   }
00091 
00097   T* Extract (size_t n)
00098   {
00099     T* ret = GetAndClear (n);
00100     this->DeleteIndex (n); // see *1*
00101     return ret;
00102   }
00103 
00105   T* Pop ()
00106   {
00107     CS_ASSERT (this->GetSize () > 0);
00108     T* ret = GetAndClear (this->GetSize () - 1); // see *1*
00109     Truncate (this->GetSize () - 1);
00110     return ret;
00111   }
00112 
00120   void SetSize (size_t n)
00121   {
00122     superclass::SetSize (0);
00123   }
00124 
00129   void SetSize (size_t n, T const &what)
00130   {
00131     if (n <= this->GetSize ()) // see *1*
00132     {
00133       this->Truncate (n);
00134     }
00135     else
00136     {
00137       size_t old_len = this->GetSize (); // see *1*
00138       superclass::SetSize (n);
00139       for (size_t i = old_len ; i < n ; i++) this->Get(i) = new T (what);
00140     }
00141   }
00142 };
00143 
00144 #endif // __CS_PTRARR_H__
00145 

Generated for Crystal Space 1.2.1 by doxygen 1.5.3