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 <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 T& operator*() const { return *m_pObj.get(); } | 53 T& operator*() const { return *m_pObj.get(); } |
54 T* operator->() const { return m_pObj.get(); } | 54 T* operator->() const { return m_pObj.get(); } |
55 | 55 |
56 private: | 56 private: |
57 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; | 57 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; |
58 }; | 58 }; |
59 | 59 |
60 // Trivial implementation - internal ref count with virtual destructor. | 60 // Trivial implementation - internal ref count with virtual destructor. |
61 class CFX_Retainable { | 61 class CFX_Retainable { |
62 public: | 62 public: |
| 63 bool HasOneRef() const { return m_nRefCount == 1; } |
| 64 |
| 65 protected: |
| 66 virtual ~CFX_Retainable() {} |
| 67 |
| 68 private: |
| 69 template <typename U> |
| 70 friend struct ReleaseDeleter; |
| 71 |
| 72 template <typename U> |
| 73 friend class CFX_RetainPtr; |
| 74 |
63 void Retain() { ++m_nRefCount; } | 75 void Retain() { ++m_nRefCount; } |
64 void Release() { | 76 void Release() { |
65 if (--m_nRefCount == 0) | 77 if (--m_nRefCount == 0) |
66 delete this; | 78 delete this; |
67 } | 79 } |
68 bool HasOneRef() const { return m_nRefCount == 1; } | |
69 | 80 |
70 protected: | |
71 virtual ~CFX_Retainable() {} | |
72 intptr_t m_nRefCount = 0; | 81 intptr_t m_nRefCount = 0; |
73 }; | 82 }; |
74 | 83 |
75 namespace pdfium { | 84 namespace pdfium { |
76 | 85 |
77 // Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(), | 86 // Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(), |
78 // or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor. | 87 // or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor. |
| 88 // Classes managed by CFX_RetainPtr should have protected (or private) |
| 89 // constructors, and should friend this function. |
79 template <typename T, typename... Args> | 90 template <typename T, typename... Args> |
80 CFX_RetainPtr<T> MakeRetain(Args&&... args) { | 91 CFX_RetainPtr<T> MakeRetain(Args&&... args) { |
81 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...)); | 92 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...)); |
82 } | 93 } |
83 | 94 |
84 } // namespace pdfium | 95 } // namespace pdfium |
85 | 96 |
86 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ | 97 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ |
OLD | NEW |