summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/sdk/objc/helpers/scoped_cftyperef.h
blob: 092f02b3afec4db157bd5b6cd1b4c6edf2faa542 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 *
 */

#ifndef SDK_OBJC_HELPERS_SCOPED_CFTYPEREF_H_
#define SDK_OBJC_HELPERS_SCOPED_CFTYPEREF_H_

#include <CoreFoundation/CoreFoundation.h>
namespace rtc {

// RETAIN: ScopedTypeRef should retain the object when it takes
// ownership.
// ASSUME: Assume the object already has already been retained.
// ScopedTypeRef takes over ownership.
enum class RetainPolicy { RETAIN, ASSUME };

namespace internal {
template <typename T>
struct CFTypeRefTraits {
  static T InvalidValue() { return nullptr; }
  static void Release(T ref) { CFRelease(ref); }
  static T Retain(T ref) {
    CFRetain(ref);
    return ref;
  }
};

template <typename T, typename Traits>
class ScopedTypeRef {
 public:
  ScopedTypeRef() : ptr_(Traits::InvalidValue()) {}
  explicit ScopedTypeRef(T ptr) : ptr_(ptr) {}
  ScopedTypeRef(T ptr, RetainPolicy policy) : ScopedTypeRef(ptr) {
    if (ptr_ && policy == RetainPolicy::RETAIN)
      Traits::Retain(ptr_);
  }

  ScopedTypeRef(const ScopedTypeRef<T, Traits>& rhs) : ptr_(rhs.ptr_) {
    if (ptr_)
      ptr_ = Traits::Retain(ptr_);
  }

  ~ScopedTypeRef() {
    if (ptr_) {
      Traits::Release(ptr_);
    }
  }

  T get() const { return ptr_; }
  T operator->() const { return ptr_; }
  explicit operator bool() const { return ptr_; }

  bool operator!() const { return !ptr_; }

  ScopedTypeRef& operator=(const T& rhs) {
    if (ptr_)
      Traits::Release(ptr_);
    ptr_ = rhs;
    return *this;
  }

  ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& rhs) {
    reset(rhs.get(), RetainPolicy::RETAIN);
    return *this;
  }

  // This is intended to take ownership of objects that are
  // created by pass-by-pointer initializers.
  T* InitializeInto() {
    RTC_DCHECK(!ptr_);
    return &ptr_;
  }

  void reset(T ptr, RetainPolicy policy = RetainPolicy::ASSUME) {
    if (ptr && policy == RetainPolicy::RETAIN)
      Traits::Retain(ptr);
    if (ptr_)
      Traits::Release(ptr_);
    ptr_ = ptr;
  }

  T release() {
    T temp = ptr_;
    ptr_ = Traits::InvalidValue();
    return temp;
  }

 private:
  T ptr_;
};
}  // namespace internal

template <typename T>
using ScopedCFTypeRef =
    internal::ScopedTypeRef<T, internal::CFTypeRefTraits<T>>;

template <typename T>
static ScopedCFTypeRef<T> AdoptCF(T cftype) {
  return ScopedCFTypeRef<T>(cftype, RetainPolicy::RETAIN);
}

template <typename T>
static ScopedCFTypeRef<T> ScopedCF(T cftype) {
  return ScopedCFTypeRef<T>(cftype);
}

}  // namespace rtc

#endif  // SDK_OBJC_HELPERS_SCOPED_CFTYPEREF_H_