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_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ | 7 #ifndef CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ |
8 #define CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ | 8 #define CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ |
9 | 9 |
| 10 #include <memory> |
| 11 |
10 #include "core/fpdfapi/page/cpdf_colorspace.h" | 12 #include "core/fpdfapi/page/cpdf_colorspace.h" |
11 #include "core/fpdfapi/page/cpdf_pattern.h" | 13 #include "core/fpdfapi/page/cpdf_pattern.h" |
12 #include "core/fxcrt/fx_system.h" | 14 #include "core/fxcrt/fx_system.h" |
13 | 15 |
14 template <class T> | 16 template <class T> |
15 class CPDF_CountedObject { | 17 class CPDF_CountedObject { |
16 public: | 18 public: |
17 explicit CPDF_CountedObject(T* ptr) : m_nCount(1), m_pObj(ptr) {} | 19 explicit CPDF_CountedObject(std::unique_ptr<T> ptr) |
18 void reset(T* ptr) { // CAUTION: tosses prior ref counts. | 20 : m_nCount(1), m_pObj(ptr.release()) {} |
| 21 void reset(std::unique_ptr<T> ptr) { // CAUTION: tosses prior ref counts. |
19 m_nCount = 1; | 22 m_nCount = 1; |
20 m_pObj = ptr; | 23 m_pObj = ptr.release(); |
21 } | 24 } |
22 void clear() { // Now you're all weak ptrs ... | 25 void clear() { // Now you're all weak ptrs ... |
23 // Guard against accidental re-entry. | 26 // Guard against accidental re-entry. |
24 T* pObj = m_pObj; | 27 T* pObj = m_pObj; |
25 m_pObj = nullptr; | 28 m_pObj = nullptr; |
26 delete pObj; | 29 delete pObj; |
27 } | 30 } |
28 T* get() const { return m_pObj; } | 31 T* get() const { return m_pObj; } |
29 T* AddRef() { | 32 T* AddRef() { |
30 ASSERT(m_pObj); | 33 ASSERT(m_pObj); |
31 ++m_nCount; | 34 ++m_nCount; |
32 return m_pObj; | 35 return m_pObj; |
33 } | 36 } |
34 void RemoveRef() { | 37 void RemoveRef() { |
35 if (m_nCount) | 38 if (m_nCount) |
36 --m_nCount; | 39 --m_nCount; |
37 } | 40 } |
38 size_t use_count() const { return m_nCount; } | 41 size_t use_count() const { return m_nCount; } |
39 | 42 |
40 protected: | 43 protected: |
41 size_t m_nCount; | 44 size_t m_nCount; |
42 T* m_pObj; | 45 T* m_pObj; |
43 }; | 46 }; |
44 using CPDF_CountedColorSpace = CPDF_CountedObject<CPDF_ColorSpace>; | 47 using CPDF_CountedColorSpace = CPDF_CountedObject<CPDF_ColorSpace>; |
45 using CPDF_CountedPattern = CPDF_CountedObject<CPDF_Pattern>; | 48 using CPDF_CountedPattern = CPDF_CountedObject<CPDF_Pattern>; |
46 | 49 |
47 #endif // CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ | 50 #endif // CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ |
OLD | NEW |