Port Android relocation packer to chromium build
[chromium-blink-merge.git] / base / win / scoped_hdc.h
blob9aead967929dba6b1866347a7a18c7426262d2be
1 // Copyright (c) 2012 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.
5 #ifndef BASE_WIN_SCOPED_HDC_H_
6 #define BASE_WIN_SCOPED_HDC_H_
8 #include <windows.h>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/win/scoped_handle.h"
14 namespace base {
15 namespace win {
17 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
18 // GetDC.
19 class ScopedGetDC {
20 public:
21 explicit ScopedGetDC(HWND hwnd)
22 : hwnd_(hwnd),
23 hdc_(GetDC(hwnd)) {
24 if (hwnd_) {
25 DCHECK(IsWindow(hwnd_));
26 DCHECK(hdc_);
27 } else {
28 // If GetDC(NULL) returns NULL, something really bad has happened, like
29 // GDI handle exhaustion. In this case Chrome is going to behave badly no
30 // matter what, so we may as well just force a crash now.
31 CHECK(hdc_);
35 ~ScopedGetDC() {
36 if (hdc_)
37 ReleaseDC(hwnd_, hdc_);
40 operator HDC() { return hdc_; }
42 private:
43 HWND hwnd_;
44 HDC hdc_;
46 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC);
49 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
50 // CreateCompatibleDC, CreateDC and CreateIC.
51 class CreateDCTraits {
52 public:
53 typedef HDC Handle;
55 static bool CloseHandle(HDC handle) {
56 return ::DeleteDC(handle) != FALSE;
59 static bool IsHandleValid(HDC handle) {
60 return handle != NULL;
63 static HDC NullHandle() {
64 return NULL;
67 private:
68 DISALLOW_IMPLICIT_CONSTRUCTORS(CreateDCTraits);
71 typedef GenericScopedHandle<CreateDCTraits, VerifierTraits> ScopedCreateDC;
73 } // namespace win
74 } // namespace base
76 #endif // BASE_WIN_SCOPED_HDC_H_