Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00053
00054 size_t elements;
00055 #endif
00056
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
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__