admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#ifndef MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_
#define MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_
 
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/scoped_policy.h"
 
namespace base {
 
template <typename T>
struct ScopedTypeRefTraits;
 
template <typename T, typename Traits = ScopedTypeRefTraits<T>>
class ScopedTypeRef {
 public:
  typedef T element_type;
 
  ScopedTypeRef(
      T object = Traits::InvalidValue(),
      base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
      : object_(object) {
    if (object_ && policy == base::scoped_policy::RETAIN)
      object_ = Traits::Retain(object_);
  }
 
  ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) : object_(that.object_) {
    if (object_)
      object_ = Traits::Retain(object_);
  }
 
  ~ScopedTypeRef() {
    if (object_)
      Traits::Release(object_);
  }
 
  ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& that) {
    reset(that.get(), base::scoped_policy::RETAIN);
    return *this;
  }
 
  T* InitializeInto() WARN_UNUSED_RESULT {
    DCHECK(!object_);
    return &object_;
  }
 
  void reset(T object = Traits::InvalidValue(),
             base::scoped_policy::OwnershipPolicy policy =
                 base::scoped_policy::ASSUME) {
    if (object && policy == base::scoped_policy::RETAIN)
      object = Traits::Retain(object);
    if (object_)
      Traits::Release(object_);
    object_ = object;
  }
 
  bool operator==(T that) const { return object_ == that; }
 
  bool operator!=(T that) const { return object_ != that; }
 
  operator T() const { return object_; }
 
  T get() const { return object_; }
 
  void swap(ScopedTypeRef& that) {
    T temp = that.object_;
    that.object_ = object_;
    object_ = temp;
  }
 
  T release() WARN_UNUSED_RESULT {
    T temp = object_;
    object_ = Traits::InvalidValue();
    return temp;
  }
 
 private:
  T object_;
};
 
}  // namespace base
 
#endif  // MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_