Template Class ScopedBitmapLocker#

Inheritance Relationships#

Base Type#

Class Documentation#

template<typename tBitmap>
class ScopedBitmapLocker : public libCZI::BitmapLockInfo#

A helper class used to scope the lock state of a bitmap.

It is intended to be used like this:

libCZI::IBitmapData* bm = ... // assume that we have a pointer to a bitmap

// this example assumes that the pixel type is Gray8 and nothing else...
if (bm->GetPixelType() != libCZI::PixelType::Gray8) throw std::exception();

{
    // access the bitmap's pixels directly within this scope
    libCZI::ScopedBitmapLocker<libCZI::IBitmapData*> lckBm{ bm };   // <- will call bm->Lock here
    for (std::uint32_t y  = 0; y < bm->GetHeight(); ++y)
    {
        const std::uint8_t* ptrLine = ((const std::uint8_t*)lckBm.ptrDataRoi) + y * lckBm.stride;
        for (std::uint32_t x  = 0; x < bm->GetWidth(); ++x)
        {
            auto pixelVal = ptrLine[x];
            // do something with the pixel...
        }
    }
    
    // when lckBm goes out of scope, bm->Unlock will be called
 }

For convenience two typedef are provided: ScopedBitmapLockerP and ScopedBitmapLockerSP for use with the types IBitmapData* and std::shared_ptr<IBitmapData>.

typedef ScopedBitmapLocker<IBitmapData*> ScopedBitmapLockerP;
typedef ScopedBitmapLocker<std::shared_ptr<IBitmapData>> ScopedBitmapLockerSP;

So in above sample we could have used

libCZI::ScopedBitmapLockerP lckBm{ bm };

This utility is intended to help adhering to the RAII-pattern, since it makes writing exception-safe code easier - in case of an exception (within the scope of the ScopedBitmapLocker object) the bitmap’s Unlock method will be called (which is cumbersome to achieve otherwise).

Public Functions

ScopedBitmapLocker() = delete#
inline explicit ScopedBitmapLocker(tBitmap bmData)#

Constructor taking the object for which we provide the scope-guard.

Parameters:

bmData – The object for which we are to provide the scope-guard.

inline ScopedBitmapLocker(const ScopedBitmapLocker<tBitmap> &other)#

Copy-Constructor .

Parameters:

other – The other object.

inline ScopedBitmapLocker(ScopedBitmapLocker<tBitmap> &&other) noexcept#

move constructor

inline ScopedBitmapLocker<tBitmap> &operator=(ScopedBitmapLocker<tBitmap> &&other) noexcept#

move assignment

inline ~ScopedBitmapLocker()#