aboutsummaryrefslogtreecommitdiff
path: root/core/fxcrt/maybe_owned.h
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-12-13 18:05:38 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-12-13 18:05:38 +0000
commite03171e52ec4232369ab3b46e11d3a0abe1eb630 (patch)
tree6a57f6a0948032e6ec387c22d8b462215e6ca1e4 /core/fxcrt/maybe_owned.h
parent8bbfdf2fca7197a0eccfef7b6fc27c590a44e7f7 (diff)
parent326d96bf5d52fdcf790b467b13f58ba3df3e81e4 (diff)
downloadpdfium-e03171e52ec4232369ab3b46e11d3a0abe1eb630.tar.gz
Change-Id: If925f85c6a039b56ff549fd5327795f57b9cd12c
Diffstat (limited to 'core/fxcrt/maybe_owned.h')
-rw-r--r--core/fxcrt/maybe_owned.h92
1 files changed, 44 insertions, 48 deletions
diff --git a/core/fxcrt/maybe_owned.h b/core/fxcrt/maybe_owned.h
index bbdcd4047..c599d3925 100644
--- a/core/fxcrt/maybe_owned.h
+++ b/core/fxcrt/maybe_owned.h
@@ -1,4 +1,4 @@
-// Copyright 2016 PDFium Authors. All rights reserved.
+// Copyright 2016 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -8,8 +8,8 @@
#include <memory>
#include <utility>
-#include "core/fxcrt/fx_system.h"
#include "core/fxcrt/unowned_ptr.h"
+#include "third_party/abseil-cpp/absl/types/variant.h"
namespace fxcrt {
@@ -20,87 +20,83 @@ namespace fxcrt {
template <typename T, typename D = std::default_delete<T>>
class MaybeOwned {
public:
+ using OwnedType = std::unique_ptr<T, D>;
+ using UnownedType = UnownedPtr<T>;
+
MaybeOwned() = default;
- explicit MaybeOwned(T* ptr) : m_pObj(ptr) {}
- explicit MaybeOwned(const UnownedPtr<T>& ptr) : m_pObj(ptr.Get()) {}
- explicit MaybeOwned(std::unique_ptr<T, D> ptr)
- : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
+ explicit MaybeOwned(T* ptr) : ptr_(UnownedType(ptr)) {}
+ explicit MaybeOwned(const UnownedType& ptr) : ptr_(ptr) {}
+ explicit MaybeOwned(OwnedType ptr) : ptr_(std::move(ptr)) {}
MaybeOwned(const MaybeOwned& that) = delete;
- MaybeOwned(MaybeOwned&& that) noexcept
- : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
- that.m_pObj = nullptr;
- }
+ MaybeOwned(MaybeOwned&& that) noexcept = default;
+
+ MaybeOwned& operator=(const MaybeOwned& that) = delete;
+ MaybeOwned& operator=(MaybeOwned&& that) noexcept = default;
+
+ ~MaybeOwned() = default;
+
+ void Reset(T* ptr = nullptr) { ptr_ = UnownedType(ptr); }
+ void Reset(OwnedType ptr) { ptr_ = std::move(ptr); }
+
+ bool IsOwned() const { return absl::holds_alternative<OwnedType>(ptr_); }
- void Reset(std::unique_ptr<T, D> ptr) {
- m_pObj = ptr.get();
- m_pOwnedObj = std::move(ptr);
- }
- void Reset(T* ptr = nullptr) {
- m_pObj = ptr;
- m_pOwnedObj.reset();
- }
// Helpful for untangling a collection of intertwined MaybeOwned<>.
void ResetIfUnowned() {
if (!IsOwned())
Reset();
}
- T* Get() const { return m_pObj.Get(); }
- bool IsOwned() const { return !!m_pOwnedObj; }
+ T* Get() const& {
+ return absl::visit([](const auto& obj) { return obj.get(); }, ptr_);
+ }
+ T* Get() && {
+ auto local_variable_preventing_move_elision = std::move(ptr_);
+ return absl::visit([](const auto& obj) { return obj.get(); },
+ local_variable_preventing_move_elision);
+ }
// Downgrades to unowned, caller takes ownership.
- std::unique_ptr<T, D> Release() {
- ASSERT(IsOwned());
- return std::move(m_pOwnedObj);
+ OwnedType Release() {
+ auto result = std::move(absl::get<OwnedType>(ptr_));
+ ptr_ = UnownedType(result.get());
+ return result;
}
// Downgrades to empty, caller takes ownership.
- std::unique_ptr<T, D> ReleaseAndClear() {
- ASSERT(IsOwned());
- m_pObj = nullptr;
- return std::move(m_pOwnedObj);
+ OwnedType ReleaseAndClear() {
+ auto result = std::move(absl::get<OwnedType>(ptr_));
+ ptr_ = UnownedType();
+ return result;
}
- MaybeOwned& operator=(const MaybeOwned& that) = delete;
- MaybeOwned& operator=(MaybeOwned&& that) {
- m_pObj = that.m_pObj;
- m_pOwnedObj = std::move(that.m_pOwnedObj);
- that.m_pObj = nullptr;
- return *this;
- }
MaybeOwned& operator=(T* ptr) {
Reset(ptr);
return *this;
}
- MaybeOwned& operator=(const UnownedPtr<T>& ptr) {
- Reset(ptr.Get());
+ MaybeOwned& operator=(const UnownedType& ptr) {
+ Reset(ptr);
return *this;
}
- MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
+ MaybeOwned& operator=(OwnedType ptr) {
Reset(std::move(ptr));
return *this;
}
bool operator==(const MaybeOwned& that) const { return Get() == that.Get(); }
- bool operator==(const std::unique_ptr<T, D>& ptr) const {
- return Get() == ptr.get();
- }
+ bool operator==(const OwnedType& ptr) const { return Get() == ptr.get(); }
bool operator==(T* ptr) const { return Get() == ptr; }
bool operator!=(const MaybeOwned& that) const { return !(*this == that); }
- bool operator!=(const std::unique_ptr<T, D> ptr) const {
- return !(*this == ptr);
- }
+ bool operator!=(const OwnedType ptr) const { return !(*this == ptr); }
bool operator!=(T* ptr) const { return !(*this == ptr); }
- explicit operator bool() const { return !!m_pObj; }
- T& operator*() const { return *m_pObj; }
- T* operator->() const { return m_pObj.Get(); }
+ explicit operator bool() const { return !!Get(); }
+ T& operator*() const { return *Get(); }
+ T* operator->() const { return Get(); }
private:
- std::unique_ptr<T, D> m_pOwnedObj;
- UnownedPtr<T> m_pObj;
+ absl::variant<UnownedType, OwnedType> ptr_;
};
} // namespace fxcrt