xref: /aosp_15_r20/external/pdfium/core/fxcrt/maybe_owned.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_MAYBE_OWNED_H_
6 #define CORE_FXCRT_MAYBE_OWNED_H_
7 
8 #include <memory>
9 #include <utility>
10 
11 #include "core/fxcrt/unowned_ptr.h"
12 #include "third_party/abseil-cpp/absl/types/variant.h"
13 
14 namespace fxcrt {
15 
16 // A template that can hold either owned or unowned references, and cleans up
17 // appropriately.  Possibly the most pernicious anti-pattern imaginable, but
18 // it crops up throughout the codebase due to a desire to avoid copying-in
19 // objects or data.
20 template <typename T, typename D = std::default_delete<T>>
21 class MaybeOwned {
22  public:
23   using OwnedType = std::unique_ptr<T, D>;
24   using UnownedType = UnownedPtr<T>;
25 
26   MaybeOwned() = default;
MaybeOwned(T * ptr)27   explicit MaybeOwned(T* ptr) : ptr_(UnownedType(ptr)) {}
MaybeOwned(const UnownedType & ptr)28   explicit MaybeOwned(const UnownedType& ptr) : ptr_(ptr) {}
MaybeOwned(OwnedType ptr)29   explicit MaybeOwned(OwnedType ptr) : ptr_(std::move(ptr)) {}
30 
31   MaybeOwned(const MaybeOwned& that) = delete;
32   MaybeOwned(MaybeOwned&& that) noexcept = default;
33 
34   MaybeOwned& operator=(const MaybeOwned& that) = delete;
35   MaybeOwned& operator=(MaybeOwned&& that) noexcept = default;
36 
37   ~MaybeOwned() = default;
38 
39   void Reset(T* ptr = nullptr) { ptr_ = UnownedType(ptr); }
Reset(OwnedType ptr)40   void Reset(OwnedType ptr) { ptr_ = std::move(ptr); }
41 
IsOwned()42   bool IsOwned() const { return absl::holds_alternative<OwnedType>(ptr_); }
43 
44   // Helpful for untangling a collection of intertwined MaybeOwned<>.
ResetIfUnowned()45   void ResetIfUnowned() {
46     if (!IsOwned())
47       Reset();
48   }
49 
Get()50   T* Get() const& {
51     return absl::visit([](const auto& obj) { return obj.get(); }, ptr_);
52   }
Get()53   T* Get() && {
54     auto local_variable_preventing_move_elision = std::move(ptr_);
55     return absl::visit([](const auto& obj) { return obj.get(); },
56                        local_variable_preventing_move_elision);
57   }
58 
59   // Downgrades to unowned, caller takes ownership.
Release()60   OwnedType Release() {
61     auto result = std::move(absl::get<OwnedType>(ptr_));
62     ptr_ = UnownedType(result.get());
63     return result;
64   }
65 
66   // Downgrades to empty, caller takes ownership.
ReleaseAndClear()67   OwnedType ReleaseAndClear() {
68     auto result = std::move(absl::get<OwnedType>(ptr_));
69     ptr_ = UnownedType();
70     return result;
71   }
72 
73   MaybeOwned& operator=(T* ptr) {
74     Reset(ptr);
75     return *this;
76   }
77   MaybeOwned& operator=(const UnownedType& ptr) {
78     Reset(ptr);
79     return *this;
80   }
81   MaybeOwned& operator=(OwnedType ptr) {
82     Reset(std::move(ptr));
83     return *this;
84   }
85 
86   bool operator==(const MaybeOwned& that) const { return Get() == that.Get(); }
87   bool operator==(const OwnedType& ptr) const { return Get() == ptr.get(); }
88   bool operator==(T* ptr) const { return Get() == ptr; }
89 
90   bool operator!=(const MaybeOwned& that) const { return !(*this == that); }
91   bool operator!=(const OwnedType ptr) const { return !(*this == ptr); }
92   bool operator!=(T* ptr) const { return !(*this == ptr); }
93 
94   explicit operator bool() const { return !!Get(); }
95   T& operator*() const { return *Get(); }
96   T* operator->() const { return Get(); }
97 
98  private:
99   absl::variant<UnownedType, OwnedType> ptr_;
100 };
101 
102 }  // namespace fxcrt
103 
104 using fxcrt::MaybeOwned;
105 
106 #endif  // CORE_FXCRT_MAYBE_OWNED_H_
107