OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_ |
| 6 #define MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_ |
| 7 |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_policy.h" |
| 11 |
| 12 namespace base { |
| 13 |
| 14 template <typename T> |
| 15 struct ScopedTypeRefTraits; |
| 16 |
| 17 template <typename T, typename Traits = ScopedTypeRefTraits<T>> |
| 18 class ScopedTypeRef { |
| 19 public: |
| 20 typedef T element_type; |
| 21 |
| 22 ScopedTypeRef( |
| 23 T object = Traits::InvalidValue(), |
| 24 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) |
| 25 : object_(object) { |
| 26 if (object_ && policy == base::scoped_policy::RETAIN) |
| 27 object_ = Traits::Retain(object_); |
| 28 } |
| 29 |
| 30 ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) : object_(that.object_) { |
| 31 if (object_) |
| 32 object_ = Traits::Retain(object_); |
| 33 } |
| 34 |
| 35 ~ScopedTypeRef() { |
| 36 if (object_) |
| 37 Traits::Release(object_); |
| 38 } |
| 39 |
| 40 ScopedTypeRef& operator=(const ScopedTypeRef<T, Traits>& that) { |
| 41 reset(that.get(), base::scoped_policy::RETAIN); |
| 42 return *this; |
| 43 } |
| 44 |
| 45 T* InitializeInto() WARN_UNUSED_RESULT { |
| 46 DCHECK(!object_); |
| 47 return &object_; |
| 48 } |
| 49 |
| 50 void reset(T object = Traits::InvalidValue(), |
| 51 base::scoped_policy::OwnershipPolicy policy = |
| 52 base::scoped_policy::ASSUME) { |
| 53 if (object && policy == base::scoped_policy::RETAIN) |
| 54 object = Traits::Retain(object); |
| 55 if (object_) |
| 56 Traits::Release(object_); |
| 57 object_ = object; |
| 58 } |
| 59 |
| 60 bool operator==(T that) const { return object_ == that; } |
| 61 |
| 62 bool operator!=(T that) const { return object_ != that; } |
| 63 |
| 64 operator T() const { return object_; } |
| 65 |
| 66 T get() const { return object_; } |
| 67 |
| 68 void swap(ScopedTypeRef& that) { |
| 69 T temp = that.object_; |
| 70 that.object_ = object_; |
| 71 object_ = temp; |
| 72 } |
| 73 |
| 74 T release() WARN_UNUSED_RESULT { |
| 75 T temp = object_; |
| 76 object_ = Traits::InvalidValue(); |
| 77 return temp; |
| 78 } |
| 79 |
| 80 private: |
| 81 T object_; |
| 82 }; |
| 83 |
| 84 } // namespace base |
| 85 |
| 86 #endif // MINI_CHROMIUM_BASE_MAC_SCOPED_TYPEREF_H_ |
OLD | NEW |