CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

sarray.h

Go to the documentation of this file.
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 __CS_SARRAY_H__
00020 #define __CS_SARRAY_H__
00021 
00022 #ifndef COMP_VC
00023 # warning csStaticArray is DEPRECATED. Use csGrowingArray instead
00024 #endif
00025 
00034 #define CS_DECLARE_STATIC_ARRAY(Name,Type)                              \
00035   CS_PRIVATE_DECLARE_STATIC_ARRAY (Name, Type)
00036 
00037 //----------------------------------------------------------------------------
00038 //--- implementation of the above macro follows ------------------------------
00039 //----------------------------------------------------------------------------
00040 
00042 class csStaticArray
00043 {
00044 protected:
00045   void *Map;
00046   int Size;
00047 
00052   void Copy (const csStaticArray *other, bool DeleteOld = true);
00057   void Copy (void *NewData, int NewSize, bool DeleteOld = true);
00063   inline void TakeOver (csStaticArray *other, bool DeleteOld = true);
00068   inline void TakeOver (void *NewData, int NewSize, bool DeleteOld = true);
00069 
00071   virtual void *AllocateArray (int Size) const = 0;
00073   virtual void DeleteArray (void *Array) const = 0;
00075   virtual void CopyArray (void *Dest, void *src, int Count) const = 0;
00076 
00077 public:
00079   csStaticArray (int Size = 0);
00081   virtual ~csStaticArray ();
00082 
00084   inline int GetSize () const;
00089   void Clear (bool DeleteOld = true);
00094   void Alloc (int s, bool DeleteOld = true);
00098   void ReAlloc (int s);
00099 };
00100 
00101 #define CS_PRIVATE_DECLARE_STATIC_ARRAY(Name,Type)                      \
00102 class Name : public csStaticArray                                       \
00103 {                                                                       \
00104   typedef Type cont_type;                                               \
00105 private:                                                                \
00106   void *AllocateArray (int n) const {return new cont_type[n];}          \
00107   void DeleteArray (void *a) const {delete[] ((cont_type*)a);}          \
00108   void CopyArray (void *Dest, void *Src, int n) const                   \
00109     {memcpy (Dest, Src, n*sizeof (cont_type)); }                        \
00110 public:                                                                 \
00111   inline Name (int Size = 0) :                                          \
00112     csStaticArray (Size) {}                                             \
00113   virtual ~Name ()                                                       \
00114   { Clear (); }                                                         \
00115   inline cont_type *GetArray ()                                         \
00116     { return (cont_type*)Map; }                                         \
00117   inline const cont_type *GetArray () const                             \
00118     { return (cont_type*)Map; }                                         \
00119   inline void Copy (cont_type *NewData, int NewSize, bool DeleteOld = true)     \
00120     { csStaticArray::Copy (NewData, NewSize, DeleteOld); }              \
00121   inline void Copy (const Name *other, bool DeleteOld = true)           \
00122     { csStaticArray::Copy (other, DeleteOld); }                         \
00123   inline void TakeOver (cont_type *NewData, int NewSize, bool DeleteOld = true) \
00124     { csStaticArray::TakeOver (NewData, NewSize, DeleteOld); }          \
00125   inline void TakeOver (Name *other, bool DeleteOld = true)             \
00126     { csStaticArray::TakeOver (other, DeleteOld); }                     \
00127   inline cont_type &operator[] (int n)                                  \
00128     { return GetArray () [n]; }                                         \
00129   inline const cont_type &operator[] (int n) const                      \
00130     { return GetArray () [n]; }                                         \
00131 }
00132 
00133 inline int csStaticArray::GetSize () const
00134   {
00135     return Size;
00136   }
00137 inline void csStaticArray::TakeOver (csStaticArray *other, bool DeleteOld)
00138   {
00139     TakeOver (other->Map, other->Size, DeleteOld);
00140     other->Map = NULL;
00141     other->Size = 0;
00142   }
00143 inline void csStaticArray::TakeOver (void *NewData, int NewSize, bool DeleteOld)
00144   {
00145     if (DeleteOld) Clear ();
00146     Map = NewData;
00147     Size = NewSize;
00148   }
00149 
00150 #endif // __CS_SARRAY_H__

Generated for Crystal Space by doxygen 1.2.14