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

Side by Side Diff: core/fxcrt/include/cfx_count_ref.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/cfx_count_ref_unittest.cpp ('k') | core/fxcrt/include/fx_basic.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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_FXCRT_INCLUDE_CFX_COUNT_REF_H_
8 #define CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_
9
10 #include "core/fxcrt/include/cfx_retain_ptr.h"
11 #include "core/fxcrt/include/fx_system.h"
12
13 template <class ObjClass>
14 class CFX_CountRef {
15 public:
16 CFX_CountRef() {}
17 CFX_CountRef(const CFX_CountRef& other) : m_pObject(other.m_pObject) {}
18 ~CFX_CountRef() {}
19
20 template <typename... Args>
21 ObjClass* New(Args... params) {
22 m_pObject.Reset(new CountedObj(params...));
23 return m_pObject.Get();
24 }
25
26 CFX_CountRef& operator=(const CFX_CountRef& that) {
27 if (*this != that)
28 m_pObject = that.m_pObject;
29 return *this;
30 }
31
32 void SetNull() { m_pObject.Reset(); }
33 bool IsNull() const { return !m_pObject; }
34 bool NotNull() const { return !IsNull(); }
35
36 const ObjClass* GetObject() const { return m_pObject.Get(); }
37
38 template <typename... Args>
39 ObjClass* GetModify(Args... params) {
40 if (!m_pObject)
41 return New(params...);
42 if (!m_pObject->HasOneRef())
43 m_pObject.Reset(new CountedObj(*m_pObject));
44 return m_pObject.Get();
45 }
46
47 bool operator==(const CFX_CountRef& that) const {
48 return m_pObject == that.m_pObject;
49 }
50 bool operator!=(const CFX_CountRef& that) const { return !(*this == that); }
51
52 protected:
53 class CountedObj : public ObjClass {
54 public:
55 template <typename... Args>
56 CountedObj(Args... params) : ObjClass(params...), m_RefCount(0) {}
57
58 CountedObj(const CountedObj& src) : ObjClass(src), m_RefCount(0) {}
59
60 bool HasOneRef() const { return m_RefCount == 1; }
61 void Retain() { m_RefCount++; }
62 void Release() {
63 if (--m_RefCount <= 0)
64 delete this;
65 }
66
67 private:
68 intptr_t m_RefCount;
69 };
70
71 CFX_RetainPtr<CountedObj> m_pObject;
72 };
73
74 #endif // CORE_FXCRT_INCLUDE_CFX_COUNT_REF_H_
OLDNEW
« no previous file with comments | « core/fxcrt/cfx_count_ref_unittest.cpp ('k') | core/fxcrt/include/fx_basic.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698