Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
|
Albert Bodenhamer
2012/03/02 21:14:03
Update copyright
| |
| 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_HANDLE_H_ | 5 #ifndef BASE_WIN_SCOPED_HANDLE_H_ |
| 6 #define BASE_WIN_SCOPED_HANDLE_H_ | 6 #define BASE_WIN_SCOPED_HANDLE_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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 23 // ScopedHandle hfile(CreateFile(...)); | 23 // ScopedHandle hfile(CreateFile(...)); |
| 24 // if (!hfile.Get()) | 24 // if (!hfile.Get()) |
| 25 // ...process error | 25 // ...process error |
| 26 // ReadFile(hfile.Get(), ...); | 26 // ReadFile(hfile.Get(), ...); |
| 27 // | 27 // |
| 28 // To sqirrel the handle away somewhere else: | 28 // To sqirrel the handle away somewhere else: |
| 29 // secret_handle_ = hfile.Take(); | 29 // secret_handle_ = hfile.Take(); |
| 30 // | 30 // |
| 31 // To explicitly close the handle: | 31 // To explicitly close the handle: |
| 32 // hfile.Close(); | 32 // hfile.Close(); |
| 33 class ScopedHandle { | 33 template <class Traits> |
| 34 class GenericScopedHandle { | |
|
Albert Bodenhamer
2012/03/02 21:14:03
If you need to change ScopedHandle it's probably w
| |
| 34 public: | 35 public: |
| 35 ScopedHandle() : handle_(NULL) { | 36 GenericScopedHandle() : handle_(NULL) { |
| 36 } | 37 } |
| 37 | 38 |
| 38 explicit ScopedHandle(HANDLE h) : handle_(NULL) { | 39 explicit GenericScopedHandle(HANDLE h) : handle_(NULL) { |
| 39 Set(h); | 40 Set(h); |
| 40 } | 41 } |
| 41 | 42 |
| 42 ~ScopedHandle() { | 43 ~GenericScopedHandle() { |
| 43 Close(); | 44 Close(); |
| 44 } | 45 } |
| 45 | 46 |
| 46 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL | 47 // Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL |
| 47 // usage for errors. | 48 // usage for errors. |
| 48 bool IsValid() const { | 49 bool IsValid() const { |
| 49 return handle_ != NULL; | 50 return handle_ != NULL; |
| 50 } | 51 } |
| 51 | 52 |
| 52 void Set(HANDLE new_handle) { | 53 void Set(HANDLE new_handle) { |
| 53 Close(); | 54 Close(); |
| 54 | 55 |
| 55 // Windows is inconsistent about invalid handles, so we always use NULL | 56 // Windows is inconsistent about invalid handles, so we always use NULL |
| 56 if (new_handle != INVALID_HANDLE_VALUE) | 57 if (new_handle != INVALID_HANDLE_VALUE) |
| 57 handle_ = new_handle; | 58 handle_ = new_handle; |
| 58 } | 59 } |
| 59 | 60 |
| 60 HANDLE Get() { | 61 HANDLE Get() { |
| 61 return handle_; | 62 return handle_; |
| 62 } | 63 } |
| 63 | 64 |
| 64 operator HANDLE() { return handle_; } | 65 operator HANDLE() { return handle_; } |
| 65 | 66 |
| 67 HANDLE* Receive() { | |
| 68 DCHECK(!handle_) << "Handle must be NULL"; | |
| 69 return &handle_; | |
| 70 } | |
| 71 | |
| 66 HANDLE Take() { | 72 HANDLE Take() { |
| 67 // transfers ownership away from this object | 73 // transfers ownership away from this object |
| 68 HANDLE h = handle_; | 74 HANDLE h = handle_; |
| 69 handle_ = NULL; | 75 handle_ = NULL; |
| 70 return h; | 76 return h; |
| 71 } | 77 } |
| 72 | 78 |
| 73 void Close() { | 79 void Close() { |
| 74 if (handle_) { | 80 if (handle_) { |
| 75 if (!::CloseHandle(handle_)) { | 81 if (!Traits::CloseHandle(handle_)) { |
| 76 NOTREACHED(); | 82 NOTREACHED(); |
| 77 } | 83 } |
| 78 handle_ = NULL; | 84 handle_ = NULL; |
| 79 } | 85 } |
| 80 } | 86 } |
| 81 | 87 |
| 82 private: | 88 private: |
| 83 HANDLE handle_; | 89 HANDLE handle_; |
| 84 DISALLOW_COPY_AND_ASSIGN(ScopedHandle); | 90 DISALLOW_COPY_AND_ASSIGN(GenericScopedHandle); |
| 85 }; | 91 }; |
| 86 | 92 |
| 93 class DefaultHandleTraits { | |
| 94 public: | |
| 95 static bool CloseHandle(HANDLE handle) { | |
| 96 return ::CloseHandle(handle) != FALSE; | |
| 97 } | |
| 98 }; | |
| 99 | |
| 100 typedef GenericScopedHandle<DefaultHandleTraits> ScopedHandle; | |
| 101 | |
| 87 } // namespace win | 102 } // namespace win |
| 88 } // namespace base | 103 } // namespace base |
| 89 | 104 |
| 90 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 105 #endif // BASE_SCOPED_HANDLE_WIN_H_ |
| OLD | NEW |