Home | History | Annotate | Download | only in mac
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_MAC_SCOPED_IOOBJECT_H_
      6 #define BASE_MAC_SCOPED_IOOBJECT_H_
      7 
      8 #include <IOKit/IOKitLib.h>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 
     13 namespace base {
     14 namespace mac {
     15 
     16 // Just like ScopedCFTypeRef but for io_object_t and subclasses.
     17 template<typename IOT>
     18 class ScopedIOObject {
     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 
     71 }  // namespace mac
     72 }  // namespace base
     73 
     74 #endif  // BASE_MAC_SCOPED_IOOBJECT_H_
     75