Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / vcl / scopedbitmapaccess.hxx
blobb06618091529baf1f3eb9cf2985ebbe5c060962c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_VCL_SCOPEDBITMAPACCESS_HXX
21 #define INCLUDED_VCL_SCOPEDBITMAPACCESS_HXX
23 #include <sal/types.h>
25 namespace vcl
28 /** This template handles BitmapAccess the RAII way.
30 Please don't use directly, but through the ready-made typedefs
31 ScopedReadAccess and ScopedWriteAccess in classes Bitmap and
32 AlphaMask.
34 Use as follows:
35 Bitmap aBitmap;
36 Bitmap::ScopedReadAccess pReadAccess( aBitmap );
37 pReadAccess->SetPixel()...
39 Bitmap aBitmap2;
40 BitmapScopedWriteAccess pWriteAccess( bCond ? aBitmap2.AcquireWriteAccess() : 0, aBitmap2 );
41 if ( pWriteAccess )...
43 @attention for practical reasons, ScopedBitmapAccess stores a
44 reference to the provided bitmap, thus, make sure that the bitmap
45 specified at construction time lives at least as long as the
46 ScopedBitmapAccess.
48 template < class Access, class Bitmap, Access* (Bitmap::* Acquire)() > class ScopedBitmapAccess
50 public:
51 explicit ScopedBitmapAccess( Bitmap& rBitmap ) :
52 mpAccess( nullptr ),
53 mpBitmap( &rBitmap )
55 mpAccess = (mpBitmap->*Acquire)();
58 ScopedBitmapAccess( Access* pAccess, Bitmap& rBitmap ) :
59 mpAccess( pAccess ),
60 mpBitmap( &rBitmap )
64 ScopedBitmapAccess( ) :
65 mpAccess( nullptr ),
66 mpBitmap( nullptr )
70 // Move semantics
71 ScopedBitmapAccess &operator=(ScopedBitmapAccess&&other)
73 mpAccess=other.mpAccess;
74 mpBitmap=other.mpBitmap;
75 other.mpAccess = nullptr;
76 other.mpBitmap = nullptr;
77 return *this;
80 // Disable copy from lvalue.
81 ScopedBitmapAccess(const ScopedBitmapAccess&) = delete;
82 ScopedBitmapAccess &operator=(const ScopedBitmapAccess&) = delete;
84 ~ScopedBitmapAccess() COVERITY_NOEXCEPT_FALSE
86 if (mpAccess)
87 mpBitmap->ReleaseAccess( mpAccess );
90 void reset()
92 if (mpAccess)
94 mpBitmap->ReleaseAccess( mpAccess );
95 mpAccess = nullptr;
99 bool operator!() const { return !mpAccess; }
100 explicit operator bool() const
102 return mpAccess;
105 Access* get() { return mpAccess; }
106 const Access* get() const { return mpAccess; }
108 Access* operator->() { return mpAccess; }
109 const Access* operator->() const { return mpAccess; }
111 Access& operator*() { return *mpAccess; }
112 const Access& operator*() const { return *mpAccess; }
114 private:
115 Access* mpAccess;
116 Bitmap* mpBitmap;
121 #endif // INCLUDED_VCL_SCOPEDBITMAPACCESS_HXX
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */