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

Side by Side Diff: base/scoped_handle.h

Issue 7275: Add a templatized scoped GDI handle object. Use this to implement ScopedBitma... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // 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 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_H__ 5 #ifndef BASE_SCOPED_HANDLE_H__
6 #define BASE_SCOPED_HANDLE_H__ 6 #define BASE_SCOPED_HANDLE_H__
7 7
8 #include <windows.h> 8 #include <windows.h>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 private: 126 private:
127 void Close() { 127 void Close() {
128 if (hdc_) 128 if (hdc_)
129 DeleteDC(hdc_); 129 DeleteDC(hdc_);
130 } 130 }
131 131
132 HDC hdc_; 132 HDC hdc_;
133 DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC); 133 DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC);
134 }; 134 };
135 135
136 // Like ScopedHandle but for HBITMAP. 136 // Like ScopedHandle but for GDI objects.
137 class ScopedBitmap { 137 template<class T>
138 class ScopedGDIObject {
138 public: 139 public:
139 ScopedBitmap() : hbitmap_(NULL) { } 140 ScopedGDIObject() : object_(NULL) {}
140 explicit ScopedBitmap(HBITMAP h) : hbitmap_(h) { } 141 explicit ScopedGDIObject(T object) : object_(object) {}
141 142
142 ~ScopedBitmap() { 143 ~ScopedGDIObject() {
143 Close(); 144 Close();
144 } 145 }
145 146
146 HBITMAP Get() { 147 T Get() {
147 return hbitmap_; 148 return object_;
148 } 149 }
149 150
150 void Set(HBITMAP h) { 151 void Set(T object) {
151 Close(); 152 if (object_ && object != object_)
152 hbitmap_ = h; 153 Close();
154 object_ = object;
153 } 155 }
154 156
155 operator HBITMAP() { return hbitmap_; } 157 ScopedGDIObject& operator=(T object) {
158 Set(object);
159 return *this;
160 }
161
162 operator T() { return object_; }
156 163
157 private: 164 private:
158 void Close() { 165 void Close() {
159 if (hbitmap_) 166 if (object_)
160 DeleteObject(hbitmap_); 167 DeleteObject(object_);
161 } 168 }
162 169
163 HBITMAP hbitmap_; 170 T object_;
164 DISALLOW_EVIL_CONSTRUCTORS(ScopedBitmap); 171 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
165 }; 172 };
166 173
167 // Like ScopedHandle but for HRGN. 174 // Typedefs for some common use cases.
168 class ScopedHRGN { 175 typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
169 public: 176 typedef ScopedGDIObject<HRGN> ScopedHRGN;
170 explicit ScopedHRGN(HRGN h) : hrgn_(h) { } 177 typedef ScopedGDIObject<HFONT> ScopedHFONT;
171 178
172 ~ScopedHRGN() {
173 if (hrgn_)
174 DeleteObject(hrgn_);
175 }
176
177 operator HRGN() { return hrgn_; }
178
179 ScopedHRGN& operator=(HRGN hrgn) {
180 if (hrgn_ && hrgn != hrgn_)
181 DeleteObject(hrgn_);
182 hrgn_ = hrgn;
183 return *this;
184 }
185
186 private:
187 HRGN hrgn_;
188 DISALLOW_EVIL_CONSTRUCTORS(ScopedHRGN);
189 };
190 179
191 // Like ScopedHandle except for HGLOBAL. 180 // Like ScopedHandle except for HGLOBAL.
192 template<class T> 181 template<class T>
193 class ScopedHGlobal { 182 class ScopedHGlobal {
194 public: 183 public:
195 explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) { 184 explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
196 data_ = static_cast<T*>(GlobalLock(glob_)); 185 data_ = static_cast<T*>(GlobalLock(glob_));
197 } 186 }
198 ~ScopedHGlobal() { 187 ~ScopedHGlobal() {
199 GlobalUnlock(glob_); 188 GlobalUnlock(glob_);
(...skipping 11 matching lines...) Expand all
211 private: 200 private:
212 HGLOBAL glob_; 201 HGLOBAL glob_;
213 202
214 T* data_; 203 T* data_;
215 204
216 DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal); 205 DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal);
217 }; 206 };
218 207
219 #endif // BASE_SCOPED_HANDLE_H__ 208 #endif // BASE_SCOPED_HANDLE_H__
220 209
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698