Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: core/fxcrt/include/fx_basic.h

Issue 2281683002: Rework CFX_CountRef in terms of CFX_RetainPtr (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Add test that GetModify() may construct a duplicate Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fxcrt/include/cfx_count_ref.h ('k') | core/fxge/include/fx_dib.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 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_FX_BASIC_H_ 7 #ifndef CORE_FXCRT_INCLUDE_FX_BASIC_H_
8 #define CORE_FXCRT_INCLUDE_FX_BASIC_H_ 8 #define CORE_FXCRT_INCLUDE_FX_BASIC_H_
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 } 642 }
643 643
644 protected: 644 protected:
645 uint32_t m_BitPos; 645 uint32_t m_BitPos;
646 646
647 uint32_t m_BitSize; 647 uint32_t m_BitSize;
648 648
649 const uint8_t* m_pData; 649 const uint8_t* m_pData;
650 }; 650 };
651 651
652 template <class ObjClass>
653 class CFX_CountRef {
654 public:
655 using Ref = CFX_CountRef<ObjClass>;
656
657 class CountedObj : public ObjClass {
658 public:
659 CountedObj() {}
660 CountedObj(const CountedObj& src) : ObjClass(src) {}
661
662 int m_RefCount;
663 };
664
665 CFX_CountRef() : m_pObject(nullptr) {}
666 CFX_CountRef(const Ref& ref) : m_pObject(ref.m_pObject) {
667 if (m_pObject)
668 m_pObject->m_RefCount++;
669 }
670
671 ~CFX_CountRef() { SetNull(); }
672
673 ObjClass* New() {
674 SetNull();
675 m_pObject = new CountedObj;
676 m_pObject->m_RefCount = 1;
677 return m_pObject;
678 }
679
680 void operator=(const Ref& ref) {
681 if (ref.m_pObject)
682 ref.m_pObject->m_RefCount++;
683 SetNull();
684 m_pObject = ref.m_pObject;
685 }
686
687 bool IsNull() const { return !m_pObject; }
688 bool NotNull() const { return !IsNull(); }
689
690 const ObjClass* GetObject() const { return m_pObject; }
691 ObjClass* GetModify() {
692 if (!m_pObject) {
693 m_pObject = new CountedObj;
694 m_pObject->m_RefCount = 1;
695 } else if (m_pObject->m_RefCount > 1) {
696 m_pObject->m_RefCount--;
697 CountedObj* pOldObject = m_pObject;
698 m_pObject = new CountedObj(*pOldObject);
699 m_pObject->m_RefCount = 1;
700 }
701 return m_pObject;
702 }
703
704 void SetNull() {
705 if (!m_pObject) {
706 return;
707 }
708 m_pObject->m_RefCount--;
709 if (m_pObject->m_RefCount <= 0) {
710 delete m_pObject;
711 }
712 m_pObject = nullptr;
713 }
714
715 bool operator==(const Ref& ref) const { return m_pObject == ref.m_pObject; }
716
717 protected:
718 CountedObj* m_pObject;
719 };
720
721 class IFX_Pause { 652 class IFX_Pause {
722 public: 653 public:
723 virtual ~IFX_Pause() {} 654 virtual ~IFX_Pause() {}
724 virtual FX_BOOL NeedToPauseNow() = 0; 655 virtual FX_BOOL NeedToPauseNow() = 0;
725 }; 656 };
726 657
727 template <typename T> 658 template <typename T>
728 class CFX_AutoRestorer { 659 class CFX_AutoRestorer {
729 public: 660 public:
730 explicit CFX_AutoRestorer(T* location) 661 explicit CFX_AutoRestorer(T* location)
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 FX_FLOAT e; 850 FX_FLOAT e;
920 FX_FLOAT f; 851 FX_FLOAT f;
921 FX_FLOAT g; 852 FX_FLOAT g;
922 FX_FLOAT h; 853 FX_FLOAT h;
923 FX_FLOAT i; 854 FX_FLOAT i;
924 }; 855 };
925 856
926 uint32_t GetBits32(const uint8_t* pData, int bitpos, int nbits); 857 uint32_t GetBits32(const uint8_t* pData, int bitpos, int nbits);
927 858
928 #endif // CORE_FXCRT_INCLUDE_FX_BASIC_H_ 859 #endif // CORE_FXCRT_INCLUDE_FX_BASIC_H_
OLDNEW
« no previous file with comments | « core/fxcrt/include/cfx_count_ref.h ('k') | core/fxge/include/fx_dib.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698