OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MAC_SCOPED_TYPEREF_H_ | 5 #ifndef BASE_MAC_SCOPED_TYPEREF_H_ |
6 #define BASE_MAC_SCOPED_TYPEREF_H_ | 6 #define BASE_MAC_SCOPED_TYPEREF_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_policy.h" | 10 #include "base/memory/scoped_policy.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 template<typename T> | 48 template<typename T> |
49 struct ScopedTypeRefTraits; | 49 struct ScopedTypeRefTraits; |
50 | 50 |
51 template<typename T, typename Traits = ScopedTypeRefTraits<T>> | 51 template<typename T, typename Traits = ScopedTypeRefTraits<T>> |
52 class ScopedTypeRef { | 52 class ScopedTypeRef { |
53 public: | 53 public: |
54 typedef T element_type; | 54 typedef T element_type; |
55 | 55 |
56 ScopedTypeRef( | 56 ScopedTypeRef( |
57 T object = Traits::InvalidValue(), | 57 __unsafe_unretained T object = Traits::InvalidValue(), |
58 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) | 58 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) |
59 : object_(object) { | 59 : object_(object) { |
60 if (object_ && policy == base::scoped_policy::RETAIN) | 60 if (object_ && policy == base::scoped_policy::RETAIN) |
61 object_ = Traits::Retain(object_); | 61 object_ = Traits::Retain(object_); |
62 } | 62 } |
63 | 63 |
64 ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) | 64 ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) |
65 : object_(that.object_) { | 65 : object_(that.object_) { |
66 if (object_) | 66 if (object_) |
67 object_ = Traits::Retain(object_); | 67 object_ = Traits::Retain(object_); |
(...skipping 24 matching lines...) Expand all Loading... |
92 } | 92 } |
93 | 93 |
94 // This is to be used only to take ownership of objects that are created | 94 // This is to be used only to take ownership of objects that are created |
95 // by pass-by-pointer create functions. To enforce this, require that the | 95 // by pass-by-pointer create functions. To enforce this, require that the |
96 // object be reset to NULL before this may be used. | 96 // object be reset to NULL before this may be used. |
97 T* InitializeInto() WARN_UNUSED_RESULT { | 97 T* InitializeInto() WARN_UNUSED_RESULT { |
98 DCHECK(!object_); | 98 DCHECK(!object_); |
99 return &object_; | 99 return &object_; |
100 } | 100 } |
101 | 101 |
102 void reset(T object = Traits::InvalidValue(), | 102 void reset(__unsafe_unretained T object = Traits::InvalidValue(), |
103 base::scoped_policy::OwnershipPolicy policy = | 103 base::scoped_policy::OwnershipPolicy policy = |
104 base::scoped_policy::ASSUME) { | 104 base::scoped_policy::ASSUME) { |
105 if (object && policy == base::scoped_policy::RETAIN) | 105 if (object && policy == base::scoped_policy::RETAIN) |
106 object = Traits::Retain(object); | 106 object = Traits::Retain(object); |
107 if (object_) | 107 if (object_) |
108 Traits::Release(object_); | 108 Traits::Release(object_); |
109 object_ = object; | 109 object_ = object; |
110 } | 110 } |
111 | 111 |
112 bool operator==(T that) const { | 112 bool operator==(__unsafe_unretained T that) const { return object_ == that; } |
113 return object_ == that; | |
114 } | |
115 | 113 |
116 bool operator!=(T that) const { | 114 bool operator!=(__unsafe_unretained T that) const { return object_ != that; } |
117 return object_ != that; | |
118 } | |
119 | 115 |
120 operator T() const { | 116 operator T() const __attribute((ns_returns_not_retained)) { return object_; } |
121 return object_; | |
122 } | |
123 | 117 |
124 T get() const { | 118 T get() const __attribute((ns_returns_not_retained)) { return object_; } |
125 return object_; | |
126 } | |
127 | 119 |
128 void swap(ScopedTypeRef& that) { | 120 void swap(ScopedTypeRef& that) { |
129 T temp = that.object_; | 121 __unsafe_unretained T temp = that.object_; |
130 that.object_ = object_; | 122 that.object_ = object_; |
131 object_ = temp; | 123 object_ = temp; |
132 } | 124 } |
133 | 125 |
134 // ScopedTypeRef<>::release() is like scoped_ptr<>::release. It is NOT | 126 // ScopedTypeRef<>::release() is like scoped_ptr<>::release. It is NOT |
135 // a wrapper for Release(). To force a ScopedTypeRef<> object to call | 127 // a wrapper for Release(). To force a ScopedTypeRef<> object to call |
136 // Release(), use ScopedTypeRef<>::reset(). | 128 // Release(), use ScopedTypeRef<>::reset(). |
137 T release() WARN_UNUSED_RESULT { | 129 T release() __attribute((ns_returns_not_retained)) WARN_UNUSED_RESULT { |
138 T temp = object_; | 130 __unsafe_unretained T temp = object_; |
139 object_ = Traits::InvalidValue(); | 131 object_ = Traits::InvalidValue(); |
140 return temp; | 132 return temp; |
141 } | 133 } |
142 | 134 |
143 private: | 135 private: |
144 T object_; | 136 __unsafe_unretained T object_; |
145 }; | 137 }; |
146 | 138 |
147 } // namespace base | 139 } // namespace base |
148 | 140 |
149 #endif // BASE_MAC_SCOPED_TYPEREF_H_ | 141 #endif // BASE_MAC_SCOPED_TYPEREF_H_ |
OLD | NEW |