Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
retain_ptr.h
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CORE_FXCRT_RETAIN_PTR_H_
6#define CORE_FXCRT_RETAIN_PTR_H_
7
8#include <stdint.h>
9
10#include <functional>
11#include <memory>
12#include <type_traits>
13#include <utility>
14
15#include "core/fxcrt/check.h"
16#include "core/fxcrt/compiler_specific.h"
17
18namespace fxcrt {
19
20// Used with std::unique_ptr to Release() objects that can't be deleted.
21template <class T>
23 inline void operator()(T* ptr) const { ptr->Release(); }
24};
25
26// Analogous to base's scoped_refptr.
27template <class T>
29 public:
30 RetainPtr() noexcept = default;
31
32 // Deliberately implicit to allow returning nullptrs.
33 // NOLINTNEXTLINE(runtime/explicit)
34 RetainPtr(std::nullptr_t ptr) {}
35
36 explicit RetainPtr(T* pObj) noexcept : m_pObj(pObj) {
37 if (m_pObj)
38 m_pObj->Retain();
39 }
40
41 // Copy-construct a RetainPtr.
42 // Required in addition to copy conversion constructor below.
43 RetainPtr(const RetainPtr& that) noexcept : RetainPtr(that.Get()) {}
44
45 // Move-construct a RetainPtr. After construction, |that| will be NULL.
46 // Required in addition to move conversion constructor below.
47 RetainPtr(RetainPtr&& that) noexcept { Unleak(that.Leak()); }
48
49 // Copy conversion constructor.
50 template <class U,
51 typename = typename std::enable_if<
53 RetainPtr(const RetainPtr<U>& that) : RetainPtr(that.Get()) {}
54
55 // Move-conversion constructor.
56 template <class U,
57 typename = typename std::enable_if<
59 RetainPtr(RetainPtr<U>&& that) noexcept {
60 Unleak(that.Leak());
61 }
62
63 // Assign a RetainPtr from nullptr;
64 RetainPtr& operator=(std::nullptr_t) noexcept {
65 Reset();
66 return *this;
67 }
68
69 // Copy-assign a RetainPtr.
70 // Required in addition to copy conversion assignment below.
71 RetainPtr& operator=(const RetainPtr& that) {
72 if (*this != that)
73 Reset(that.Get());
74 return *this;
75 }
76
77 // Move-assign a RetainPtr. After assignment, |that| will be NULL.
78 // Required in addition to move conversion assignment below.
79 RetainPtr& operator=(RetainPtr&& that) noexcept {
81 return *this;
82 }
83
84 // Copy-convert assign a RetainPtr.
85 template <class U,
86 typename = typename std::enable_if<
88 RetainPtr& operator=(const RetainPtr<U>& that) {
89 if (*this != that)
90 Reset(that.Get());
91 return *this;
92 }
93
94 // Move-convert assign a RetainPtr. After assignment, |that| will be NULL.
95 template <class U,
96 typename = typename std::enable_if<
98 RetainPtr& operator=(RetainPtr<U>&& that) noexcept {
99 Unleak(that.Leak());
100 return *this;
101 }
102
103 ~RetainPtr() = default;
104
105 template <class U>
106 U* AsRaw() const {
107 return static_cast<U*>(Get());
108 }
109
110 template <class U>
111 RetainPtr<U> As() const {
112 return RetainPtr<U>(AsRaw<U>());
113 }
114
115 void Reset(T* obj = nullptr) {
116 if (obj)
117 obj->Retain();
118 m_pObj.reset(obj);
119 }
120
121 operator T*() const noexcept { return Get(); }
122 T* Get() const noexcept { return m_pObj.get(); }
123
124 void Swap(RetainPtr& that) { m_pObj.swap(that.m_pObj); }
125
126 // Useful for passing notion of object ownership across a C API.
127 T* Leak() { return m_pObj.release(); }
128 void Unleak(T* ptr) { m_pObj.reset(ptr); }
129
130 bool operator==(const RetainPtr& that) const { return Get() == that.Get(); }
131 bool operator!=(const RetainPtr& that) const { return !(*this == that); }
132
133 template <typename U>
134 bool operator==(const U& that) const {
135 return Get() == that;
136 }
137
138 template <typename U>
139 bool operator!=(const U& that) const {
140 return !(*this == that);
141 }
142
143 bool operator<(const RetainPtr& that) const {
144 return std::less<T*>()(Get(), that.Get());
145 }
146
147 explicit operator bool() const { return !!m_pObj; }
148 T& operator*() const { return *m_pObj; }
149 T* operator->() const { return m_pObj.get(); }
150
151 private:
152 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj;
153};
154
155// Trivial implementation - internal ref count with virtual destructor.
157 public:
158 Retainable() = default;
159
160 bool HasOneRef() const { return m_nRefCount == 1; }
161
162 protected:
163 virtual ~Retainable() = default;
164
165 private:
166 template <typename U>
167 friend struct ReleaseDeleter;
168
169 template <typename U>
170 friend class RetainPtr;
171
172 Retainable(const Retainable& that) = delete;
173 Retainable& operator=(const Retainable& that) = delete;
174
175 // These need to be const methods operating on a mutable member so that
176 // RetainPtr<const T> can be used for an object that is otherwise const
177 // apart from the internal ref-counting.
178 void Retain() const {
179 ++m_nRefCount;
180 CHECK(m_nRefCount > 0);
181 }
182 void Release() const {
183 CHECK(m_nRefCount > 0);
184 if (--m_nRefCount == 0)
185 delete this;
186 }
187
188 mutable uintptr_t m_nRefCount = 0;
189 static_assert(std::is_unsigned<decltype(m_nRefCount)>::value,
190 "m_nRefCount must be an unsigned type for overflow check"
191 "to work properly in Retain()");
192};
193
194} // namespace fxcrt
195
196using fxcrt::ReleaseDeleter;
197using fxcrt::Retainable;
198using fxcrt::RetainPtr;
199
200namespace pdfium {
201
202// Helper to make a RetainPtr along the lines of std::make_unique<>().
203// Arguments are forwarded to T's constructor. Classes managed by RetainPtr
204// should have protected (or private) constructors, and should friend this
205// function.
206template <typename T, typename... Args>
207RetainPtr<T> MakeRetain(Args&&... args) {
208 return RetainPtr<T>(new T(std::forward<Args>(args)...));
209}
210
211// Type-deducing wrapper to make a RetainPtr from an ordinary pointer,
212// since equivalent constructor is explicit.
213template <typename T>
215 return RetainPtr<T>(that);
216}
217
218} // namespace pdfium
219
220// Macro to allow construction via MakeRetain<>() only, when used
221// with a private constructor in a class.
222#define CONSTRUCT_VIA_MAKE_RETAIN
223 template <typename T, typename... Args>
224 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args)
225
226#endif // CORE_FXCRT_RETAIN_PTR_H_
uint32_t FX_HashCode_GetLoweredAsIfW(ByteStringView str)
fxcrt::ByteString ByteString
Definition bytestring.h:180
uint32_t FX_HashCode_GetLoweredA(ByteStringView str)
uint32_t FX_HashCode_GetAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetA(ByteStringView str)
uint32_t GetID() const
Definition bytestring.h:98
ByteString(const char *ptr)
bool EqualNoCase(ByteStringView str) const
static ByteString Format(const char *pFormat,...)
ByteString & operator+=(const ByteString &str)
bool operator!=(ByteStringView str) const
Definition bytestring.h:66
ByteString Substr(size_t first, size_t count) const
bool operator==(ByteStringView str) const
ByteString()=default
intptr_t ReferenceCountForTesting() const
ByteString & operator+=(char ch)
ByteString(ByteStringView bstrc)
static ByteString FormatInteger(int i)
bool operator==(const ByteString &other) const
bool operator==(const char *ptr) const
UNSAFE_BUFFER_USAGE ByteString(const char *pStr, size_t len)
bool operator<(ByteStringView str) const
ByteString(wchar_t)=delete
ByteString & operator+=(const char *str)
ByteString & operator+=(ByteStringView str)
~ByteString()=default
UNSAFE_BUFFER_USAGE ByteString(const uint8_t *pStr, size_t len)
bool operator!=(const ByteString &other) const
Definition bytestring.h:67
ByteString & operator=(ByteStringView str)
ByteString(ByteStringView str1, ByteStringView str2)
ByteString & operator=(const char *str)
static ByteString FormatV(const char *pFormat, va_list argList)
ByteString Substr(size_t offset) const
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
ByteString(ByteString &&other) noexcept=default
bool operator<(const ByteString &other) const
ByteString(const std::initializer_list< ByteStringView > &list)
bool operator!=(const char *ptr) const
Definition bytestring.h:65
int Compare(ByteStringView str) const
ByteString(const ByteString &other)=default
ByteString(const fxcrt::ostringstream &outStream)
ByteString First(size_t count) const
bool operator<(const char *ptr) const
ByteString Last(size_t count) const
U * AsRaw() const
Definition retain_ptr.h:106
RetainPtr & operator=(const RetainPtr< U > &that)
Definition retain_ptr.h:88
operator bool() const
Definition retain_ptr.h:147
void Unleak(T *ptr)
Definition retain_ptr.h:128
RetainPtr(std::nullptr_t ptr)
Definition retain_ptr.h:34
void Reset(T *obj=nullptr)
Definition retain_ptr.h:115
bool operator!=(const RetainPtr &that) const
Definition retain_ptr.h:131
void Swap(RetainPtr &that)
Definition retain_ptr.h:124
RetainPtr() noexcept=default
RetainPtr(RetainPtr &&that) noexcept
Definition retain_ptr.h:47
bool operator!=(const U &that) const
Definition retain_ptr.h:139
RetainPtr & operator=(const RetainPtr &that)
Definition retain_ptr.h:71
bool operator==(const RetainPtr &that) const
Definition retain_ptr.h:130
T & operator*() const
Definition retain_ptr.h:148
bool operator==(const U &that) const
Definition retain_ptr.h:134
RetainPtr & operator=(RetainPtr &&that) noexcept
Definition retain_ptr.h:79
T * operator->() const
Definition retain_ptr.h:149
RetainPtr & operator=(RetainPtr< U > &&that) noexcept
Definition retain_ptr.h:98
RetainPtr(const RetainPtr< U > &that)
Definition retain_ptr.h:53
T * Get() const noexcept
Definition retain_ptr.h:122
~RetainPtr()=default
bool operator<(const RetainPtr &that) const
Definition retain_ptr.h:143
RetainPtr(const RetainPtr &that) noexcept
Definition retain_ptr.h:43
operator T*() const noexcept
Definition retain_ptr.h:121
RetainPtr & operator=(std::nullptr_t) noexcept
Definition retain_ptr.h:64
RetainPtr(RetainPtr< U > &&that) noexcept
Definition retain_ptr.h:59
RetainPtr< U > As() const
Definition retain_ptr.h:111
RetainPtr(T *pObj) noexcept
Definition retain_ptr.h:36
bool HasOneRef() const
Definition retain_ptr.h:160
Retainable()=default
virtual ~Retainable()=default
pdfium::span< T > GetBuffer(size_t nMinBufLength)
size_t Delete(size_t index, size_t count=1)
const_iterator begin() const
void AllocBeforeWrite(size_t nNewLen)
pdfium::span< const UnsignedType > unsigned_span_with_terminator() const
size_t Replace(StringView oldstr, StringView newstr)
StringTemplate(const StringTemplate &other)=default
pdfium::span< const CharType > span_with_terminator() const
StringView AsStringView() const
void ReleaseBuffer(size_t nNewLength)
void Reserve(size_t len)
CharType operator[](const size_t index) const
CharType Front() const
pdfium::span< const UnsignedType > unsigned_span() const
StringDataTemplate< T > StringData
std::optional< size_t > ReverseFind(T ch) const
StringViewTemplate< T > StringView
void AssignCopy(const T *pSrcData, size_t nSrcLen)
RetainPtr< StringData > m_pData
std::optional< size_t > Find(StringView str, size_t start=0) const
void SetAt(size_t index, T ch)
const_reverse_iterator rbegin() const
const_iterator end() const
bool IsValidIndex(size_t index) const
bool Contains(T ch, size_t start=0) const
typename std::make_unsigned< CharType >::type UnsignedType
void TrimBack(StringView targets)
void Trim(StringView targets)
StringTemplate(StringTemplate &&other) noexcept=default
std::reverse_iterator< const_iterator > const_reverse_iterator
bool Contains(StringView str, size_t start=0) const
void Concat(const T *pSrcData, size_t nSrcLen)
CharType Back() const
std::optional< size_t > Find(T ch, size_t start=0) const
size_t GetLength() const
pdfium::span< const CharType > span() const
const UnsignedType * unsigned_str() const
bool IsValidLength(size_t length) const
size_t GetStringLength() const
void ReallocBeforeWrite(size_t nNewLen)
void TrimFront(StringView targets)
size_t Insert(size_t index, T ch)
const_reverse_iterator rend() const
const CharType * c_str() const
#define UNSAFE_BUFFERS(...)
#define TRIVIAL_ABI
#define UNSAFE_BUFFER_USAGE
CRYPT_md5_context CRYPT_MD5Start()
Definition fx_crypt.cpp:164
TEST(FXCRYPT, CryptToBase16)
TEST(FXCRYPT, MD5GenerateEmtpyData)
std::string CryptToBase16(const uint8_t *digest)
Definition hash.cpp:9
bool operator==(const char *lhs, const ByteString &rhs)
Definition bytestring.h:109
bool operator<(const ByteStringView &lhs, const char *rhs)
Definition bytestring.h:127
ByteString operator+(const ByteString &str1, const ByteString &str2)
Definition bytestring.h:146
ByteString operator+(ByteStringView str1, const char *str2)
Definition bytestring.h:134
bool operator<(const ByteStringView &lhs, const ByteString &rhs)
Definition bytestring.h:124
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:149
ByteString operator+(const ByteString &str1, const char *str2)
Definition bytestring.h:155
void PrintTo(const ByteString &str, std::ostream *os)
ByteString operator+(char ch, const ByteString &str2)
Definition bytestring.h:152
ByteString operator+(const ByteString &str1, ByteStringView str2)
Definition bytestring.h:161
ByteString operator+(const char *str1, const ByteString &str2)
Definition bytestring.h:158
constexpr const wchar_t * EmptyString(wchar_t *)
StringViewTemplate< char > ByteStringView
bool operator!=(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:118
bool operator!=(const char *lhs, const ByteString &rhs)
Definition bytestring.h:115
ByteString operator+(ByteStringView str1, ByteStringView str2)
Definition bytestring.h:131
ByteString operator+(ByteStringView str1, char ch)
Definition bytestring.h:140
constexpr const char * EmptyString(char *)
bool operator<(const char *lhs, const ByteString &rhs)
Definition bytestring.h:121
ByteString operator+(char ch, ByteStringView str2)
Definition bytestring.h:143
ByteString operator+(ByteStringView str1, const ByteString &str2)
Definition bytestring.h:164
ByteString operator+(const char *str1, ByteStringView str2)
Definition bytestring.h:137
bool operator==(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:112
RetainPtr< T > MakeRetain(Args &&... args)
Definition retain_ptr.h:207
RetainPtr< T > WrapRetain(T *that)
Definition retain_ptr.h:214
#define CHECK(cvref)
fxcrt::ByteStringView ByteStringView
void operator()(T *ptr) const
Definition retain_ptr.h:23
size_t operator()(const ByteString &str) const
Definition bytestring.h:191