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 <memory> | 9 #include <memory> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "core/fxcrt/fx_memory.h" | 12 #include "core/fxcrt/fx_memory.h" |
12 | 13 |
| 14 // Analogous to base's scoped_refptr. |
13 template <class T> | 15 template <class T> |
14 class CFX_RetainPtr { | 16 class CFX_RetainPtr { |
15 public: | 17 public: |
16 explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) { | 18 explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) { |
17 if (m_pObj) | 19 if (m_pObj) |
18 m_pObj->Retain(); | 20 m_pObj->Retain(); |
19 } | 21 } |
20 | 22 |
21 CFX_RetainPtr() {} | 23 CFX_RetainPtr() {} |
22 CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {} | 24 CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {} |
(...skipping 20 matching lines...) Expand all Loading... |
43 | 45 |
44 CFX_RetainPtr& operator=(const CFX_RetainPtr& that) { | 46 CFX_RetainPtr& operator=(const CFX_RetainPtr& that) { |
45 if (*this != that) | 47 if (*this != that) |
46 Reset(that.Get()); | 48 Reset(that.Get()); |
47 return *this; | 49 return *this; |
48 } | 50 } |
49 | 51 |
50 bool operator==(const CFX_RetainPtr& that) const { | 52 bool operator==(const CFX_RetainPtr& that) const { |
51 return Get() == that.Get(); | 53 return Get() == that.Get(); |
52 } | 54 } |
| 55 bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); } |
53 | 56 |
54 bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); } | 57 bool operator<(const CFX_RetainPtr& that) const { |
| 58 return std::less<T*>()(Get(), that.Get()); |
| 59 } |
55 | 60 |
56 explicit operator bool() const { return !!m_pObj; } | 61 explicit operator bool() const { return !!m_pObj; } |
57 T& operator*() const { return *m_pObj.get(); } | 62 T& operator*() const { return *m_pObj.get(); } |
58 T* operator->() const { return m_pObj.get(); } | 63 T* operator->() const { return m_pObj.get(); } |
59 | 64 |
60 private: | 65 private: |
61 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; | 66 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; |
62 }; | 67 }; |
63 | 68 |
64 // Trivial implementation - internal ref count with virtual destructor. | 69 // Trivial implementation - internal ref count with virtual destructor. |
(...skipping 27 matching lines...) Expand all Loading... |
92 // Classes managed by CFX_RetainPtr should have protected (or private) | 97 // Classes managed by CFX_RetainPtr should have protected (or private) |
93 // constructors, and should friend this function. | 98 // constructors, and should friend this function. |
94 template <typename T, typename... Args> | 99 template <typename T, typename... Args> |
95 CFX_RetainPtr<T> MakeRetain(Args&&... args) { | 100 CFX_RetainPtr<T> MakeRetain(Args&&... args) { |
96 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...)); | 101 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...)); |
97 } | 102 } |
98 | 103 |
99 } // namespace pdfium | 104 } // namespace pdfium |
100 | 105 |
101 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ | 106 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ |
OLD | NEW |