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

Unified Diff: core/fxcrt/cfx_retain_ptr.h

Issue 2536973003: Add pdfium::MakeRetain<>() helper function. (Closed)
Patch Set: better test Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | core/fxcrt/cfx_retain_ptr_unittest.cpp » ('j') | core/fxcrt/cfx_retain_ptr_unittest.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/cfx_retain_ptr.h
diff --git a/core/fxcrt/cfx_retain_ptr.h b/core/fxcrt/cfx_retain_ptr.h
index 883494e7eceb432b4fc2eeeb3cc94260e2c2496d..98f965f1a1a52c5f6117dec35941c2101cf03a2c 100644
--- a/core/fxcrt/cfx_retain_ptr.h
+++ b/core/fxcrt/cfx_retain_ptr.h
@@ -6,6 +6,7 @@
#define CORE_FXCRT_CFX_RETAIN_PTR_H_
#include <memory>
+#include <utility>
#include "core/fxcrt/fx_memory.h"
@@ -17,13 +18,16 @@ class CFX_RetainPtr {
m_pObj->Retain();
}
- CFX_RetainPtr() : CFX_RetainPtr(nullptr) {}
+ CFX_RetainPtr() {}
CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {}
CFX_RetainPtr(CFX_RetainPtr&& that) { Swap(that); }
template <class U>
CFX_RetainPtr(const CFX_RetainPtr<U>& that) : CFX_RetainPtr(that.Get()) {}
+ // Deliberately implicit to allow returning nullptrs.
+ 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
+
void Reset(T* obj = nullptr) {
if (obj)
obj->Retain();
@@ -53,4 +57,30 @@ class CFX_RetainPtr {
std::unique_ptr<T, ReleaseDeleter<T>> m_pObj;
};
+// Trivial implementation - internal ref count with virtual destructor.
+class CFX_Retainable {
+ public:
+ void Retain() { ++m_nRefCount; }
+ void Release() {
+ if (--m_nRefCount == 0)
+ delete this;
+ }
+ bool HasOneRef() const { return m_nRefCount == 1; }
+
+ protected:
+ virtual ~CFX_Retainable() {}
+ intptr_t m_nRefCount = 0;
+};
+
+namespace pdfium {
+
+// Helper to make a CFX_RetainPtr along the lines of std::make_unique<>(),
+// or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor.
+template <typename T, typename... Args>
+CFX_RetainPtr<T> MakeRetain(Args&&... args) {
+ return CFX_RetainPtr<T>(new T(std::forward<Args>(args)...));
+}
+
+} // namespace pdfium
+
#endif // CORE_FXCRT_CFX_RETAIN_PTR_H_
« 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