Chromium Code Reviews| Index: base/win/scoped_handle.h |
| diff --git a/base/win/scoped_handle.h b/base/win/scoped_handle.h |
| index 3bb0279648aef9c785a440659c2336b893606ce4..45cab9a53bfe59297debdb37a7de1979ce9a3fcf 100644 |
| --- a/base/win/scoped_handle.h |
| +++ b/base/win/scoped_handle.h |
| @@ -30,16 +30,17 @@ namespace win { |
| // |
| // To explicitly close the handle: |
| // hfile.Close(); |
| -class ScopedHandle { |
| +template <class Traits> |
| +class GenericScopedHandle { |
|
alexeypa (please no reviews)
2012/03/01 21:37:43
Consider implementing it as
template <class T, cl
Vitaly Buka (NO REVIEWS)
2012/03/01 22:14:42
I thought about that. But I'd like to add feature
|
| public: |
| - ScopedHandle() : handle_(NULL) { |
| + GenericScopedHandle() : handle_(NULL) { |
| } |
| - explicit ScopedHandle(HANDLE h) : handle_(NULL) { |
| + explicit GenericScopedHandle(HANDLE h) : handle_(NULL) { |
| Set(h); |
| } |
| - ~ScopedHandle() { |
| + ~GenericScopedHandle() { |
| Close(); |
| } |
| @@ -63,6 +64,11 @@ class ScopedHandle { |
| operator HANDLE() { return handle_; } |
| + HANDLE* Receive() { |
| + DCHECK(!handle_) << "Handle must be NULL"; |
| + return &handle_; |
| + } |
| + |
| HANDLE Take() { |
| // transfers ownership away from this object |
| HANDLE h = handle_; |
| @@ -72,7 +78,7 @@ class ScopedHandle { |
| void Close() { |
| if (handle_) { |
| - if (!::CloseHandle(handle_)) { |
| + if (!Traits::CloseHandle(handle_)) { |
| NOTREACHED(); |
| } |
| handle_ = NULL; |
| @@ -81,9 +87,18 @@ class ScopedHandle { |
| private: |
| HANDLE handle_; |
| - DISALLOW_COPY_AND_ASSIGN(ScopedHandle); |
| + DISALLOW_COPY_AND_ASSIGN(GenericScopedHandle); |
| +}; |
| + |
| +class DefaultHandleTraits { |
|
alexeypa (please no reviews)
2012/03/01 21:37:43
Consider renaming it to HandleTraits. It is in a W
Vitaly Buka (NO REVIEWS)
2012/03/01 22:14:42
Done.
|
| + public: |
| + static bool CloseHandle(HANDLE handle) { |
| + return ::CloseHandle(handle) != FALSE; |
| + } |
| }; |
| +typedef GenericScopedHandle<DefaultHandleTraits> ScopedHandle; |
| + |
| } // namespace win |
| } // namespace base |