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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ | 7 #ifndef CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ |
8 #define CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ | 8 #define CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ |
9 | 9 |
10 #include "core/fxcrt/include/cfx_retain_ptr.h" | 10 #include "core/fxcrt/include/cfx_retain_ptr.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 return m_pObject.Get(); | 23 return m_pObject.Get(); |
24 } | 24 } |
25 | 25 |
26 CFX_CountRef& operator=(const CFX_CountRef& that) { | 26 CFX_CountRef& operator=(const CFX_CountRef& that) { |
27 if (*this != that) | 27 if (*this != that) |
28 m_pObject = that.m_pObject; | 28 m_pObject = that.m_pObject; |
29 return *this; | 29 return *this; |
30 } | 30 } |
31 | 31 |
32 void SetNull() { m_pObject.Reset(); } | 32 void SetNull() { m_pObject.Reset(); } |
33 bool IsNull() const { return !m_pObject; } | |
34 bool NotNull() const { return !IsNull(); } | |
35 | |
36 const ObjClass* GetObject() const { return m_pObject.Get(); } | 33 const ObjClass* GetObject() const { return m_pObject.Get(); } |
37 | 34 |
38 template <typename... Args> | 35 template <typename... Args> |
39 ObjClass* GetModify(Args... params) { | 36 ObjClass* GetModify(Args... params) { |
40 if (!m_pObject) | 37 if (!m_pObject) |
41 return New(params...); | 38 return New(params...); |
42 if (!m_pObject->HasOneRef()) | 39 if (!m_pObject->HasOneRef()) |
43 m_pObject.Reset(new CountedObj(*m_pObject)); | 40 m_pObject.Reset(new CountedObj(*m_pObject)); |
44 return m_pObject.Get(); | 41 return m_pObject.Get(); |
45 } | 42 } |
46 | 43 |
47 bool operator==(const CFX_CountRef& that) const { | 44 bool operator==(const CFX_CountRef& that) const { |
48 return m_pObject == that.m_pObject; | 45 return m_pObject == that.m_pObject; |
49 } | 46 } |
50 bool operator!=(const CFX_CountRef& that) const { return !(*this == that); } | 47 bool operator!=(const CFX_CountRef& that) const { return !(*this == that); } |
| 48 operator bool() const { return m_pObject; } |
51 | 49 |
52 protected: | 50 protected: |
53 class CountedObj : public ObjClass { | 51 class CountedObj : public ObjClass { |
54 public: | 52 public: |
55 template <typename... Args> | 53 template <typename... Args> |
56 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} | 54 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} |
57 | 55 |
58 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} | 56 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} |
59 | 57 |
60 bool HasOneRef() const { return m_RefCount == 1; } | 58 bool HasOneRef() const { return m_RefCount == 1; } |
61 void Retain() { m_RefCount++; } | 59 void Retain() { m_RefCount++; } |
62 void Release() { | 60 void Release() { |
63 if (--m_RefCount <= 0) | 61 if (--m_RefCount <= 0) |
64 delete this; | 62 delete this; |
65 } | 63 } |
66 | 64 |
67 private: | 65 private: |
68 intptr_t m_RefCount; | 66 intptr_t m_RefCount; |
69 }; | 67 }; |
70 | 68 |
71 CFX_RetainPtr<CountedObj> m_pObject; | 69 CFX_RetainPtr<CountedObj> m_pObject; |
72 }; | 70 }; |
73 | 71 |
74 #endif // CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ | 72 #endif // CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ |
OLD | NEW |