Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef VCL_SCOPEDBITMAPACCESS_HXX_INCLUDED
30 : : #define VCL_SCOPEDBITMAPACCESS_HXX_INCLUDED
31 : :
32 : : namespace vcl
33 : : {
34 : :
35 : : /** This template handles BitmapAccess the RAII way.
36 : :
37 : : Please don't use directly, but through the ready-made typedefs
38 : : ScopedReadAccess and ScopedWriteAccess in classes Bitmap and
39 : : AlphaMask.
40 : :
41 : : Use as follows:
42 : : Bitmap aBitmap;
43 : : Bitmap::ScopedReadAccess pReadAccess( aBitmap );
44 : : pReadAccess->SetPixel()...
45 : :
46 : : Bitmap aBitmap2;
47 : : Bitmap::ScopedWriteAccess pWriteAccess( bCond ? aBitmap2.AcquireWriteAccess() : 0, aBitmap2 );
48 : : if ( pWriteAccess )...
49 : :
50 : : @attention for practical reasons, ScopedBitmapAccess stores a
51 : : reference to the provided bitmap, thus, make sure that the bitmap
52 : : specified at construction time lives at least as long as the
53 : : ScopedBitmapAccess.
54 : : */
55 : : template < class Access, class Bitmap, Access* (Bitmap::* Acquire)() > class ScopedBitmapAccess
56 : : {
57 : : typedef ScopedBitmapAccess< Access, Bitmap, Acquire > self_type;
58 : : typedef bool (self_type::* unspecified_bool_type)() const;
59 : :
60 : : public:
61 : 0 : explicit ScopedBitmapAccess( Bitmap& rBitmap ) :
62 : : mpAccess( 0 ),
63 : 0 : mrBitmap( rBitmap )
64 : : {
65 : 0 : mpAccess = (mrBitmap.*Acquire)();
66 : 0 : }
67 : :
68 : 0 : ScopedBitmapAccess( Access* pAccess, Bitmap& rBitmap ) :
69 : : mpAccess( pAccess ),
70 : 0 : mrBitmap( rBitmap )
71 : : {
72 : 0 : }
73 : :
74 : 0 : ~ScopedBitmapAccess()
75 : : {
76 : 0 : mrBitmap.ReleaseAccess( mpAccess );
77 : 0 : }
78 : :
79 : 0 : bool operator!() const { return !mpAccess; }
80 : 0 : operator unspecified_bool_type() const
81 : : {
82 : 0 : return mpAccess ? &self_type::operator! : 0;
83 : : }
84 : :
85 : 0 : Access* get() { return mpAccess; }
86 : : const Access* get() const { return mpAccess; }
87 : :
88 : 0 : Access* operator->() { return mpAccess; }
89 : : const Access* operator->() const { return mpAccess; }
90 : :
91 : : Access& operator*() { return *mpAccess; }
92 : : const Access& operator*() const { return *mpAccess; }
93 : :
94 : : private:
95 : : Access* mpAccess;
96 : : Bitmap& mrBitmap;
97 : : };
98 : :
99 : : }
100 : :
101 : : #endif // VCL_SCOPEDBITMAPACCESS_HXX_INCLUDED
102 : :
103 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|