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

Side by Side Diff: base/win/scoped_gdi_object.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_GDI_OBJECT_H_
6 #define BASE_SCOPED_HANDLE_WIN_H_ 6 #define BASE_WIN_SCOPED_GDI_OBJECT_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 // Used so we always remember to close the handle. 14 namespace base {
15 // The class interface matches that of ScopedStdioHandle in addition to an 15 namespace win {
16 // IsValid() method since invalid handles on windows can be either NULL or
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 16
146 // Like ScopedHandle but for GDI objects. 17 // Like ScopedHandle but for GDI objects.
147 template<class T> 18 template<class T>
148 class ScopedGDIObject { 19 class ScopedGDIObject {
149 public: 20 public:
150 ScopedGDIObject() : object_(NULL) {} 21 ScopedGDIObject() : object_(NULL) {}
151 explicit ScopedGDIObject(T object) : object_(object) {} 22 explicit ScopedGDIObject(T object) : object_(object) {}
152 23
153 ~ScopedGDIObject() { 24 ~ScopedGDIObject() {
154 Close(); 25 Close();
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 if (object_) 65 if (object_)
195 DestroyIcon(object_); 66 DestroyIcon(object_);
196 } 67 }
197 68
198 // Typedefs for some common use cases. 69 // Typedefs for some common use cases.
199 typedef ScopedGDIObject<HBITMAP> ScopedBitmap; 70 typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
200 typedef ScopedGDIObject<HRGN> ScopedRegion; 71 typedef ScopedGDIObject<HRGN> ScopedRegion;
201 typedef ScopedGDIObject<HFONT> ScopedHFONT; 72 typedef ScopedGDIObject<HFONT> ScopedHFONT;
202 typedef ScopedGDIObject<HICON> ScopedHICON; 73 typedef ScopedGDIObject<HICON> ScopedHICON;
203 74
204 // Like ScopedHandle except for HGLOBAL. 75 } // namespace win
205 template<class T> 76 } // namespace base
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 77
215 T* get() { return data_; } 78 #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_
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