## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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
    00039 template <class T, class TbufferKeeper = csRef<iRenderBuffer> >
    00040 class csRenderBufferLock
    00041 {
    00043   TbufferKeeper buffer;
    00045   uint lockState;
    00047   T* lockBuf;
    00049   size_t bufStride;
    00050 #ifdef CS_DEBUG
    00051
    00052   size_t elements;
    00053 #endif
    00054
    00055   size_t currElement;
    00056
    00057   enum { LockedFlag = 0x10000, LockTypeMask = 0xffff };
    00058   inline bool IsLocked() { return (lockState & LockedFlag) != 0; }
    00059   inline void SetLocked (bool b)
    00060   { lockState = b ? (lockState | LockedFlag) : (lockState & ~LockedFlag); }
    00061   inline csRenderBufferLockType LockType()
    00062   { return (csRenderBufferLockType)(lockState & LockTypeMask); }
    00063
    00064   csRenderBufferLock() {}
    00065   // Copying the locking stuff is somewhat nasty so ... prevent it
    00066   csRenderBufferLock (const csRenderBufferLock& other) {}
    00067 public:
    00071   csRenderBufferLock (iRenderBuffer* buf,
    00072     csRenderBufferLockType lock = CS_BUF_LOCK_NORMAL) : buffer(buf),
    00073     lockState(lock), lockBuf(0),
    00074     bufStride(buf ? buf->GetElementDistance() : 0),
    00075     currElement(0)
    00076   {
    00077 #ifdef CS_DEBUG
    00078     elements = buf->GetElementCount();
    00079 #endif
    00080   }
    00081
    00085   ~csRenderBufferLock()
    00086   {
    00087     Unlock();
    00088   }
    00089
    00094   T* Lock ()
    00095   {
    00096     CS_ASSERT (buffer != 0);
    00097     if (!IsLocked())
    00098     {
    00099       lockBuf =
    00100         buffer ? ((T*)((uint8*)buffer->Lock (LockType()))) : (T*)-1;
    00101       SetLocked (true);
    00102     }
    00103     return lockBuf;
    00104   }
    00105
    00107   void Unlock ()
    00108   {
    00109     if (IsLocked ())
    00110     {
    00111       if (buffer) buffer->Release();
    00112       SetLocked (false);
    00113     }
    00114   }
    00115
    00120   operator T* ()
    00121   {
    00122     return Lock();
    00123   }
    00124
    00126   T& operator*()
    00127   {
    00128     return Get (currElement);
    00129   }
    00130
    00132   void operator++ ()
    00133   {
    00134     currElement++;
    00135     CS_ASSERT(currElement<elements);
    00136   }
    00137
    00139   T& operator [] (size_t n)
    00140   {
    00141     return Get (n);
    00142   }
    00143
    00145   T& Get (size_t n)
    00146   {
    00147     CS_ASSERT (n < elements);
    00148     return *((T*)((uint8*)Lock() + n * bufStride));
    00149   }
    00150
    00152   size_t GetSize() const
    00153   {
    00154     return buffer ? buffer->GetElementCount() : 0;
    00155   }
    00156
    00158   bool IsValid() const { return buffer.IsValid(); }
    00159 };
    00160
    00161 #endif // __CS_CSTOOL_RBUFLOCK_H__