| 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_IOOBJECT_H_ | 5 #ifndef BASE_MAC_SCOPED_IOOBJECT_H_ |
| 6 #define BASE_MAC_SCOPED_IOOBJECT_H_ | 6 #define 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 |
| 15 namespace internal { |
| 16 |
| 17 template <typename IOT> |
| 18 struct ScopedIOObjectTraits { |
| 19 static IOT InvalidValue() { return IO_OBJECT_NULL; } |
| 20 static IOT Retain(IOT iot) { |
| 21 IOObjectRetain(iot); |
| 22 return iot; |
| 23 } |
| 24 static void Release(IOT iot) { IOObjectRelease(iot); } |
| 25 }; |
| 26 |
| 27 } // namespce internal |
| 28 |
| 16 // Just like ScopedCFTypeRef but for io_object_t and subclasses. | 29 // Just like ScopedCFTypeRef but for io_object_t and subclasses. |
| 17 template<typename IOT> | 30 template <typename IOT> |
| 18 class ScopedIOObject { | 31 using ScopedIOObject = ScopedTypeRef<IOT, internal::ScopedIOObjectTraits<IOT>>; |
| 19 public: | |
| 20 typedef IOT element_type; | |
| 21 | |
| 22 explicit ScopedIOObject(IOT object = IO_OBJECT_NULL) | |
| 23 : object_(object) { | |
| 24 } | |
| 25 | |
| 26 ~ScopedIOObject() { | |
| 27 if (object_) | |
| 28 IOObjectRelease(object_); | |
| 29 } | |
| 30 | |
| 31 void reset(IOT object = IO_OBJECT_NULL) { | |
| 32 if (object_) | |
| 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 | 32 |
| 71 } // namespace mac | 33 } // namespace mac |
| 72 } // namespace base | 34 } // namespace base |
| 73 | 35 |
| 74 #endif // BASE_MAC_SCOPED_IOOBJECT_H_ | 36 #endif // BASE_MAC_SCOPED_IOOBJECT_H_ |
| OLD | NEW |