summaryrefslogtreecommitdiff
path: root/core/fxcrt/include
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-09-28 14:49:01 -0700
committerCommit bot <commit-bot@chromium.org>2016-09-28 14:49:01 -0700
commit4ba37c6f6964f6a24fc4b8b48bc82c02edb70370 (patch)
treea9b0787c38d9b7dbedcd19390144c6da1c222510 /core/fxcrt/include
parent7c292e0f67e0f0bf4062e0b2adf244e17d7dacb5 (diff)
downloadpdfium-4ba37c6f6964f6a24fc4b8b48bc82c02edb70370.tar.xz
Implement weak pointerschromium/2876chromium/2875
These will be a replacement for CFX_CountRef in future CLs, since CFX_CountRef is manually incremented and error-prone. Review-Url: https://codereview.chromium.org/2377143002
Diffstat (limited to 'core/fxcrt/include')
-rw-r--r--core/fxcrt/include/cfx_weak_ptr.h80
1 files changed, 80 insertions, 0 deletions
diff --git a/core/fxcrt/include/cfx_weak_ptr.h b/core/fxcrt/include/cfx_weak_ptr.h
new file mode 100644
index 0000000000..64218cd679
--- /dev/null
+++ b/core/fxcrt/include/cfx_weak_ptr.h
@@ -0,0 +1,80 @@
+// 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.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef CORE_FXCRT_INCLUDE_CFX_WEAK_PTR_H_
+#define CORE_FXCRT_INCLUDE_CFX_WEAK_PTR_H_
+
+#include <memory>
+
+#include "core/fxcrt/include/cfx_retain_ptr.h"
+#include "core/fxcrt/include/fx_system.h"
+
+template <class T, class D = std::default_delete<T>>
+class CFX_WeakPtr {
+ public:
+ CFX_WeakPtr() {}
+ CFX_WeakPtr(const CFX_WeakPtr& that) : m_pHandle(that.m_pHandle) {}
+ CFX_WeakPtr(CFX_WeakPtr&& that) { Swap(that); }
+ CFX_WeakPtr(std::unique_ptr<T, D> pObj)
+ : m_pHandle(new Handle(std::move(pObj))) {}
+
+ explicit operator bool() const { return m_pHandle && !!m_pHandle->Get(); }
+ bool HasOneRef() const { return m_pHandle && m_pHandle->HasOneRef(); }
+ T* operator->() { return m_pHandle->Get(); }
+ const T* operator->() const { return m_pHandle->Get(); }
+ CFX_WeakPtr& operator=(const CFX_WeakPtr& that) {
+ m_pHandle = that.m_pHandle;
+ return *this;
+ }
+ bool operator==(const CFX_WeakPtr& that) const {
+ return m_pHandle == that.m_pHandle;
+ }
+ bool operator!=(const CFX_WeakPtr& that) const { return !(*this == that); }
+
+ T* Get() const { return m_pHandle ? m_pHandle->Get() : nullptr; }
+ void Clear() {
+ if (m_pHandle) {
+ m_pHandle->Clear();
+ m_pHandle.Reset();
+ }
+ }
+ void Reset() { m_pHandle.Reset(); }
+ void Reset(std::unique_ptr<T, D> pObj) {
+ m_pHandle.Reset(new Handle(std::move(pObj)));
+ }
+ void Swap(CFX_WeakPtr& that) { m_pHandle.Swap(that.m_pHandle); }
+
+ private:
+ class Handle {
+ public:
+ explicit Handle(std::unique_ptr<T, D> ptr)
+ : m_nCount(0), m_pObj(std::move(ptr)) {}
+ void Reset(std::unique_ptr<T, D> ptr) { m_pObj = std::move(ptr); }
+ void Clear() { // Now you're all weak ptrs ...
+ m_pObj.reset(); // unique_ptr nulls first before invoking delete.
+ }
+ T* Get() const { return m_pObj.get(); }
+ T* Retain() {
+ ++m_nCount;
+ return m_pObj.get();
+ }
+ void Release() {
+ if (--m_nCount == 0)
+ delete this;
+ }
+ bool HasOneRef() const { return m_nCount == 1; }
+
+ private:
+ ~Handle() {}
+
+ intptr_t m_nCount;
+ std::unique_ptr<T, D> m_pObj;
+ };
+
+ CFX_RetainPtr<Handle> m_pHandle;
+};
+
+#endif // CORE_FXCRT_INCLUDE_CFX_WEAK_PTR_H_