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_IOPLUGININTERFACE_H_
6#define BASE_MAC_SCOPED_IOPLUGININTERFACE_H_
7
8#include <IOKit/IOKitLib.h>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12
13namespace base {
14namespace mac {
15
16// Just like ScopedCFTypeRef but for IOCFPlugInInterface and friends
17// (IOUSBInterfaceStruct and IOUSBDeviceStruct320 in particular).
18template<typename T>
19class ScopedIOPluginInterface {
20 public:
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
73}  // namespace mac
74}  // namespace base
75
76#endif  // BASE_MAC_SCOPED_IOPLUGININTERFACE_H_
77