csutil/parray.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 MemoryAllocator = CS::Container::ArrayAllocDefault,
    00065           class CapacityHandler = csArrayCapacityFixedGrow<16> >
    00066 class csPDelArray :
    00067   public csArray<T*, csPDelArrayElementHandler<T*>, MemoryAllocator,
    00068                  CapacityHandler>
    00069 {
    00070   typedef csArray<T*, csPDelArrayElementHandler<T*>, MemoryAllocator,
    00071     CapacityHandler> superclass;
    00072
    00073 private:
    00074   csPDelArray (const csPDelArray&);            // Illegal; unimplemented.
    00075   csPDelArray& operator= (const csPDelArray&); // Illegal; unimplemented.
    00076
    00077 public:
    00082   csPDelArray (size_t ilimit = 0,
    00083     const CapacityHandler& ch = CapacityHandler()) :
    00084     superclass (ilimit, ch) {}
    00085
    00091   T* GetAndClear (size_t n)
    00092   {
    00093     T* ret = this->Get (n); // see *1*
    00094     this->InitRegion (n, 1);
    00095     return ret;
    00096   }
    00097
    00103   T* Extract (size_t n)
    00104   {
    00105     T* ret = GetAndClear (n);
    00106     this->DeleteIndex (n); // see *1*
    00107     return ret;
    00108   }
    00109
    00111   T* Pop ()
    00112   {
    00113     CS_ASSERT (this->GetSize () > 0);
    00114     T* ret = GetAndClear (this->GetSize () - 1); // see *1*
    00115     Truncate (this->GetSize () - 1);
    00116     return ret;
    00117   }
    00118
    00126   void SetSize (size_t n)
    00127   {
    00128     superclass::SetSize (n, 0);
    00129   }
    00130
    00135   void SetSize (size_t n, T const &what)
    00136   {
    00137     if (n <= this->GetSize ()) // see *1*
    00138     {
    00139       this->Truncate (n);
    00140     }
    00141     else
    00142     {
    00143       size_t old_len = this->GetSize (); // see *1*
    00144       superclass::SetSize (n);
    00145       for (size_t i = old_len ; i < n ; i++) this->Get(i) = new T (what);
    00146     }
    00147   }
    00148 };
    00149
    00150 #endif // __CS_PTRARR_H__
    00151