OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_MAC_SCOPED_IOOBJECT_H_ | 5 #ifndef BASE_MAC_SCOPED_IOOBJECT_H_ |
6 #define BASE_MAC_SCOPED_IOOBJECT_H_ | 6 #define BASE_MAC_SCOPED_IOOBJECT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <IOKit/IOKitLib.h> | 9 #include <IOKit/IOKitLib.h> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace mac { | 15 namespace mac { |
16 | 16 |
17 // Just like ScopedCFTypeRef but for io_object_t and subclasses. | 17 // Just like ScopedCFTypeRef but for io_object_t and subclasses. |
18 template<typename IOT> | 18 template<typename IOT> |
19 class ScopedIOObject { | 19 class ScopedIOObject { |
20 public: | 20 public: |
21 typedef IOT element_type; | 21 typedef IOT element_type; |
22 | 22 |
23 explicit ScopedIOObject(IOT object = NULL) | 23 explicit ScopedIOObject(IOT object = IO_OBJECT_NULL) |
24 : object_(object) { | 24 : object_(object) { |
25 } | 25 } |
26 | 26 |
27 ~ScopedIOObject() { | 27 ~ScopedIOObject() { |
28 if (object_) | 28 if (object_) |
29 IOObjectRelease(object_); | 29 IOObjectRelease(object_); |
30 } | 30 } |
31 | 31 |
32 void reset(IOT object = NULL) { | 32 void reset(IOT object = IO_OBJECT_NULL) { |
33 if (object_) | 33 if (object_) |
34 IOObjectRelease(object_); | 34 IOObjectRelease(object_); |
35 object_ = object; | 35 object_ = object; |
36 } | 36 } |
37 | 37 |
38 bool operator==(IOT that) const { | 38 bool operator==(IOT that) const { |
39 return object_ == that; | 39 return object_ == that; |
40 } | 40 } |
41 | 41 |
42 bool operator!=(IOT that) const { | 42 bool operator!=(IOT that) const { |
43 return object_ != that; | 43 return object_ != that; |
44 } | 44 } |
45 | 45 |
46 operator IOT() const { | 46 operator IOT() const { |
47 return object_; | 47 return object_; |
48 } | 48 } |
49 | 49 |
50 IOT get() const { | 50 IOT get() const { |
51 return object_; | 51 return object_; |
52 } | 52 } |
53 | 53 |
54 void swap(ScopedIOObject& that) { | 54 void swap(ScopedIOObject& that) { |
55 IOT temp = that.object_; | 55 IOT temp = that.object_; |
56 that.object_ = object_; | 56 that.object_ = object_; |
57 object_ = temp; | 57 object_ = temp; |
58 } | 58 } |
59 | 59 |
60 IOT release() WARN_UNUSED_RESULT { | 60 IOT release() WARN_UNUSED_RESULT { |
61 IOT temp = object_; | 61 IOT temp = object_; |
62 object_ = NULL; | 62 object_ = IO_OBJECT_NULL; |
63 return temp; | 63 return temp; |
64 } | 64 } |
65 | 65 |
66 private: | 66 private: |
67 IOT object_; | 67 IOT object_; |
68 | 68 |
69 DISALLOW_COPY_AND_ASSIGN(ScopedIOObject); | 69 DISALLOW_COPY_AND_ASSIGN(ScopedIOObject); |
70 }; | 70 }; |
71 | 71 |
72 } // namespace mac | 72 } // namespace mac |
73 } // namespace base | 73 } // namespace base |
74 | 74 |
75 #endif // BASE_MAC_SCOPED_IOOBJECT_H_ | 75 #endif // BASE_MAC_SCOPED_IOOBJECT_H_ |
OLD | NEW |