OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_HANDLE_H_ | 5 #ifndef BASE_WIN_SCOPED_HANDLE_H_ |
6 #define BASE_WIN_SCOPED_HANDLE_H_ | 6 #define BASE_WIN_SCOPED_HANDLE_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 // automatically. The class interface follows the style of | 29 // automatically. The class interface follows the style of |
30 // the ScopedFILE class with two additions: | 30 // the ScopedFILE class with two additions: |
31 // - IsValid() method can tolerate multiple invalid handle values such as NULL | 31 // - IsValid() method can tolerate multiple invalid handle values such as NULL |
32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. | 32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. |
33 // - Set() (and the constructors and assignment operators that call it) | 33 // - Set() (and the constructors and assignment operators that call it) |
34 // preserve the Windows LastError code. This ensures that GetLastError() can | 34 // preserve the Windows LastError code. This ensures that GetLastError() can |
35 // be called after stashing a handle in a GenericScopedHandle object. Doing | 35 // be called after stashing a handle in a GenericScopedHandle object. Doing |
36 // this explicitly is necessary because of bug 528394 and VC++ 2015. | 36 // this explicitly is necessary because of bug 528394 and VC++ 2015. |
37 template <class Traits, class Verifier> | 37 template <class Traits, class Verifier> |
38 class GenericScopedHandle { | 38 class GenericScopedHandle { |
39 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle, RValue) | 39 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle) |
40 | 40 |
41 public: | 41 public: |
42 typedef typename Traits::Handle Handle; | 42 typedef typename Traits::Handle Handle; |
43 | 43 |
44 GenericScopedHandle() : handle_(Traits::NullHandle()) {} | 44 GenericScopedHandle() : handle_(Traits::NullHandle()) {} |
45 | 45 |
46 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { | 46 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { |
47 Set(handle); | 47 Set(handle); |
48 } | 48 } |
49 | 49 |
50 // Move constructor for C++03 move emulation of this type. | 50 GenericScopedHandle(GenericScopedHandle&& other) |
51 GenericScopedHandle(RValue other) : handle_(Traits::NullHandle()) { | 51 : handle_(Traits::NullHandle()) { |
52 Set(other.object->Take()); | 52 Set(other.Take()); |
53 } | 53 } |
54 | 54 |
55 ~GenericScopedHandle() { | 55 ~GenericScopedHandle() { |
56 Close(); | 56 Close(); |
57 } | 57 } |
58 | 58 |
59 bool IsValid() const { | 59 bool IsValid() const { |
60 return Traits::IsHandleValid(handle_); | 60 return Traits::IsHandleValid(handle_); |
61 } | 61 } |
62 | 62 |
63 // Move operator= for C++03 move emulation of this type. | 63 GenericScopedHandle& operator=(GenericScopedHandle&& other) { |
64 GenericScopedHandle& operator=(RValue other) { | 64 DCHECK_NE(this, &other); |
65 if (this != other.object) { | 65 Set(other.Take()); |
66 Set(other.object->Take()); | |
67 } | |
68 return *this; | 66 return *this; |
69 } | 67 } |
70 | 68 |
71 void Set(Handle handle) { | 69 void Set(Handle handle) { |
72 if (handle_ != handle) { | 70 if (handle_ != handle) { |
73 // Preserve old LastError to avoid bug 528394. | 71 // Preserve old LastError to avoid bug 528394. |
74 auto last_error = ::GetLastError(); | 72 auto last_error = ::GetLastError(); |
75 Close(); | 73 Close(); |
76 | 74 |
77 if (Traits::IsHandleValid(handle)) { | 75 if (Traits::IsHandleValid(handle)) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // This should be called whenever the OS is closing a handle, if extended | 173 // This should be called whenever the OS is closing a handle, if extended |
176 // verification of improper handle closing is desired. If |handle| is being | 174 // verification of improper handle closing is desired. If |handle| is being |
177 // tracked by the handle verifier and ScopedHandle is not the one closing it, | 175 // tracked by the handle verifier and ScopedHandle is not the one closing it, |
178 // a CHECK is generated. | 176 // a CHECK is generated. |
179 void BASE_EXPORT OnHandleBeingClosed(HANDLE handle); | 177 void BASE_EXPORT OnHandleBeingClosed(HANDLE handle); |
180 | 178 |
181 } // namespace win | 179 } // namespace win |
182 } // namespace base | 180 } // namespace base |
183 | 181 |
184 #endif // BASE_WIN_SCOPED_HANDLE_H_ | 182 #endif // BASE_WIN_SCOPED_HANDLE_H_ |
OLD | NEW |