OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium 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 CORE_FXCRT_CFX_RETAIN_PTR_H_ | 5 #ifndef CORE_FXCRT_CFX_RETAIN_PTR_H_ |
6 #define CORE_FXCRT_CFX_RETAIN_PTR_H_ | 6 #define CORE_FXCRT_CFX_RETAIN_PTR_H_ |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 76 |
77 private: | 77 private: |
78 template <typename U> | 78 template <typename U> |
79 friend struct ReleaseDeleter; | 79 friend struct ReleaseDeleter; |
80 | 80 |
81 template <typename U> | 81 template <typename U> |
82 friend class CFX_RetainPtr; | 82 friend class CFX_RetainPtr; |
83 | 83 |
84 void Retain() { ++m_nRefCount; } | 84 void Retain() { ++m_nRefCount; } |
85 void Release() { | 85 void Release() { |
| 86 ASSERT(m_nRefCount > 0); |
86 if (--m_nRefCount == 0) | 87 if (--m_nRefCount == 0) |
87 delete this; | 88 delete this; |
88 } | 89 } |
89 | 90 |
90 intptr_t m_nRefCount = 0; | 91 intptr_t m_nRefCount = 0; |
91 }; | 92 }; |
92 | 93 |
93 namespace pdfium { | 94 namespace pdfium { |
94 | 95 |
95 // Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(), | 96 // Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(), |
96 // or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor. | 97 // or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor. |
97 // Classes managed by CFX_RetainPtr should have protected (or private) | 98 // Classes managed by CFX_RetainPtr should have protected (or private) |
98 // constructors, and should friend this function. | 99 // constructors, and should friend this function. |
99 template <typename T, typename... Args> | 100 template <typename T, typename... Args> |
100 CFX_RetainPtr<T> MakeRetain(Args&&... args) { | 101 CFX_RetainPtr<T> MakeRetain(Args&&... args) { |
101 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...)); | 102 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...)); |
102 } | 103 } |
103 | 104 |
104 } // namespace pdfium | 105 } // namespace pdfium |
105 | 106 |
106 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ | 107 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ |
OLD | NEW |