csutil/dirtyaccessarray.h
Go to the documentation of this file.00001 /* 00002 Crystal Space utility library: vector class interface 00003 Copyright (C) 1998,1999,2000 by Andrew Zabolotny <bit@eltech.ru> 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_DIRTYACCESSARRAY_H__ 00021 #define __CS_DIRTYACCESSARRAY_H__ 00022 00028 //----------------------------------------------------------------------------- 00029 // Note *1*: The explicit "this->" is needed by modern compilers (such as gcc 00030 // 3.4.x) which distinguish between dependent and non-dependent names in 00031 // templates. See: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html 00032 //----------------------------------------------------------------------------- 00033 00034 #include "array.h" 00035 00045 template <class T, 00046 class ElementHandler = csArrayElementHandler<T>, 00047 class MemoryAllocator = CS::Container::ArrayAllocDefault, 00048 class CapacityHandler = CS::Container::ArrayCapacityDefault> 00049 class csDirtyAccessArray : 00050 public csArray<T, ElementHandler, MemoryAllocator, CapacityHandler> 00051 { 00052 public: 00059 csDirtyAccessArray (size_t in_capacity = 0, 00060 const CapacityHandler& ch = CapacityHandler()) 00061 : csArray<T, ElementHandler, MemoryAllocator, CapacityHandler> ( 00062 in_capacity, ch) {} 00063 00065 T* GetArray () 00066 { 00067 if (this->GetSize () > 0) // see *1* 00068 return &this->Get (0); 00069 else 00070 return 0; 00071 } 00072 00074 const T* GetArray () const 00075 { 00076 if (this->GetSize () > 0) // see *1* 00077 return &this->Get (0); 00078 else 00079 return 0; 00080 } 00081 00087 T* GetArrayCopy () 00088 { 00089 if (this->GetSize () > 0) // see *1* 00090 { 00091 T* copy = new T [this->GetSize ()]; 00092 memcpy (copy, &this->Get (0), sizeof (T) * this->GetSize ()); 00093 return copy; 00094 } 00095 else 00096 return 0; 00097 } 00098 00105 T* Detach () 00106 { 00107 T* ptr = GetArray (); 00108 this->SetDataVeryUnsafe (nullptr); 00109 this->SetSizeVeryUnsafe (0); 00110 this->SetCapacityVeryUnsafe (0); 00111 return ptr; 00112 } 00113 }; 00114 00120 template <class T, class ElementHandler = csArrayElementHandler<T>, 00121 class MemoryAllocator = CS::Container::ArrayAllocDefault, 00122 class CapacityHandler = CS::Container::ArrayCapacityDefault> 00123 class csDirtyAccessArrayRefCounted : 00124 public csDirtyAccessArray<T, ElementHandler, MemoryAllocator, 00125 CapacityHandler> 00126 { 00127 private: 00128 int RefCount; 00129 public: 00130 csDirtyAccessArrayRefCounted (int ilimit = 0, 00131 const CapacityHandler& ch = CapacityHandler()) 00132 : csDirtyAccessArray<T, ElementHandler, MemoryAllocator, 00133 CapacityHandler> (ilimit, ch), RefCount (0) 00134 { } 00135 00137 void IncRef () { RefCount++; } 00138 00140 void DecRef () 00141 { 00142 if (RefCount == 1) { this->DeleteAll (); } // see *1* 00143 RefCount--; 00144 } 00145 00146 }; 00147 00148 00149 #endif // __CS_DIRTYACCESSARRAY_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1