blob: f679696992b93bd4fe0f72fb8821c50e4d3f071c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
// 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_CFX_WEAK_PTR_H_
#define CORE_FXCRT_CFX_WEAK_PTR_H_
#include <cstddef>
#include <memory>
#include "core/fxcrt/cfx_retain_ptr.h"
#include "core/fxcrt/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))) {}
// Deliberately implicit to allow passing nullptr.
CFX_WeakPtr(std::nullptr_t arg) {}
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 DeleteObject() {
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_CFX_WEAK_PTR_H_
|