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_CFX_COUNT_REF_H_ | 7 #ifndef CORE_FXCRT_CFX_SHARED_COPY_ON_WRITE_H_ |
8 #define CORE_FXCRT_CFX_COUNT_REF_H_ | 8 #define CORE_FXCRT_CFX_SHARED_COPY_ON_WRITE_H_ |
9 | 9 |
10 #include "core/fxcrt/cfx_retain_ptr.h" | 10 #include "core/fxcrt/cfx_retain_ptr.h" |
11 #include "core/fxcrt/fx_system.h" | 11 #include "core/fxcrt/fx_system.h" |
12 | 12 |
13 // A shared object with Copy on Write semantics that makes it appear as | 13 // A shared object with Copy on Write semantics that makes it appear as |
14 // if each one were independent. | 14 // if each one were independent. |
15 template <class ObjClass> | 15 template <class ObjClass> |
16 class CFX_CountRef { | 16 class CFX_SharedCopyOnWrite { |
17 public: | 17 public: |
18 CFX_CountRef() {} | 18 CFX_SharedCopyOnWrite() {} |
19 CFX_CountRef(const CFX_CountRef& other) : m_pObject(other.m_pObject) {} | 19 CFX_SharedCopyOnWrite(const CFX_SharedCopyOnWrite& other) |
20 ~CFX_CountRef() {} | 20 : m_pObject(other.m_pObject) {} |
| 21 ~CFX_SharedCopyOnWrite() {} |
21 | 22 |
22 template <typename... Args> | 23 template <typename... Args> |
23 ObjClass* Emplace(Args... params) { | 24 ObjClass* Emplace(Args... params) { |
24 m_pObject.Reset(new CountedObj(params...)); | 25 m_pObject.Reset(new CountedObj(params...)); |
25 return m_pObject.Get(); | 26 return m_pObject.Get(); |
26 } | 27 } |
27 | 28 |
28 CFX_CountRef& operator=(const CFX_CountRef& that) { | 29 CFX_SharedCopyOnWrite& operator=(const CFX_SharedCopyOnWrite& that) { |
29 if (*this != that) | 30 if (*this != that) |
30 m_pObject = that.m_pObject; | 31 m_pObject = that.m_pObject; |
31 return *this; | 32 return *this; |
32 } | 33 } |
33 | 34 |
34 void SetNull() { m_pObject.Reset(); } | 35 void SetNull() { m_pObject.Reset(); } |
35 const ObjClass* GetObject() const { return m_pObject.Get(); } | 36 const ObjClass* GetObject() const { return m_pObject.Get(); } |
36 | 37 |
37 template <typename... Args> | 38 template <typename... Args> |
38 ObjClass* GetPrivateCopy(Args... params) { | 39 ObjClass* GetPrivateCopy(Args... params) { |
39 if (!m_pObject) | 40 if (!m_pObject) |
40 return Emplace(params...); | 41 return Emplace(params...); |
41 if (!m_pObject->HasOneRef()) | 42 if (!m_pObject->HasOneRef()) |
42 m_pObject.Reset(new CountedObj(*m_pObject)); | 43 m_pObject.Reset(new CountedObj(*m_pObject)); |
43 return m_pObject.Get(); | 44 return m_pObject.Get(); |
44 } | 45 } |
45 | 46 |
46 bool operator==(const CFX_CountRef& that) const { | 47 bool operator==(const CFX_SharedCopyOnWrite& that) const { |
47 return m_pObject == that.m_pObject; | 48 return m_pObject == that.m_pObject; |
48 } | 49 } |
49 bool operator!=(const CFX_CountRef& that) const { return !(*this == that); } | 50 bool operator!=(const CFX_SharedCopyOnWrite& that) const { |
| 51 return !(*this == that); |
| 52 } |
50 explicit operator bool() const { return !!m_pObject; } | 53 explicit operator bool() const { return !!m_pObject; } |
51 | 54 |
52 private: | 55 private: |
53 class CountedObj : public ObjClass { | 56 class CountedObj : public ObjClass { |
54 public: | 57 public: |
55 template <typename... Args> | 58 template <typename... Args> |
56 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} | 59 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {} |
57 | 60 |
58 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} | 61 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {} |
59 ~CountedObj() { m_RefCount = 0; } | 62 ~CountedObj() { m_RefCount = 0; } |
(...skipping 11 matching lines...) Expand all Loading... |
71 // the entire address space contains nothing but pointers to this object. | 74 // the entire address space contains nothing but pointers to this object. |
72 // Since the count increments with each new pointer, the largest value is | 75 // Since the count increments with each new pointer, the largest value is |
73 // the number of pointers that can fit into the address space. The size of | 76 // the number of pointers that can fit into the address space. The size of |
74 // the address space itself is a good upper bound on it. | 77 // the address space itself is a good upper bound on it. |
75 intptr_t m_RefCount; | 78 intptr_t m_RefCount; |
76 }; | 79 }; |
77 | 80 |
78 CFX_RetainPtr<CountedObj> m_pObject; | 81 CFX_RetainPtr<CountedObj> m_pObject; |
79 }; | 82 }; |
80 | 83 |
81 #endif // CORE_FXCRT_CFX_COUNT_REF_H_ | 84 #endif // CORE_FXCRT_CFX_SHARED_COPY_ON_WRITE_H_ |
OLD | NEW |