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