Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: base/win/scoped_handle.h

Issue 9480026: Added ScopedPrinterHandle (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | printing/backend/win_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_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
(...skipping 12 matching lines...) Expand all
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 {
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 HandleTraits {
94 public:
95 static bool CloseHandle(HANDLE handle) {
96 return ::CloseHandle(handle) != FALSE;
97 }
98 };
99
100 typedef GenericScopedHandle<HandleTraits> 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_
OLDNEW
« no previous file with comments | « no previous file | printing/backend/win_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698