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 operator T*() const 00102 { 00103 #ifdef CS_DEBUG 00104 return &(parent.Get (elemNum)); 00105 #else 00106 return p; 00107 #endif 00108 } 00109 }; 00110 00111 csRenderBufferLock() {} 00112 // Copying the locking stuff is somewhat nasty so ... prevent it 00113 csRenderBufferLock (const csRenderBufferLock& other) {} 00114 00116 void Unlock () 00117 { 00118 if (buffer) buffer->Release(); 00119 } 00120 public: 00124 csRenderBufferLock (iRenderBuffer* buf, 00125 csRenderBufferLockType lock = CS_BUF_LOCK_NORMAL) : buffer(buf), 00126 lockBuf(0), bufStride(buf ? buf->GetElementDistance() : 0), 00127 currElement(0) 00128 { 00129 #ifdef CS_DEBUG 00130 elements = buf ? buf->GetElementCount() : 0; 00131 #endif 00132 lockBuf = 00133 buffer ? ((T*)((uint8*)buffer->Lock (lock))) : (T*)-1; 00134 } 00135 00139 ~csRenderBufferLock() 00140 { 00141 Unlock(); 00142 } 00143 00148 T* Lock () const 00149 { 00150 return lockBuf; 00151 } 00152 00157 operator T* () const 00158 { 00159 return Lock(); 00160 } 00161 00163 T& operator*() const 00164 { 00165 return Get (currElement); 00166 } 00167 00169 PointerProxy operator++ () 00170 { 00171 currElement++; 00172 return PointerProxy (*this, currElement); 00173 } 00174 00176 PointerProxy operator++ (int) 00177 { 00178 size_t n = currElement; 00179 currElement++; 00180 return PointerProxy (*this, n); 00181 } 00182 00184 PointerProxy operator+= (int n) 00185 { 00186 currElement += n; 00187 return PointerProxy (*this, currElement); 00188 } 00189 00191 T& operator [] (size_t n) const 00192 { 00193 return Get (n); 00194 } 00195 00197 T& Get (size_t n) const 00198 { 00199 CS_ASSERT (n < elements); 00200 return *((T*)((uint8*)Lock() + n * bufStride)); 00201 } 00202 00204 size_t GetSize() const 00205 { 00206 return buffer ? buffer->GetElementCount() : 0; 00207 } 00208 00210 bool IsValid() const { return buffer.IsValid(); } 00211 }; 00212 00213 #endif // __CS_CSTOOL_RBUFLOCK_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1