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" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 // ScopedTypeRef<> is patterned after scoped_ptr<>, but maintains a ownership | 14 // ScopedTypeRef<> is patterned after std::unique_ptr<>, but maintains ownership |
15 // of a reference to any type that is maintained by Retain and Release methods. | 15 // of a reference to any type that is maintained by Retain and Release methods. |
16 // | 16 // |
17 // The Traits structure must provide the Retain and Release methods for type T. | 17 // The Traits structure must provide the Retain and Release methods for type T. |
18 // A default ScopedTypeRefTraits is used but not defined, and should be defined | 18 // A default ScopedTypeRefTraits is used but not defined, and should be defined |
19 // for each type to use this interface. For example, an appropriate definition | 19 // for each type to use this interface. For example, an appropriate definition |
20 // of ScopedTypeRefTraits for CGLContextObj would be: | 20 // of ScopedTypeRefTraits for CGLContextObj would be: |
21 // | 21 // |
22 // template<> | 22 // template<> |
23 // struct ScopedTypeRefTraits<CGLContextObj> { | 23 // struct ScopedTypeRefTraits<CGLContextObj> { |
24 // static CGLContextObj InvalidValue() { return nullptr; } | 24 // static CGLContextObj InvalidValue() { return nullptr; } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 T get() const { | 124 T get() const { |
125 return object_; | 125 return object_; |
126 } | 126 } |
127 | 127 |
128 void swap(ScopedTypeRef& that) { | 128 void swap(ScopedTypeRef& that) { |
129 T temp = that.object_; | 129 T temp = that.object_; |
130 that.object_ = object_; | 130 that.object_ = object_; |
131 object_ = temp; | 131 object_ = temp; |
132 } | 132 } |
133 | 133 |
134 // ScopedTypeRef<>::release() is like scoped_ptr<>::release. It is NOT | 134 // ScopedTypeRef<>::release() is like std::unique_ptr<>::release. It is NOT |
135 // a wrapper for Release(). To force a ScopedTypeRef<> object to call | 135 // a wrapper for Release(). To force a ScopedTypeRef<> object to call |
136 // Release(), use ScopedTypeRef<>::reset(). | 136 // Release(), use ScopedTypeRef<>::reset(). |
137 T release() WARN_UNUSED_RESULT { | 137 T release() WARN_UNUSED_RESULT { |
138 T temp = object_; | 138 T temp = object_; |
139 object_ = Traits::InvalidValue(); | 139 object_ = Traits::InvalidValue(); |
140 return temp; | 140 return temp; |
141 } | 141 } |
142 | 142 |
143 private: | 143 private: |
144 T object_; | 144 T object_; |
145 }; | 145 }; |
146 | 146 |
147 } // namespace base | 147 } // namespace base |
148 | 148 |
149 #endif // BASE_MAC_SCOPED_TYPEREF_H_ | 149 #endif // BASE_MAC_SCOPED_TYPEREF_H_ |
OLD | NEW |