cstool/rbuflock.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2004 by Jorrit Tyberghein 00003 (C) 2004 by Frank Richter 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_CSTOOL_RBUFLOCK_H__ 00021 #define __CS_CSTOOL_RBUFLOCK_H__ 00022 00027 #include "csutil/ref.h" 00028 #include "ivideo/rndbuf.h" 00029 00043 template <class T, class TbufferKeeper = iRenderBuffer*> 00044 class csRenderBufferLock 00045 { 00047 TbufferKeeper buffer; 00049 T* lockBuf; 00051 size_t bufStride; 00052 #ifdef CS_DEBUG 00054 size_t elements; 00055 #endif 00057 size_t currElement; 00058 00059 typedef csRenderBufferLock<T, TbufferKeeper> LockType; 00069 struct PointerProxy 00070 { 00071 #ifdef CS_DEBUG 00072 const LockType& parent; 00073 size_t elemNum; 00074 #else 00075 T* p; 00076 #endif 00077 00078 PointerProxy (const LockType& parent, size_t elemNum) 00079 #ifdef CS_DEBUG 00080 : parent (parent), elemNum (elemNum) 00081 #else 00082 : p (&parent.Get (elemNum)) 00083 #endif 00084 { } 00085 T& operator*() 00086 { 00087 #ifdef CS_DEBUG 00088 return parent.Get (elemNum); 00089 #else 00090 return *p; 00091 #endif 00092 } 00093 const T& operator*() const 00094 { 00095 #ifdef CS_DEBUG 00096 return parent.Get (elemNum); 00097 #else 00098 return *p; 00099 #endif 00100 } 00101 }; 00102 00103 csRenderBufferLock() {} 00104 // Copying the locking stuff is somewhat nasty so ... prevent it 00105 csRenderBufferLock (const csRenderBufferLock& other) {} 00106 00108 void Unlock () 00109 { 00110 if (buffer) buffer->Release(); 00111 } 00112 public: 00116 csRenderBufferLock (iRenderBuffer* buf, 00117 csRenderBufferLockType lock = CS_BUF_LOCK_NORMAL) : buffer(buf), 00118 lockBuf(0), bufStride(buf ? buf->GetElementDistance() : 0), 00119 currElement(0) 00120 { 00121 #ifdef CS_DEBUG 00122 elements = buf ? buf->GetElementCount() : 0; 00123 #endif 00124 lockBuf = 00125 buffer ? ((T*)((uint8*)buffer->Lock (lock))) : (T*)-1; 00126 } 00127 00131 ~csRenderBufferLock() 00132 { 00133 Unlock(); 00134 } 00135 00140 T* Lock () const 00141 { 00142 return lockBuf; 00143 } 00144 00149 operator T* () const 00150 { 00151 return Lock(); 00152 } 00153 00155 T& operator*() const 00156 { 00157 return Get (currElement); 00158 } 00159 00161 PointerProxy operator++ () 00162 { 00163 currElement++; 00164 return PointerProxy (*this, currElement); 00165 } 00166 00168 PointerProxy operator++ (int) 00169 { 00170 size_t n = currElement; 00171 currElement++; 00172 return PointerProxy (*this, n); 00173 } 00174 00176 PointerProxy operator+= (int n) 00177 { 00178 currElement += n; 00179 return PointerProxy (*this, currElement); 00180 } 00181 00183 T& operator [] (size_t n) const 00184 { 00185 return Get (n); 00186 } 00187 00189 T& Get (size_t n) const 00190 { 00191 CS_ASSERT (n < elements); 00192 return *((T*)((uint8*)Lock() + n * bufStride)); 00193 } 00194 00196 size_t GetSize() const 00197 { 00198 return buffer ? buffer->GetElementCount() : 0; 00199 } 00200 00202 bool IsValid() const { return buffer.IsValid(); } 00203 }; 00204 00205 #endif // __CS_CSTOOL_RBUFLOCK_H__
Generated for Crystal Space 1.4.0 by doxygen 1.5.8