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

Side by Side Diff: base/win/scoped_handle.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 #ifndef BASE_WIN_SCOPED_HANDLE_H_
6 #define BASE_SCOPED_HANDLE_WIN_H_ 6 #define BASE_WIN_SCOPED_HANDLE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <windows.h> 9 #include <windows.h>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 13
14 namespace base {
15 namespace win {
16
14 // Used so we always remember to close the handle. 17 // Used so we always remember to close the handle.
15 // The class interface matches that of ScopedStdioHandle in addition to an 18 // The class interface matches that of ScopedStdioHandle in addition to an
16 // IsValid() method since invalid handles on windows can be either NULL or 19 // IsValid() method since invalid handles on windows can be either NULL or
17 // INVALID_HANDLE_VALUE (-1). 20 // INVALID_HANDLE_VALUE (-1).
18 // 21 //
19 // Example: 22 // Example:
20 // ScopedHandle hfile(CreateFile(...)); 23 // ScopedHandle hfile(CreateFile(...));
21 // if (!hfile.Get()) 24 // if (!hfile.Get())
22 // ...process error 25 // ...process error
23 // ReadFile(hfile.Get(), ...); 26 // ReadFile(hfile.Get(), ...);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 77 }
75 handle_ = NULL; 78 handle_ = NULL;
76 } 79 }
77 } 80 }
78 81
79 private: 82 private:
80 HANDLE handle_; 83 HANDLE handle_;
81 DISALLOW_COPY_AND_ASSIGN(ScopedHandle); 84 DISALLOW_COPY_AND_ASSIGN(ScopedHandle);
82 }; 85 };
83 86
84 // Like ScopedHandle, but for HANDLEs returned from FindFile(). 87 } // namespace win
85 class ScopedFindFileHandle { 88 } // namespace base
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 89
238 #endif // BASE_SCOPED_HANDLE_WIN_H_ 90 #endif // BASE_SCOPED_HANDLE_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698