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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 // is to |ASSUME|. | 46 // is to |ASSUME|. |
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 explicit ScopedTypeRef( |
57 T object = Traits::InvalidValue(), | 57 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_); |
68 } | 68 } |
69 | 69 |
70 // Without this, passing a ScopedTypeRef<A,TraitsX> to construct a | 70 // This allows passing an object to a function that takes its superclass. |
71 // ScopedTypeRef<A,TraitsY> would automatically cast down to an A, and then | 71 template <typename R, typename RTraits> |
72 // ASSUME ownership of A, when a retain is what was needed. | 72 explicit ScopedTypeRef(const ScopedTypeRef<R, RTraits>& that_as_subclass) |
ccameron
2016/06/01 00:39:11
Note that this is not true anymore, now that we ha
| |
73 template<typename OtherTraits> | 73 : object_(that_as_subclass.get()) { |
74 ScopedTypeRef(const ScopedTypeRef<T, OtherTraits>& that_with_other_traits) | |
75 : object_(that_with_other_traits.get()) { | |
76 if (object_) | 74 if (object_) |
77 object_ = Traits::Retain(object_); | 75 object_ = Traits::Retain(object_); |
78 } | 76 } |
79 | 77 |
80 ScopedTypeRef(ScopedTypeRef<T, Traits>&& that) : object_(that.object_) { | 78 ScopedTypeRef(ScopedTypeRef<T, Traits>&& that) : object_(that.object_) { |
81 that.object_ = Traits::InvalidValue(); | 79 that.object_ = Traits::InvalidValue(); |
82 } | 80 } |
83 | 81 |
84 ~ScopedTypeRef() { | 82 ~ScopedTypeRef() { |
85 if (object_) | 83 if (object_) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 return temp; | 138 return temp; |
141 } | 139 } |
142 | 140 |
143 private: | 141 private: |
144 T object_; | 142 T object_; |
145 }; | 143 }; |
146 | 144 |
147 } // namespace base | 145 } // namespace base |
148 | 146 |
149 #endif // BASE_MAC_SCOPED_TYPEREF_H_ | 147 #endif // BASE_MAC_SCOPED_TYPEREF_H_ |
OLD | NEW |