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 { |