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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/scoped_handle.h
===================================================================
--- base/scoped_handle.h (revision 3285)
+++ base/scoped_handle.h (working copy)
@@ -133,61 +133,50 @@
DISALLOW_EVIL_CONSTRUCTORS(ScopedHDC);
};
-// Like ScopedHandle but for HBITMAP.
-class ScopedBitmap {
+// Like ScopedHandle but for GDI objects.
+template<class T>
+class ScopedGDIObject {
public:
- ScopedBitmap() : hbitmap_(NULL) { }
- explicit ScopedBitmap(HBITMAP h) : hbitmap_(h) { }
+ ScopedGDIObject() : object_(NULL) {}
+ explicit ScopedGDIObject(T object) : object_(object) {}
- ~ScopedBitmap() {
+ ~ScopedGDIObject() {
Close();
}
- HBITMAP Get() {
- return hbitmap_;
+ T Get() {
+ return object_;
}
- void Set(HBITMAP h) {
- Close();
- hbitmap_ = h;
+ void Set(T object) {
+ if (object_ && object != object_)
+ Close();
+ object_ = object;
}
- operator HBITMAP() { return hbitmap_; }
+ ScopedGDIObject& operator=(T object) {
+ Set(object);
+ return *this;
+ }
+ operator T() { return object_; }
+
private:
void Close() {
- if (hbitmap_)
- DeleteObject(hbitmap_);
+ if (object_)
+ DeleteObject(object_);
}
- HBITMAP hbitmap_;
- DISALLOW_EVIL_CONSTRUCTORS(ScopedBitmap);
+ T object_;
+ DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
};
-// Like ScopedHandle but for HRGN.
-class ScopedHRGN {
- public:
- explicit ScopedHRGN(HRGN h) : hrgn_(h) { }
+// Typedefs for some common use cases.
+typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
+typedef ScopedGDIObject<HRGN> ScopedHRGN;
+typedef ScopedGDIObject<HFONT> ScopedHFONT;
- ~ScopedHRGN() {
- if (hrgn_)
- DeleteObject(hrgn_);
- }
- operator HRGN() { return hrgn_; }
-
- ScopedHRGN& operator=(HRGN hrgn) {
- if (hrgn_ && hrgn != hrgn_)
- DeleteObject(hrgn_);
- hrgn_ = hrgn;
- return *this;
- }
-
- private:
- HRGN hrgn_;
- DISALLOW_EVIL_CONSTRUCTORS(ScopedHRGN);
-};
-
// Like ScopedHandle except for HGLOBAL.
template<class T>
class ScopedHGlobal {
« 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