OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_SCOPED_HANDLE_WIN_H_ |
6 #define BASE_SCOPED_HANDLE_WIN_H_ | 6 #define BASE_SCOPED_HANDLE_WIN_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 | 12 |
13 // Used so we always remember to close the handle. Example: | 13 // Used so we always remember to close the handle. |
| 14 // The class interface matches that of ScopedStdioHandle in addition to an |
| 15 // IsValid() method since invalid handles on windows can be either NULL or |
| 16 // INVALID_HANDLE_VALUE (-1). |
| 17 // |
| 18 // Example: |
14 // ScopedHandle hfile(CreateFile(...)); | 19 // ScopedHandle hfile(CreateFile(...)); |
15 // if (!hfile.Get()) | 20 // if (!hfile.Get()) |
16 // ...process error | 21 // ...process error |
17 // ReadFile(hfile.Get(), ...); | 22 // ReadFile(hfile.Get(), ...); |
18 // | 23 // |
19 // To sqirrel the handle away somewhere else: | 24 // To sqirrel the handle away somewhere else: |
20 // secret_handle_ = hfile.Take(); | 25 // secret_handle_ = hfile.Take(); |
21 // | 26 // |
22 // To explicitly close the handle: | 27 // To explicitly close the handle: |
23 // CloseHandle(hfile.Take()); | 28 // hfile.Close(); |
24 class ScopedHandle { | 29 class ScopedHandle { |
25 public: | 30 public: |
26 ScopedHandle() : handle_(NULL) { | 31 ScopedHandle() : handle_(NULL) { |
27 } | 32 } |
28 | 33 |
29 explicit ScopedHandle(HANDLE h) : handle_(NULL) { | 34 explicit ScopedHandle(HANDLE h) : handle_(NULL) { |
30 Set(h); | 35 Set(h); |
31 } | 36 } |
32 | 37 |
33 ~ScopedHandle() { | 38 ~ScopedHandle() { |
(...skipping 20 matching lines...) Expand all Loading... |
54 | 59 |
55 operator HANDLE() { return handle_; } | 60 operator HANDLE() { return handle_; } |
56 | 61 |
57 HANDLE Take() { | 62 HANDLE Take() { |
58 // transfers ownership away from this object | 63 // transfers ownership away from this object |
59 HANDLE h = handle_; | 64 HANDLE h = handle_; |
60 handle_ = NULL; | 65 handle_ = NULL; |
61 return h; | 66 return h; |
62 } | 67 } |
63 | 68 |
64 private: | |
65 void Close() { | 69 void Close() { |
66 if (handle_) { | 70 if (handle_) { |
67 if (!::CloseHandle(handle_)) { | 71 if (!::CloseHandle(handle_)) { |
68 NOTREACHED(); | 72 NOTREACHED(); |
69 } | 73 } |
70 handle_ = NULL; | 74 handle_ = NULL; |
71 } | 75 } |
72 } | 76 } |
73 | 77 |
| 78 private: |
74 HANDLE handle_; | 79 HANDLE handle_; |
75 DISALLOW_EVIL_CONSTRUCTORS(ScopedHandle); | 80 DISALLOW_EVIL_CONSTRUCTORS(ScopedHandle); |
76 }; | 81 }; |
77 | 82 |
78 // Like ScopedHandle, but for HANDLEs returned from FindFile(). | 83 // Like ScopedHandle, but for HANDLEs returned from FindFile(). |
79 class ScopedFindFileHandle { | 84 class ScopedFindFileHandle { |
80 public: | 85 public: |
81 explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) { | 86 explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) { |
82 // Windows is inconsistent about invalid handles, so we always use NULL | 87 // Windows is inconsistent about invalid handles, so we always use NULL |
83 if (handle_ == INVALID_HANDLE_VALUE) | 88 if (handle_ == INVALID_HANDLE_VALUE) |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 private: | 205 private: |
201 HGLOBAL glob_; | 206 HGLOBAL glob_; |
202 | 207 |
203 T* data_; | 208 T* data_; |
204 | 209 |
205 DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal); | 210 DISALLOW_EVIL_CONSTRUCTORS(ScopedHGlobal); |
206 }; | 211 }; |
207 | 212 |
208 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 213 #endif // BASE_SCOPED_HANDLE_WIN_H_ |
209 | 214 |
OLD | NEW |