Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

sarray.h

00001 /*
00002     Copyright (C) 2001 by Martin Geisse <mgeisse@gmx.net>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __SARRAY_H__
00020 #define __SARRAY_H__
00021 
00026 #define CS_DECLARE_STATIC_ARRAY(Name,Type)                              \
00027   CS_PRIVATE_DECLARE_STATIC_ARRAY (Name, Type)
00028 
00029 //----------------------------------------------------------------------------
00030 //--- implementation of the above macro follows ------------------------------
00031 //----------------------------------------------------------------------------
00032 
00033 class csStaticArray
00034 {
00035 protected:
00036   void *Map;
00037   int Size;
00038 
00039   /*
00040    * Remove all elements and copy the contents from another array. If
00041    * 'DeleteOld' is true then the old contents are deleted.
00042    */
00043   void Copy (const csStaticArray *other, bool DeleteOld = true);
00044   /*
00045    * Remove all elements and copy the contents from another array. If
00046    * 'DeleteOld' is true then the old contents are deleted.
00047    */
00048   void Copy (void *NewData, int NewSize, bool DeleteOld = true);
00049   /*
00050    * Remove all elements (and delete them if 'DeleteOld is true), then
00051    * use the contents of another array. The other array will lose control
00052    * over its elements and be cleared to empty.
00053    */
00054   inline void TakeOver (csStaticArray *other, bool DeleteOld = true);
00055   /*
00056    * Use the given array and size for this array. They are taken over,
00057    * not copied!
00058    */
00059   inline void TakeOver (void *NewData, int NewSize, bool DeleteOld = true);
00060 
00061   // Allocate an array of the given number of elements
00062   virtual void *AllocateArray (int Size) const = 0;
00063   // Delete an array of elements
00064   virtual void DeleteArray (void *Array) const = 0;
00065   // Copy one array into another one
00066   virtual void CopyArray (void *Dest, void *src, int Count) const = 0;
00067 
00068 public:
00069   // constructor
00070   csStaticArray (int Size = 0);
00071   // destructor
00072   virtual ~csStaticArray ();
00073 
00074   // Return the number of elements in the array
00075   inline int GetSize () const;
00076   /*
00077    * Remove all elements from the array. If 'DeleteOld' is true then the
00078    * elements are also deleted.
00079    */
00080   void Clear (bool DeleteOld = true);
00081   /*
00082    * Allocate the given number of elements. The old contents are removed,
00083    * and if 'DeleteOld' is true they are also deleted.
00084    */
00085   void Alloc (int s, bool DeleteOld = true);
00086   /*
00087    * Change the size of the array but copy the contents.
00088    */
00089   void ReAlloc (int s);
00090 };
00091 
00092 #define CS_PRIVATE_DECLARE_STATIC_ARRAY(Name,Type)                      \
00093 class Name : public csStaticArray                                       \
00094 {                                                                       \
00095   typedef Type cont_type;                                               \
00096 private:                                                                \
00097   void *AllocateArray (int n) const {return new cont_type[n];}          \
00098   void DeleteArray (void *a) const {delete[] ((cont_type*)a);}          \
00099   void CopyArray (void *Dest, void *Src, int n) const                   \
00100     {memcpy (Dest, Src, n*sizeof (cont_type)); }                        \
00101 public:                                                                 \
00102   inline Name (int Size = 0) :                                          \
00103     csStaticArray (Size) {}                                             \
00104   virtual ~Name ()                                                       \
00105   { Clear (); }                                                         \
00106   inline cont_type *GetArray ()                                         \
00107     { return (cont_type*)Map; }                                         \
00108   inline const cont_type *GetArray () const                             \
00109     { return (cont_type*)Map; }                                         \
00110   inline void Copy (cont_type *NewData, int NewSize, bool DeleteOld = true)     \
00111     { csStaticArray::Copy (NewData, NewSize, DeleteOld); }              \
00112   inline void Copy (const Name *other, bool DeleteOld = true)           \
00113     { csStaticArray::Copy (other, DeleteOld); }                         \
00114   inline void TakeOver (cont_type *NewData, int NewSize, bool DeleteOld = true) \
00115     { csStaticArray::TakeOver (NewData, NewSize, DeleteOld); }          \
00116   inline void TakeOver (Name *other, bool DeleteOld = true)             \
00117     { csStaticArray::TakeOver (other, DeleteOld); }                     \
00118   inline cont_type &operator[] (int n)                                  \
00119     { return GetArray () [n]; }                                         \
00120   inline const cont_type &operator[] (int n) const                      \
00121     { return GetArray () [n]; }                                         \
00122 }
00123 
00124 inline int csStaticArray::GetSize () const
00125   {
00126     return Size;
00127   }
00128 inline void csStaticArray::TakeOver (csStaticArray *other, bool DeleteOld)
00129   {
00130     TakeOver (other->Map, other->Size, DeleteOld);
00131     other->Map = NULL;
00132     other->Size = 0;
00133   }
00134 inline void csStaticArray::TakeOver (void *NewData, int NewSize, bool DeleteOld)
00135   {
00136     if (DeleteOld) Clear ();
00137     Map = NewData;
00138     Size = NewSize;
00139   }
00140 
00141 #endif // __SARRAY_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000