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 11 matching lines...) Expand all Loading... |
22 m_pObject.Reset(new CountedObj(params...)); | 22 m_pObject.Reset(new CountedObj(params...)); |
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 Clear() { m_pObject.Reset(); } | 32 void SetNull() { m_pObject.Reset(); } |
33 ObjClass* GetObject() { return m_pObject.Get(); } | 33 ObjClass* GetObject() { return m_pObject.Get(); } |
34 const ObjClass* GetObject() const { return m_pObject.Get(); } | 34 const ObjClass* GetObject() const { return m_pObject.Get(); } |
35 | 35 |
36 template <typename... Args> | 36 template <typename... Args> |
37 void MakePrivateCopy(Args... params) { | 37 ObjClass* GetPrivateCopy(Args... params) { |
38 if (!m_pObject) | 38 if (!m_pObject) |
39 m_pObject.Reset(new CountedObj(params...)); | 39 return New(params...); |
40 else if (!m_pObject->HasOneRef()) | 40 if (!m_pObject->HasOneRef()) |
41 m_pObject.Reset(new CountedObj(*m_pObject)); | 41 m_pObject.Reset(new CountedObj(*m_pObject)); |
| 42 return m_pObject.Get(); |
42 } | 43 } |
43 | 44 |
44 bool operator==(const CFX_CountRef& that) const { | 45 bool operator==(const CFX_CountRef& that) const { |
45 return m_pObject == that.m_pObject; | 46 return m_pObject == that.m_pObject; |
46 } | 47 } |
47 bool operator!=(const CFX_CountRef& that) const { return !(*this == that); } | 48 bool operator!=(const CFX_CountRef& that) const { return !(*this == that); } |
48 operator bool() const { return m_pObject; } | 49 operator bool() const { return m_pObject; } |
49 ObjClass& operator*() const { return *m_pObject.Get(); } | |
50 ObjClass* operator->() const { return m_pObject.Get(); } | |
51 | 50 |
52 private: | 51 private: |
53 class CountedObj : public ObjClass { | 52 class CountedObj : public ObjClass { |
54 public: | 53 public: |
55 template <typename... Args> | 54 template <typename... Args> |
56 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} | 55 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} |
57 | 56 |
58 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} | 57 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} |
59 | 58 |
60 bool HasOneRef() const { return m_RefCount == 1; } | 59 bool HasOneRef() const { return m_RefCount == 1; } |
61 void Retain() { m_RefCount++; } | 60 void Retain() { m_RefCount++; } |
62 void Release() { | 61 void Release() { |
63 if (--m_RefCount <= 0) | 62 if (--m_RefCount <= 0) |
64 delete this; | 63 delete this; |
65 } | 64 } |
66 | 65 |
67 private: | 66 private: |
68 intptr_t m_RefCount; | 67 intptr_t m_RefCount; |
69 }; | 68 }; |
70 | 69 |
71 CFX_RetainPtr<CountedObj> m_pObject; | 70 CFX_RetainPtr<CountedObj> m_pObject; |
72 }; | 71 }; |
73 | 72 |
74 #endif // CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ | 73 #endif // CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_ |
OLD | NEW |