blob: 9112fbaa21b8dcd02041f1c6a6a1a443ce0ec793 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#ifndef BASE_SCOPED_HANDLE_H__
6#define BASE_SCOPED_HANDLE_H__
7
8#include <windows.h>
9
10#include "base/basictypes.h"
11
12// Used so we always remember to close the handle. Example:
13// ScopedHandle hfile(CreateFile(...));
14// if (!hfile.Get())
15// ...process error
16// ReadFile(hfile.Get(), ...);
17//
18// To sqirrel the handle away somewhere else:
19// secret_handle_ = hfile.Take();
20//
21// To explicitly close the handle:
22// CloseHandle(hfile.Take());
23class ScopedHandle {
24 public:
25 ScopedHandle() : handle_(NULL) {
26 }
27
28 explicit ScopedHandle(HANDLE h) : handle_(NULL) {
29 Set(h);
30 }
31
32 ~ScopedHandle() {
33 Close();
34 }
35
36 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
37 // usage for errors.
38 bool IsValid() const {
39 return handle_ != NULL;
40 }
41
42 void Set(HANDLE new_handle) {
43 Close();
44
45 // Windows is inconsistent about invalid handles, so we always use NULL
46 if (handle_ != INVALID_HANDLE_VALUE)
47 handle_ = new_handle;
48 }
49
50 HANDLE Get() {
51 return handle_;
52 }
53
54 operator HANDLE() { return handle_; }
55
56 HANDLE Take() {
57 // transfers ownership away from this object
58 HANDLE h = handle_;
59 handle_ = NULL;
60 return h;
61 }
62
63 private:
64 void Close() {
65 if (handle_) {
66 CloseHandle(handle_);
67 handle_ = NULL;
68 }
69 }
70
71 HANDLE handle_;
72 DISALLOW_EVIL_CONSTRUCTORS(ScopedHandle);
73};
74
75// Like ScopedHandle, but for HANDLEs returned from FindFile().
76class ScopedFindFileHandle {
77 public:
78 explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) {
79 // Windows is inconsistent about invalid handles, so we always use NULL
80 if (handle_ == INVALID_HANDLE_VALUE)
81 handle_ = NULL;
82 }
83
84 ~ScopedFindFileHandle() {
85 if (handle_)
86 FindClose(handle_);
87 }
88
89 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
90 // usage for errors.
91 bool IsValid() const { return handle_ != NULL; }
92
93 operator HANDLE() { return handle_; }
94
95 private:
96 HANDLE handle_;
97
98 DISALLOW_EVIL_CONSTRUCTORS(ScopedFindFileHandle);
99};
100
101// Like ScopedHandle but for HDC. Only use this on HDCs returned from
102// CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead.
103class ScopedHDC {
104 public:
105 explicit ScopedHDC(HDC h) : hdc_(h) { }
106
107 ~ScopedHDC() {
108 if (hdc_)
109 DeleteDC(hdc_);
110 }
111
112 operator HDC() { return hdc_; }
113
114 private:
115 HDC hdc_;
116 DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC);
117};
118
119// Like ScopedHandle but for HBITMAP.
120class ScopedBitmap {
121 public:
122 explicit ScopedBitmap(HBITMAP h) : hbitmap_(h) { }
123
124 ~ScopedBitmap() {
125 if (hbitmap_)
126 DeleteObject(hbitmap_);
127 }
128
129 operator HBITMAP() { return hbitmap_; }
130
131 private:
132 HBITMAP hbitmap_;
133 DISALLOW_EVIL_CONSTRUCTORS(ScopedBitmap);
134};
135
136// Like ScopedHandle but for HRGN.
137class ScopedHRGN {
138 public:
139 explicit ScopedHRGN(HRGN h) : hrgn_(h) { }
140
141 ~ScopedHRGN() {
142 if (hrgn_)
143 DeleteObject(hrgn_);
144 }
145
146 operator HRGN() { return hrgn_; }
147
148 ScopedHRGN& operator=(HRGN hrgn) {
149 if (hrgn_ && hrgn != hrgn_)
150 DeleteObject(hrgn_);
151 hrgn_ = hrgn;
152 return *this;
153 }
154
155 private:
156 HRGN hrgn_;
157 DISALLOW_EVIL_CONSTRUCTORS(ScopedHRGN);
158};
159
160// Like ScopedHandle except for HGLOBAL.
161template<class T>
162class ScopedHGlobal {
163 public:
164 explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
165 data_ = static_cast<T*>(GlobalLock(glob_));
166 }
167 ~ScopedHGlobal() {
168 GlobalUnlock(glob_);
169 }
170
171 T* get() { return data_; }
172
173 size_t Size() const { return GlobalSize(glob_); }
174
175 T* operator->() const {
176 assert(data_ != 0);
177 return data_;
178 }
179
180 private:
181 HGLOBAL glob_;
182
183 T* data_;
184
185 DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal);
186};
187
188#endif // BASE_SCOPED_HANDLE_H__
license.botf003cfe2008-08-24 09:55:55 +0900189