Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: base/scoped_handle_win.h

Issue 3781009: Move the windows-specific scoped_* stuff from base to base/win and in the bas... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_SCOPED_HANDLE_WIN_H_ 5 // TODO(brettw) remove this file when all callers are converted to using the
6 #define BASE_SCOPED_HANDLE_WIN_H_ 6 // new location/namespace
7 #pragma once 7 #include "base/win/scoped_handle.h"
8 #include "base/win/scoped_gdi_object.h"
9 #include "base/win/scoped_handle.h"
10 #include "base/win/scoped_hdc.h"
11 #include "base/win/scoped_hglobal.h"
8 12
9 #include <windows.h> 13 using base::win::ScopedBitmap;
10 14 using base::win::ScopedGDIObject;
11 #include "base/basictypes.h" 15 using base::win::ScopedHandle;
12 #include "base/logging.h" 16 using base::win::ScopedHDC;
13 17 using base::win::ScopedHFONT;
14 // Used so we always remember to close the handle. 18 using base::win::ScopedHGlobal;
15 // The class interface matches that of ScopedStdioHandle in addition to an 19 using base::win::ScopedHICON;
16 // IsValid() method since invalid handles on windows can be either NULL or 20 using base::win::ScopedRegion;
17 // INVALID_HANDLE_VALUE (-1).
18 //
19 // Example:
20 // ScopedHandle hfile(CreateFile(...));
21 // if (!hfile.Get())
22 // ...process error
23 // ReadFile(hfile.Get(), ...);
24 //
25 // To sqirrel the handle away somewhere else:
26 // secret_handle_ = hfile.Take();
27 //
28 // To explicitly close the handle:
29 // hfile.Close();
30 class ScopedHandle {
31 public:
32 ScopedHandle() : handle_(NULL) {
33 }
34
35 explicit ScopedHandle(HANDLE h) : handle_(NULL) {
36 Set(h);
37 }
38
39 ~ScopedHandle() {
40 Close();
41 }
42
43 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
44 // usage for errors.
45 bool IsValid() const {
46 return handle_ != NULL;
47 }
48
49 void Set(HANDLE new_handle) {
50 Close();
51
52 // Windows is inconsistent about invalid handles, so we always use NULL
53 if (new_handle != INVALID_HANDLE_VALUE)
54 handle_ = new_handle;
55 }
56
57 HANDLE Get() {
58 return handle_;
59 }
60
61 operator HANDLE() { return handle_; }
62
63 HANDLE Take() {
64 // transfers ownership away from this object
65 HANDLE h = handle_;
66 handle_ = NULL;
67 return h;
68 }
69
70 void Close() {
71 if (handle_) {
72 if (!::CloseHandle(handle_)) {
73 NOTREACHED();
74 }
75 handle_ = NULL;
76 }
77 }
78
79 private:
80 HANDLE handle_;
81 DISALLOW_COPY_AND_ASSIGN(ScopedHandle);
82 };
83
84 // Like ScopedHandle, but for HANDLEs returned from FindFile().
85 class ScopedFindFileHandle {
86 public:
87 explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) {
88 // Windows is inconsistent about invalid handles, so we always use NULL
89 if (handle_ == INVALID_HANDLE_VALUE)
90 handle_ = NULL;
91 }
92
93 ~ScopedFindFileHandle() {
94 if (handle_)
95 FindClose(handle_);
96 }
97
98 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL
99 // usage for errors.
100 bool IsValid() const { return handle_ != NULL; }
101
102 operator HANDLE() { return handle_; }
103
104 private:
105 HANDLE handle_;
106
107 DISALLOW_COPY_AND_ASSIGN(ScopedFindFileHandle);
108 };
109
110 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
111 // CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead.
112 class ScopedHDC {
113 public:
114 ScopedHDC() : hdc_(NULL) { }
115 explicit ScopedHDC(HDC h) : hdc_(h) { }
116
117 ~ScopedHDC() {
118 Close();
119 }
120
121 HDC Get() {
122 return hdc_;
123 }
124
125 void Set(HDC h) {
126 Close();
127 hdc_ = h;
128 }
129
130 operator HDC() { return hdc_; }
131
132 private:
133 void Close() {
134 #ifdef NOGDI
135 assert(false);
136 #else
137 if (hdc_)
138 DeleteDC(hdc_);
139 #endif // NOGDI
140 }
141
142 HDC hdc_;
143 DISALLOW_COPY_AND_ASSIGN(ScopedHDC);
144 };
145
146 // Like ScopedHandle but for GDI objects.
147 template<class T>
148 class ScopedGDIObject {
149 public:
150 ScopedGDIObject() : object_(NULL) {}
151 explicit ScopedGDIObject(T object) : object_(object) {}
152
153 ~ScopedGDIObject() {
154 Close();
155 }
156
157 T Get() {
158 return object_;
159 }
160
161 void Set(T object) {
162 if (object_ && object != object_)
163 Close();
164 object_ = object;
165 }
166
167 ScopedGDIObject& operator=(T object) {
168 Set(object);
169 return *this;
170 }
171
172 T release() {
173 T object = object_;
174 object_ = NULL;
175 return object;
176 }
177
178 operator T() { return object_; }
179
180 private:
181 void Close() {
182 if (object_)
183 DeleteObject(object_);
184 }
185
186 T object_;
187 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
188 };
189
190 // An explicit specialization for HICON because we have to call DestroyIcon()
191 // instead of DeleteObject() for HICON.
192 template<>
193 void ScopedGDIObject<HICON>::Close() {
194 if (object_)
195 DestroyIcon(object_);
196 }
197
198 // Typedefs for some common use cases.
199 typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
200 typedef ScopedGDIObject<HRGN> ScopedRegion;
201 typedef ScopedGDIObject<HFONT> ScopedHFONT;
202 typedef ScopedGDIObject<HICON> ScopedHICON;
203
204 // Like ScopedHandle except for HGLOBAL.
205 template<class T>
206 class ScopedHGlobal {
207 public:
208 explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
209 data_ = static_cast<T*>(GlobalLock(glob_));
210 }
211 ~ScopedHGlobal() {
212 GlobalUnlock(glob_);
213 }
214
215 T* get() { return data_; }
216
217 size_t Size() const { return GlobalSize(glob_); }
218
219 T* operator->() const {
220 assert(data_ != 0);
221 return data_;
222 }
223
224 T* release() {
225 T* data = data_;
226 data_ = NULL;
227 return data;
228 }
229
230 private:
231 HGLOBAL glob_;
232
233 T* data_;
234
235 DISALLOW_COPY_AND_ASSIGN(ScopedHGlobal);
236 };
237
238 #endif // BASE_SCOPED_HANDLE_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698