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