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

Side by Side Diff: core/fxcrt/cfx_retain_ptr.h

Issue 2536973003: Add pdfium::MakeRetain<>() helper function. (Closed)
Patch Set: better test Created 4 years 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
OLDNEW
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 #ifndef CORE_FXCRT_CFX_RETAIN_PTR_H_ 5 #ifndef CORE_FXCRT_CFX_RETAIN_PTR_H_
6 #define CORE_FXCRT_CFX_RETAIN_PTR_H_ 6 #define CORE_FXCRT_CFX_RETAIN_PTR_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility>
9 10
10 #include "core/fxcrt/fx_memory.h" 11 #include "core/fxcrt/fx_memory.h"
11 12
12 template <class T> 13 template <class T>
13 class CFX_RetainPtr { 14 class CFX_RetainPtr {
14 public: 15 public:
15 explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) { 16 explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) {
16 if (m_pObj) 17 if (m_pObj)
17 m_pObj->Retain(); 18 m_pObj->Retain();
18 } 19 }
19 20
20 CFX_RetainPtr() : CFX_RetainPtr(nullptr) {} 21 CFX_RetainPtr() {}
21 CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {} 22 CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {}
22 CFX_RetainPtr(CFX_RetainPtr&& that) { Swap(that); } 23 CFX_RetainPtr(CFX_RetainPtr&& that) { Swap(that); }
23 24
24 template <class U> 25 template <class U>
25 CFX_RetainPtr(const CFX_RetainPtr<U>& that) : CFX_RetainPtr(that.Get()) {} 26 CFX_RetainPtr(const CFX_RetainPtr<U>& that) : CFX_RetainPtr(that.Get()) {}
26 27
28 // Deliberately implicit to allow returning nullptrs.
29 CFX_RetainPtr(std::nullptr_t ptr) {}
npm 2016/11/29 22:25:30 Can this go above, with the other constructors?
Tom Sepez 2016/11/29 22:36:23 Done. (Actually, the template at line 25 this is
30
27 void Reset(T* obj = nullptr) { 31 void Reset(T* obj = nullptr) {
28 if (obj) 32 if (obj)
29 obj->Retain(); 33 obj->Retain();
30 m_pObj.reset(obj); 34 m_pObj.reset(obj);
31 } 35 }
32 36
33 T* Get() const { return m_pObj.get(); } 37 T* Get() const { return m_pObj.get(); }
34 void Swap(CFX_RetainPtr& that) { m_pObj.swap(that.m_pObj); } 38 void Swap(CFX_RetainPtr& that) { m_pObj.swap(that.m_pObj); }
35 39
36 CFX_RetainPtr& operator=(const CFX_RetainPtr& that) { 40 CFX_RetainPtr& operator=(const CFX_RetainPtr& that) {
37 if (*this != that) 41 if (*this != that)
38 Reset(that.Get()); 42 Reset(that.Get());
39 return *this; 43 return *this;
40 } 44 }
41 45
42 bool operator==(const CFX_RetainPtr& that) const { 46 bool operator==(const CFX_RetainPtr& that) const {
43 return Get() == that.Get(); 47 return Get() == that.Get();
44 } 48 }
45 49
46 bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); } 50 bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); }
47 51
48 explicit operator bool() const { return !!m_pObj; } 52 explicit operator bool() const { return !!m_pObj; }
49 T& operator*() const { return *m_pObj.get(); } 53 T& operator*() const { return *m_pObj.get(); }
50 T* operator->() const { return m_pObj.get(); } 54 T* operator->() const { return m_pObj.get(); }
51 55
52 private: 56 private:
53 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; 57 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj;
54 }; 58 };
55 59
60 // Trivial implementation - internal ref count with virtual destructor.
61 class CFX_Retainable {
62 public:
63 void Retain() { ++m_nRefCount; }
64 void Release() {
65 if (--m_nRefCount == 0)
66 delete this;
67 }
68 bool HasOneRef() const { return m_nRefCount == 1; }
69
70 protected:
71 virtual ~CFX_Retainable() {}
72 intptr_t m_nRefCount = 0;
73 };
74
75 namespace pdfium {
76
77 // Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(),
78 // or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor.
79 template <typename T, typename... Args>
80 CFX_RetainPtr<T> MakeRetain(Args&&... args) {
81 return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...));
82 }
83
84 } // namespace pdfium
85
56 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ 86 #endif // CORE_FXCRT_CFX_RETAIN_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | core/fxcrt/cfx_retain_ptr_unittest.cpp » ('j') | core/fxcrt/cfx_retain_ptr_unittest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698