Index: core/include/fxcrt/cfx_retain_ptr.h |
diff --git a/core/include/fxcrt/cfx_retain_ptr.h b/core/include/fxcrt/cfx_retain_ptr.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d9a5103c789bb2f41b2c41892af9090e9ff8d756 |
--- /dev/null |
+++ b/core/include/fxcrt/cfx_retain_ptr.h |
@@ -0,0 +1,59 @@ |
+// Copyright 2016 PDFium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CORE_INCLUDE_FXCRT_CFX_RETAIN_PTR_H_ |
+#define CORE_INCLUDE_FXCRT_CFX_RETAIN_PTR_H_ |
+ |
+#include <memory> |
+ |
+#include "core/include/fxcrt/fx_memory.h" |
+ |
+template <class T> |
+class CFX_RetainPtr { |
+ public: |
+ explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) { |
+ if (m_pObj) |
+ m_pObj->Retain(); |
+ } |
+ |
+ CFX_RetainPtr() : CFX_RetainPtr(nullptr) {} |
+ CFX_RetainPtr(const CFX_RetainPtr<T>& that) : CFX_RetainPtr(that.Get()) {} |
+ CFX_RetainPtr(CFX_RetainPtr<T>&& that) { Swap(that); } |
+ |
+ template <class U> |
+ CFX_RetainPtr(const CFX_RetainPtr<U>& that) |
+ : CFX_RetainPtr(that.Get()) {} |
+ |
+ void Reset(T* obj = NULL) { |
dsinclair
2016/03/16 13:47:45
nit: nullptr
Tom Sepez
2016/03/16 19:12:55
Done.
|
+ CFX_RetainPtr<T> tmp(obj); |
+ Swap(tmp); |
+ } |
+ |
+ void Swap(CFX_RetainPtr<T>& that) { m_pObj.swap(that.m_pObj); } |
+ |
+ T* Get() const { return m_pObj.get(); } |
+ |
+ CFX_RetainPtr& operator=(const CFX_RetainPtr<T>& that) { |
+ if (*this != that) |
+ Reset(that.Get()); |
+ return *this; |
+ } |
+ |
+ bool operator==(const CFX_RetainPtr<T>& that) const { |
+ return Get() == that.Get(); |
+ } |
+ |
+ bool operator!=(const CFX_RetainPtr<T>& that) const { |
+ return !(*this == that); |
+ } |
+ |
+ operator bool() const { return !!m_pObj; } |
+ T& operator*() const { return *m_pObj.get(); } |
+ T* operator->() const { return m_pObj.get(); } |
+ |
+ protected: |
dsinclair
2016/03/16 13:47:45
Why not private?
Tom Sepez
2016/03/16 19:12:55
Done.
|
+ std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; |
+}; |
+ |
+#endif // CORE_INCLUDE_FXCRT_CFX_RETAIN_PTR_H_ |