summaryrefslogtreecommitdiffstats
path: root/dom/media/platforms/apple/AppleUtils.h
blob: 9e30aff86e1b39ee5f359118c980ea32a044bfdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Utility functions to help with Apple API calls.

#ifndef mozilla_AppleUtils_h
#define mozilla_AppleUtils_h

#include "mozilla/Attributes.h"

namespace mozilla {

// Wrapper class to call CFRelease on reference types
// when they go out of scope.
template <class T>
class AutoCFRelease {
public:
  MOZ_IMPLICIT AutoCFRelease(T aRef)
    : mRef(aRef)
  {
  }
  ~AutoCFRelease()
  {
    if (mRef) {
      CFRelease(mRef);
    }
  }
  // Return the wrapped ref so it can be used as an in parameter.
  operator T()
  {
    return mRef;
  }
  // Return a pointer to the wrapped ref for use as an out parameter.
  T* receive()
  {
    return &mRef;
  }

private:
  // Copy operator isn't supported and is not implemented.
  AutoCFRelease<T>& operator=(const AutoCFRelease<T>&);
  T mRef;
};

// CFRefPtr: A CoreFoundation smart pointer.
template <class T>
class CFRefPtr {
public:
  explicit CFRefPtr(T aRef)
    : mRef(aRef)
  {
    if (mRef) {
      CFRetain(mRef);
    }
  }
  // Copy constructor.
  CFRefPtr(const CFRefPtr<T>& aCFRefPtr)
    : mRef(aCFRefPtr.mRef)
  {
    if (mRef) {
      CFRetain(mRef);
    }
  }
  // Copy operator
  CFRefPtr<T>& operator=(const CFRefPtr<T>& aCFRefPtr)
  {
    if (mRef == aCFRefPtr.mRef) {
      return;
    }
    if (mRef) {
      CFRelease(mRef);
    }
    mRef = aCFRefPtr.mRef;
    if (mRef) {
      CFRetain(mRef);
    }
    return *this;
  }
  ~CFRefPtr()
  {
    if (mRef) {
      CFRelease(mRef);
    }
  }
  // Return the wrapped ref so it can be used as an in parameter.
  operator T()
  {
    return mRef;
  }

private:
  T mRef;
};

} // namespace mozilla

#endif // mozilla_AppleUtils_h