Bump version to 6.4-15
[LibreOffice.git] / include / vcl / scopedbitmapaccess.hxx
blobf63bcc55f8a3d73eff081a942f9b0601f95fa8dc
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 namespace vcl
26 /** This template handles BitmapAccess the RAII way.
28 Please don't use directly, but through the ready-made typedefs
29 ScopedReadAccess and ScopedWriteAccess in classes Bitmap and
30 AlphaMask.
32 Use as follows:
33 Bitmap aBitmap;
34 Bitmap::ScopedReadAccess pReadAccess( aBitmap );
35 pReadAccess->SetPixel()...
37 Bitmap aBitmap2;
38 BitmapScopedWriteAccess pWriteAccess( bCond ? aBitmap2.AcquireWriteAccess() : 0, aBitmap2 );
39 if ( pWriteAccess )...
41 @attention for practical reasons, ScopedBitmapAccess stores a
42 reference to the provided bitmap, thus, make sure that the bitmap
43 specified at construction time lives at least as long as the
44 ScopedBitmapAccess.
46 template < class Access, class Bitmap, Access* (Bitmap::* Acquire)() > class ScopedBitmapAccess
48 public:
49 explicit ScopedBitmapAccess( Bitmap& rBitmap ) :
50 mpAccess( nullptr ),
51 mpBitmap( &rBitmap )
53 mpAccess = (mpBitmap->*Acquire)();
56 ScopedBitmapAccess( Access* pAccess, Bitmap& rBitmap ) :
57 mpAccess( pAccess ),
58 mpBitmap( &rBitmap )
62 ScopedBitmapAccess( ) :
63 mpAccess( nullptr ),
64 mpBitmap( nullptr )
68 // Move semantics
69 ScopedBitmapAccess &operator=(ScopedBitmapAccess&&other)
71 mpAccess=other.mpAccess;
72 mpBitmap=other.mpBitmap;
73 other.mpAccess = nullptr;
74 other.mpBitmap = nullptr;
75 return *this;
78 // Disable copy from lvalue.
79 ScopedBitmapAccess(const ScopedBitmapAccess&) = delete;
80 ScopedBitmapAccess &operator=(const ScopedBitmapAccess&) = delete;
82 ~ScopedBitmapAccess() COVERITY_NOEXCEPT_FALSE
84 if (mpAccess)
85 mpBitmap->ReleaseAccess( mpAccess );
88 void reset()
90 if (mpAccess)
92 mpBitmap->ReleaseAccess( mpAccess );
93 mpAccess = nullptr;
97 bool operator!() const { return !mpAccess; }
98 explicit operator bool() const
100 return mpAccess;
103 Access* get() { return mpAccess; }
104 const Access* get() const { return mpAccess; }
106 Access* operator->() { return mpAccess; }
107 const Access* operator->() const { return mpAccess; }
109 Access& operator*() { return *mpAccess; }
110 const Access& operator*() const { return *mpAccess; }
112 private:
113 Access* mpAccess;
114 Bitmap* mpBitmap;
119 #endif // INCLUDED_VCL_SCOPEDBITMAPACCESS_HXX
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */