• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_
8 #define CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_
9 
10 #include <memory>
11 
12 #include "core/fpdfapi/page/cpdf_colorspace.h"
13 #include "core/fpdfapi/page/cpdf_pattern.h"
14 #include "core/fxcrt/fx_system.h"
15 
16 template <class T>
17 class CPDF_CountedObject {
18  public:
CPDF_CountedObject(std::unique_ptr<T> ptr)19   explicit CPDF_CountedObject(std::unique_ptr<T> ptr)
20       : m_nCount(1), m_pObj(ptr.release()) {}
reset(std::unique_ptr<T> ptr)21   void reset(std::unique_ptr<T> ptr) {  // CAUTION: tosses prior ref counts.
22     m_nCount = 1;
23     m_pObj = ptr.release();
24   }
clear()25   void clear() {  // Now you're all weak ptrs ...
26     // Guard against accidental re-entry.
27     T* pObj = m_pObj;
28     m_pObj = nullptr;
29     delete pObj;
30   }
get()31   T* get() const { return m_pObj; }
AddRef()32   T* AddRef() {
33     ASSERT(m_pObj);
34     ++m_nCount;
35     return m_pObj;
36   }
RemoveRef()37   void RemoveRef() {
38     if (m_nCount)
39       --m_nCount;
40   }
use_count()41   size_t use_count() const { return m_nCount; }
42 
43  protected:
44   size_t m_nCount;
45   T* m_pObj;
46 };
47 using CPDF_CountedColorSpace = CPDF_CountedObject<CPDF_ColorSpace>;
48 using CPDF_CountedPattern = CPDF_CountedObject<CPDF_Pattern>;
49 
50 #endif  // CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_
51