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_MAC_SCOPED_IOPLUGININTERFACE_H_ | 5 #ifndef BASE_MAC_SCOPED_IOPLUGININTERFACE_H_ |
6 #define BASE_MAC_SCOPED_IOPLUGININTERFACE_H_ | 6 #define BASE_MAC_SCOPED_IOPLUGININTERFACE_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 |
| 15 namespace internal { |
| 16 |
| 17 template <typename T> |
| 18 struct ScopedIOPluginInterfaceTraits { |
| 19 static T InvalidValue() { return nullptr; } |
| 20 static T Retain(T t) { |
| 21 (*t)->AddRef(t); |
| 22 return t; |
| 23 } |
| 24 static void Release(T t) { (*t)->Release(t); } |
| 25 }; |
| 26 |
| 27 } // namespace internal |
| 28 |
16 // Just like ScopedCFTypeRef but for IOCFPlugInInterface and friends | 29 // Just like ScopedCFTypeRef but for IOCFPlugInInterface and friends |
17 // (IOUSBInterfaceStruct and IOUSBDeviceStruct320 in particular). | 30 // (IOUSBInterfaceStruct and IOUSBDeviceStruct320 in particular). |
18 template<typename T> | 31 template <typename T> |
19 class ScopedIOPluginInterface { | 32 using ScopedIOPluginInterface = |
20 public: | 33 ScopedTypeRef<T**, internal::ScopedIOPluginInterfaceTraits<T**>>; |
21 typedef T** InterfaceT; | |
22 typedef InterfaceT element_type; | |
23 | |
24 explicit ScopedIOPluginInterface(InterfaceT object = NULL) | |
25 : object_(object) { | |
26 } | |
27 | |
28 ~ScopedIOPluginInterface() { | |
29 if (object_) | |
30 (*object_)->Release(object_); | |
31 } | |
32 | |
33 void reset(InterfaceT object = NULL) { | |
34 if (object_) | |
35 (*object_)->Release(object_); | |
36 object_ = object; | |
37 } | |
38 | |
39 bool operator==(InterfaceT that) const { | |
40 return object_ == that; | |
41 } | |
42 | |
43 bool operator!=(InterfaceT that) const { | |
44 return object_ != that; | |
45 } | |
46 | |
47 operator InterfaceT() const { | |
48 return object_; | |
49 } | |
50 | |
51 InterfaceT get() const { | |
52 return object_; | |
53 } | |
54 | |
55 void swap(ScopedIOPluginInterface& that) { | |
56 InterfaceT temp = that.object_; | |
57 that.object_ = object_; | |
58 object_ = temp; | |
59 } | |
60 | |
61 InterfaceT release() WARN_UNUSED_RESULT { | |
62 InterfaceT temp = object_; | |
63 object_ = NULL; | |
64 return temp; | |
65 } | |
66 | |
67 private: | |
68 InterfaceT object_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(ScopedIOPluginInterface); | |
71 }; | |
72 | 34 |
73 } // namespace mac | 35 } // namespace mac |
74 } // namespace base | 36 } // namespace base |
75 | 37 |
76 #endif // BASE_MAC_SCOPED_IOPLUGININTERFACE_H_ | 38 #endif // BASE_MAC_SCOPED_IOPLUGININTERFACE_H_ |
OLD | NEW |