xref: /aosp_15_r20/external/pdfium/third_party/base/win/scoped_select_object.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BASE_WIN_SCOPED_SELECT_OBJECT_H_
6 #define THIRD_PARTY_BASE_WIN_SCOPED_SELECT_OBJECT_H_
7 
8 #include <windows.h>
9 
10 #include "third_party/base/check.h"
11 
12 namespace pdfium {
13 namespace base {
14 namespace win {
15 
16 // Helper class for deselecting object from DC.
17 class ScopedSelectObject {
18  public:
ScopedSelectObject(HDC hdc,HGDIOBJ object)19   ScopedSelectObject(HDC hdc, HGDIOBJ object)
20       : hdc_(hdc), oldobj_(SelectObject(hdc, object)) {
21     DCHECK(hdc_);
22     DCHECK(object);
23     DCHECK(oldobj_);
24     DCHECK(oldobj_ != HGDI_ERROR);
25   }
26 
27   ScopedSelectObject(const ScopedSelectObject&) = delete;
28   ScopedSelectObject& operator=(const ScopedSelectObject&) = delete;
29 
~ScopedSelectObject()30   ~ScopedSelectObject() {
31     [[maybe_unused]] HGDIOBJ object = SelectObject(hdc_, oldobj_);
32     DCHECK((GetObjectType(oldobj_) != OBJ_REGION && object) ||
33            (GetObjectType(oldobj_) == OBJ_REGION && object != HGDI_ERROR));
34   }
35 
36  private:
37   const HDC hdc_;
38   const HGDIOBJ oldobj_;
39 };
40 
41 }  // namespace win
42 }  // namespace base
43 }  // namespace pdfium
44 
45 #endif  // THIRD_PARTY_BASE_WIN_SCOPED_SELECT_OBJECT_H_
46