| 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_SCOPED_HANDLE_WIN_H_ | 5 #ifndef BASE_WIN_SCOPED_HGLOBAL_H_ |
| 6 #define BASE_SCOPED_HANDLE_WIN_H_ | 6 #define BASE_WIN_SCOPED_HGLOBAL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | |
| 13 | 12 |
| 14 // Used so we always remember to close the handle. | 13 namespace base { |
| 15 // The class interface matches that of ScopedStdioHandle in addition to an | 14 namespace win { |
| 16 // IsValid() method since invalid handles on windows can be either NULL or | |
| 17 // INVALID_HANDLE_VALUE (-1). | |
| 18 // | |
| 19 // Example: | |
| 20 // ScopedHandle hfile(CreateFile(...)); | |
| 21 // if (!hfile.Get()) | |
| 22 // ...process error | |
| 23 // ReadFile(hfile.Get(), ...); | |
| 24 // | |
| 25 // To sqirrel the handle away somewhere else: | |
| 26 // secret_handle_ = hfile.Take(); | |
| 27 // | |
| 28 // To explicitly close the handle: | |
| 29 // hfile.Close(); | |
| 30 class ScopedHandle { | |
| 31 public: | |
| 32 ScopedHandle() : handle_(NULL) { | |
| 33 } | |
| 34 | |
| 35 explicit ScopedHandle(HANDLE h) : handle_(NULL) { | |
| 36 Set(h); | |
| 37 } | |
| 38 | |
| 39 ~ScopedHandle() { | |
| 40 Close(); | |
| 41 } | |
| 42 | |
| 43 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL | |
| 44 // usage for errors. | |
| 45 bool IsValid() const { | |
| 46 return handle_ != NULL; | |
| 47 } | |
| 48 | |
| 49 void Set(HANDLE new_handle) { | |
| 50 Close(); | |
| 51 | |
| 52 // Windows is inconsistent about invalid handles, so we always use NULL | |
| 53 if (new_handle != INVALID_HANDLE_VALUE) | |
| 54 handle_ = new_handle; | |
| 55 } | |
| 56 | |
| 57 HANDLE Get() { | |
| 58 return handle_; | |
| 59 } | |
| 60 | |
| 61 operator HANDLE() { return handle_; } | |
| 62 | |
| 63 HANDLE Take() { | |
| 64 // transfers ownership away from this object | |
| 65 HANDLE h = handle_; | |
| 66 handle_ = NULL; | |
| 67 return h; | |
| 68 } | |
| 69 | |
| 70 void Close() { | |
| 71 if (handle_) { | |
| 72 if (!::CloseHandle(handle_)) { | |
| 73 NOTREACHED(); | |
| 74 } | |
| 75 handle_ = NULL; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 HANDLE handle_; | |
| 81 DISALLOW_COPY_AND_ASSIGN(ScopedHandle); | |
| 82 }; | |
| 83 | |
| 84 // Like ScopedHandle, but for HANDLEs returned from FindFile(). | |
| 85 class ScopedFindFileHandle { | |
| 86 public: | |
| 87 explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) { | |
| 88 // Windows is inconsistent about invalid handles, so we always use NULL | |
| 89 if (handle_ == INVALID_HANDLE_VALUE) | |
| 90 handle_ = NULL; | |
| 91 } | |
| 92 | |
| 93 ~ScopedFindFileHandle() { | |
| 94 if (handle_) | |
| 95 FindClose(handle_); | |
| 96 } | |
| 97 | |
| 98 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL | |
| 99 // usage for errors. | |
| 100 bool IsValid() const { return handle_ != NULL; } | |
| 101 | |
| 102 operator HANDLE() { return handle_; } | |
| 103 | |
| 104 private: | |
| 105 HANDLE handle_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(ScopedFindFileHandle); | |
| 108 }; | |
| 109 | |
| 110 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | |
| 111 // CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead. | |
| 112 class ScopedHDC { | |
| 113 public: | |
| 114 ScopedHDC() : hdc_(NULL) { } | |
| 115 explicit ScopedHDC(HDC h) : hdc_(h) { } | |
| 116 | |
| 117 ~ScopedHDC() { | |
| 118 Close(); | |
| 119 } | |
| 120 | |
| 121 HDC Get() { | |
| 122 return hdc_; | |
| 123 } | |
| 124 | |
| 125 void Set(HDC h) { | |
| 126 Close(); | |
| 127 hdc_ = h; | |
| 128 } | |
| 129 | |
| 130 operator HDC() { return hdc_; } | |
| 131 | |
| 132 private: | |
| 133 void Close() { | |
| 134 #ifdef NOGDI | |
| 135 assert(false); | |
| 136 #else | |
| 137 if (hdc_) | |
| 138 DeleteDC(hdc_); | |
| 139 #endif // NOGDI | |
| 140 } | |
| 141 | |
| 142 HDC hdc_; | |
| 143 DISALLOW_COPY_AND_ASSIGN(ScopedHDC); | |
| 144 }; | |
| 145 | |
| 146 // Like ScopedHandle but for GDI objects. | |
| 147 template<class T> | |
| 148 class ScopedGDIObject { | |
| 149 public: | |
| 150 ScopedGDIObject() : object_(NULL) {} | |
| 151 explicit ScopedGDIObject(T object) : object_(object) {} | |
| 152 | |
| 153 ~ScopedGDIObject() { | |
| 154 Close(); | |
| 155 } | |
| 156 | |
| 157 T Get() { | |
| 158 return object_; | |
| 159 } | |
| 160 | |
| 161 void Set(T object) { | |
| 162 if (object_ && object != object_) | |
| 163 Close(); | |
| 164 object_ = object; | |
| 165 } | |
| 166 | |
| 167 ScopedGDIObject& operator=(T object) { | |
| 168 Set(object); | |
| 169 return *this; | |
| 170 } | |
| 171 | |
| 172 T release() { | |
| 173 T object = object_; | |
| 174 object_ = NULL; | |
| 175 return object; | |
| 176 } | |
| 177 | |
| 178 operator T() { return object_; } | |
| 179 | |
| 180 private: | |
| 181 void Close() { | |
| 182 if (object_) | |
| 183 DeleteObject(object_); | |
| 184 } | |
| 185 | |
| 186 T object_; | |
| 187 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject); | |
| 188 }; | |
| 189 | |
| 190 // An explicit specialization for HICON because we have to call DestroyIcon() | |
| 191 // instead of DeleteObject() for HICON. | |
| 192 template<> | |
| 193 void ScopedGDIObject<HICON>::Close() { | |
| 194 if (object_) | |
| 195 DestroyIcon(object_); | |
| 196 } | |
| 197 | |
| 198 // Typedefs for some common use cases. | |
| 199 typedef ScopedGDIObject<HBITMAP> ScopedBitmap; | |
| 200 typedef ScopedGDIObject<HRGN> ScopedRegion; | |
| 201 typedef ScopedGDIObject<HFONT> ScopedHFONT; | |
| 202 typedef ScopedGDIObject<HICON> ScopedHICON; | |
| 203 | 15 |
| 204 // Like ScopedHandle except for HGLOBAL. | 16 // Like ScopedHandle except for HGLOBAL. |
| 205 template<class T> | 17 template<class T> |
| 206 class ScopedHGlobal { | 18 class ScopedHGlobal { |
| 207 public: | 19 public: |
| 208 explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) { | 20 explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) { |
| 209 data_ = static_cast<T*>(GlobalLock(glob_)); | 21 data_ = static_cast<T*>(GlobalLock(glob_)); |
| 210 } | 22 } |
| 211 ~ScopedHGlobal() { | 23 ~ScopedHGlobal() { |
| 212 GlobalUnlock(glob_); | 24 GlobalUnlock(glob_); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 228 } | 40 } |
| 229 | 41 |
| 230 private: | 42 private: |
| 231 HGLOBAL glob_; | 43 HGLOBAL glob_; |
| 232 | 44 |
| 233 T* data_; | 45 T* data_; |
| 234 | 46 |
| 235 DISALLOW_COPY_AND_ASSIGN(ScopedHGlobal); | 47 DISALLOW_COPY_AND_ASSIGN(ScopedHGlobal); |
| 236 }; | 48 }; |
| 237 | 49 |
| 238 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 50 } // namespace win |
| 51 } // namespace base |
| 52 |
| 53 #endif // BASE_WIN_SCOPED_HGLOBAL_H_ |
| OLD | NEW |