summaryrefslogtreecommitdiffstats
path: root/base/scoped_handle.h
diff options
context:
space:
mode:
Diffstat (limited to 'base/scoped_handle.h')
-rw-r--r--base/scoped_handle.h63
1 files changed, 26 insertions, 37 deletions
diff --git a/base/scoped_handle.h b/base/scoped_handle.h
index 654d6fd..8ed568d 100644
--- a/base/scoped_handle.h
+++ b/base/scoped_handle.h
@@ -133,60 +133,49 @@ class ScopedHDC {
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) { }
-
- ~ScopedHRGN() {
- if (hrgn_)
- DeleteObject(hrgn_);
- }
-
- operator HRGN() { return hrgn_; }
+// Typedefs for some common use cases.
+typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
+typedef ScopedGDIObject<HRGN> ScopedHRGN;
+typedef ScopedGDIObject<HFONT> ScopedHFONT;
- 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>