OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_WIN_SCOPED_GDI_OBJECT_H_ | 5 #ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_ |
6 #define BASE_WIN_SCOPED_GDI_OBJECT_H_ | 6 #define BASE_WIN_SCOPED_GDI_OBJECT_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/scoped_generic.h" |
11 #include "base/logging.h" | |
12 | 11 |
13 namespace base { | 12 namespace base { |
14 namespace win { | 13 namespace win { |
15 | 14 |
16 // Like ScopedHandle but for GDI objects. | 15 namespace internal { |
17 template<class T> | |
18 class ScopedGDIObject { | |
19 public: | |
20 ScopedGDIObject() : object_(NULL) {} | |
21 explicit ScopedGDIObject(T object) : object_(object) {} | |
22 | 16 |
23 ~ScopedGDIObject() { | 17 template <class T> |
24 Close(); | 18 struct ScopedGDIObjectTraits { |
25 } | 19 static T InvalidValue() { return nullptr; } |
26 | 20 static void Free(T object) { DeleteObject(object); } |
27 T Get() { | |
28 return object_; | |
29 } | |
30 | |
31 void Set(T object) { | |
32 if (object_ && object != object_) | |
33 Close(); | |
34 object_ = object; | |
35 } | |
36 | |
37 ScopedGDIObject& operator=(T object) { | |
38 Set(object); | |
39 return *this; | |
40 } | |
41 | |
42 T release() { | |
43 T object = object_; | |
44 object_ = NULL; | |
45 return object; | |
46 } | |
47 | |
48 operator T() { return object_; } | |
49 | |
50 private: | |
51 void Close() { | |
52 if (object_) | |
53 DeleteObject(object_); | |
54 } | |
55 | |
56 T object_; | |
57 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject); | |
58 }; | 21 }; |
59 | 22 |
60 // An explicit specialization for HICON because we have to call DestroyIcon() | 23 // An explicit specialization for HICON because we have to call DestroyIcon() |
61 // instead of DeleteObject() for HICON. | 24 // instead of DeleteObject() for HICON. |
62 template<> | 25 template <> |
63 void inline ScopedGDIObject<HICON>::Close() { | 26 void inline ScopedGDIObjectTraits<HICON>::Free(HICON icon) { |
64 if (object_) | 27 DestroyIcon(icon); |
65 DestroyIcon(object_); | |
66 } | 28 } |
67 | 29 |
| 30 } // namespace internal |
| 31 |
| 32 // Like ScopedHandle but for GDI objects. |
| 33 template <class T> |
| 34 using ScopedGDIObject = ScopedGeneric<T, internal::ScopedGDIObjectTraits<T>>; |
| 35 |
68 // Typedefs for some common use cases. | 36 // Typedefs for some common use cases. |
69 typedef ScopedGDIObject<HBITMAP> ScopedBitmap; | 37 typedef ScopedGDIObject<HBITMAP> ScopedBitmap; |
70 typedef ScopedGDIObject<HRGN> ScopedRegion; | 38 typedef ScopedGDIObject<HRGN> ScopedRegion; |
71 typedef ScopedGDIObject<HFONT> ScopedHFONT; | 39 typedef ScopedGDIObject<HFONT> ScopedHFONT; |
72 typedef ScopedGDIObject<HICON> ScopedHICON; | 40 typedef ScopedGDIObject<HICON> ScopedHICON; |
73 | 41 |
74 } // namespace win | 42 } // namespace win |
75 } // namespace base | 43 } // namespace base |
76 | 44 |
77 #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_ | 45 #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_ |
OLD | NEW |