CrystalSpace

Public API Reference

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     CS_ASSERT (buffer != 0);
00125     lockBuf = 
00126       buffer ? ((T*)((uint8*)buffer->Lock (lock))) : (T*)-1;
00127   }
00128   
00132   ~csRenderBufferLock()
00133   {
00134     Unlock();
00135   }
00136   
00141   T* Lock () const
00142   {
00143     return lockBuf;
00144   }
00145   
00150   operator T* () const
00151   {
00152     return Lock();
00153   }
00154 
00156   T& operator*() const
00157   {
00158     return Get (currElement);
00159   }
00160 
00162   PointerProxy operator++ ()  
00163   {
00164     currElement++;
00165     return PointerProxy (*this, currElement);
00166   }
00167 
00169   PointerProxy operator++ (int)
00170   {
00171     size_t n = currElement;
00172     currElement++;
00173     return PointerProxy (*this, n);
00174   }
00175 
00177   PointerProxy operator+= (int n)
00178   {
00179     currElement += n;
00180     return PointerProxy (*this, currElement);
00181   }
00182 
00184   T& operator [] (size_t n) const
00185   {
00186     return Get (n);
00187   }
00188 
00190   T& Get (size_t n) const
00191   {
00192     CS_ASSERT (n < elements);
00193     return *((T*)((uint8*)Lock() + n * bufStride));
00194   }
00195   
00197   size_t GetSize() const
00198   {
00199     return buffer ? buffer->GetElementCount() : 0;
00200   }
00201 
00203   bool IsValid() const { return buffer.IsValid(); }
00204 };
00205 
00206 #endif // __CS_CSTOOL_RBUFLOCK_H__

Generated for Crystal Space by doxygen 1.4.7