Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / win / scoped_gdi.hxx
blob86846e9860d66ca58fbd8dadf3bae2c0c5a09175
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #pragma once
12 #include <win/svsys.h>
13 #include <win/wincomp.hxx>
14 #include <win/saldata.hxx>
16 #include <memory>
18 template <typename H, auto DeleterFunc> struct GDIDeleter
20 using pointer = H;
21 void operator()(H h) { DeleterFunc(h); }
24 template <typename H, auto DeleterFunc>
25 using ScopedGDI = std::unique_ptr<H, GDIDeleter<H, DeleterFunc>>;
27 using ScopedHBRUSH = ScopedGDI<HBRUSH, DeleteBrush>;
28 using ScopedHRGN = ScopedGDI<HRGN, DeleteRegion>;
29 using ScopedHDC = ScopedGDI<HDC, DeleteDC>;
30 using ScopedHPEN = ScopedGDI<HPEN, DeletePen>;
31 using ScopedHFONT = ScopedGDI<HFONT, DeleteFont>;
32 using ScopedHBITMAP = ScopedGDI<HBITMAP, DeleteBitmap>;
34 template <typename ScopedH, auto SelectorFunc> class ScopedSelectedGDI
36 public:
37 ScopedSelectedGDI(HDC hDC, typename ScopedH::pointer h)
38 : m_hDC(hDC)
39 , m_hSelectedH(h)
40 , m_hOrigH(SelectorFunc(hDC, h))
44 ~ScopedSelectedGDI() { SelectorFunc(m_hDC, m_hOrigH); }
46 private:
47 HDC m_hDC;
48 ScopedH m_hSelectedH;
49 typename ScopedH::pointer m_hOrigH;
52 using ScopedSelectedHPEN = ScopedSelectedGDI<ScopedHPEN, SelectPen>;
53 using ScopedSelectedHFONT = ScopedSelectedGDI<ScopedHFONT, SelectFont>;
54 using ScopedSelectedHBRUSH = ScopedSelectedGDI<ScopedHBRUSH, SelectBrush>;
56 template <sal_uLong ID> class ScopedCachedHDC
58 public:
59 explicit ScopedCachedHDC(HBITMAP hBitmap)
60 : m_hDC(ImplGetCachedDC(ID, hBitmap))
64 ~ScopedCachedHDC() { ImplReleaseCachedDC(ID); }
66 HDC get() const { return m_hDC; }
68 private:
69 HDC m_hDC;
72 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */