xref: /aosp_15_r20/external/cronet/base/win/scoped_select_object.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
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 BASE_WIN_SCOPED_SELECT_OBJECT_H_
6 #define BASE_WIN_SCOPED_SELECT_OBJECT_H_
7 
8 #include <windows.h>
9 
10 #include "base/check.h"
11 
12 namespace base {
13 namespace win {
14 
15 // Helper class for deselecting object from DC.
16 class ScopedSelectObject {
17  public:
ScopedSelectObject(HDC hdc,HGDIOBJ object)18   ScopedSelectObject(HDC hdc, HGDIOBJ object)
19       : hdc_(hdc), oldobj_(SelectObject(hdc, object)) {
20     DCHECK(hdc_);
21     DCHECK(object);
22     DCHECK(oldobj_);
23     DCHECK(oldobj_ != HGDI_ERROR);
24   }
25 
26   ScopedSelectObject(const ScopedSelectObject&) = delete;
27   ScopedSelectObject& operator=(const ScopedSelectObject&) = delete;
28 
~ScopedSelectObject()29   ~ScopedSelectObject() {
30     HGDIOBJ object = SelectObject(hdc_, oldobj_);
31     DCHECK((GetObjectType(oldobj_) != OBJ_REGION && object) ||
32            (GetObjectType(oldobj_) == OBJ_REGION && object != HGDI_ERROR));
33   }
34 
35  private:
36   const HDC hdc_;
37   const HGDIOBJ oldobj_;
38 };
39 
40 }  // namespace win
41 }  // namespace base
42 
43 #endif  // BASE_WIN_SCOPED_SELECT_OBJECT_H_
44