diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-13 19:52:38 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-13 19:52:38 +0000 |
commit | 460ab5535d2eceb044240c3af4e8721b792c76d6 (patch) | |
tree | b895c01cd452a243029e8884d2b36275bbfaa5cc /base | |
parent | f4881dc8d74047afea3a472d5894131ee3f932c4 (diff) | |
download | chromium_src-460ab5535d2eceb044240c3af4e8721b792c76d6.zip chromium_src-460ab5535d2eceb044240c3af4e8721b792c76d6.tar.gz chromium_src-460ab5535d2eceb044240c3af4e8721b792c76d6.tar.bz2 |
Add a templatized scoped GDI handle object. Use this to implement ScopedBitmap and ScopedHRGN.
Review URL: http://codereview.chromium.org/7275
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3305 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/scoped_handle.h | 63 |
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> |