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
maybe_owned.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_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
14namespace 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.
20template <typename T, typename D = std::default_delete<T>>
22 public:
23 using OwnedType = std::unique_ptr<T, D>;
24 using UnownedType = UnownedPtr<T>;
25
26 MaybeOwned() = default;
27 explicit MaybeOwned(T* ptr) : ptr_(UnownedType(ptr)) {}
28 explicit MaybeOwned(const UnownedType& ptr) : ptr_(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); }
40 void Reset(OwnedType ptr) { ptr_ = std::move(ptr); }
41
42 bool IsOwned() const { return absl::holds_alternative<OwnedType>(ptr_); }
43
44 // Helpful for untangling a collection of intertwined MaybeOwned<>.
46 if (!IsOwned())
47 Reset();
48 }
49
50 T* Get() const& {
51 return absl::visit([](const auto& obj) { return obj.get(); }, ptr_);
52 }
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.
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.
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
104using fxcrt::MaybeOwned;
105
106#endif // CORE_FXCRT_MAYBE_OWNED_H_
bool TransferBitmap(int dest_left, int dest_top, int width, int height, RetainPtr< const CFX_DIBBase > source, int src_left, int src_top)
bool SetAlphaFromBitmap(RetainPtr< const CFX_DIBBase > source)
bool SetUniformOpaqueAlpha()
bool Create(int width, int height, FXDIB_Format format)
pdfium::span< const uint8_t > GetBuffer() const
static absl::optional< PitchAndSize > CalculatePitchAndSize(int width, int height, FXDIB_Format format, uint32_t pitch)
bool CompositeMask(int dest_left, int dest_top, int width, int height, const RetainPtr< const CFX_DIBBase > &pMask, uint32_t color, int src_left, int src_top, BlendMode blend_type, const CFX_ClipRgn *pClipRgn, bool bRgbByteOrder)
bool SetRedFromBitmap(RetainPtr< const CFX_DIBBase > source)
bool MultiplyAlphaMask(RetainPtr< const CFX_DIBBase > source)
pdfium::span< uint8_t > GetWritableScanline(int line)
bool CompositeBitmap(int dest_left, int dest_top, int width, int height, RetainPtr< const CFX_DIBBase > source, int src_left, int src_top, BlendMode blend_type, const CFX_ClipRgn *pClipRgn, bool bRgbByteOrder)
bool ConvertColorScale(uint32_t forecolor, uint32_t backcolor)
bool ConvertFormat(FXDIB_Format format)
bool CompositeRect(int dest_left, int dest_top, int width, int height, uint32_t color)
pdfium::span< const uint8_t > GetScanline(int line) const override
size_t GetEstimatedImageMemoryBurden() const override
bool MultiplyAlpha(float alpha)
pdfium::span< uint8_t > GetWritableBuffer()
bool Create(int width, int height, FXDIB_Format format, uint8_t *pBuffer, uint32_t pitch)
void TakeOver(RetainPtr< CFX_DIBitmap > &&pSrcBitmap)
void Clear(uint32_t color)
bool Copy(RetainPtr< const CFX_DIBBase > source)
void CompositeOneBPPMask(int dest_left, int dest_top, int width, int height, RetainPtr< const CFX_DIBBase > source, int src_left, int src_top)
~CFX_DIBitmap() override
FX_RECT ToRoundedFxRect() const
CFX_FloatRect(const FX_RECT &rect)
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
void Scale(float fScale)
CFX_FloatRect TransformRect(const CFX_FloatRect &rect) const
float GetXUnit() const
virtual CFX_FloatRect CalcBoundingBox() const =0
const CFX_Matrix & matrix() const
static float TextUnitToGlyphUnit(float fTextUnit)
void Transform(CPDF_Font::FormIface *pForm, const CFX_Matrix &matrix)
void InitializeFromStreamData(bool bColored, pdfium::span< const float > pData)
RetainPtr< CFX_DIBitmap > GetBitmap()
static void TextUnitRectToGlyphUnitRect(CFX_FloatRect *pRect)
const FX_RECT & bbox() const
const CPDF_Font::FormIface * form() const
bool LoadBitmapFromSoleImageOfForm()
bool colored() const
void SetForm(std::unique_ptr< CPDF_Font::FormIface > pForm)
int width() const
bool IsOwned() const
Definition maybe_owned.h:42
MaybeOwned(OwnedType ptr)
Definition maybe_owned.h:29
MaybeOwned & operator=(const MaybeOwned &that)=delete
void Reset(T *ptr=nullptr)
Definition maybe_owned.h:39
OwnedType Release()
Definition maybe_owned.h:60
MaybeOwned & operator=(const UnownedType &ptr)
Definition maybe_owned.h:77
operator bool() const
Definition maybe_owned.h:94
bool operator==(const OwnedType &ptr) const
Definition maybe_owned.h:87
MaybeOwned & operator=(MaybeOwned &&that) noexcept=default
T * Get() const &
Definition maybe_owned.h:50
MaybeOwned(const MaybeOwned &that)=delete
bool operator==(const MaybeOwned &that) const
Definition maybe_owned.h:86
bool operator!=(const MaybeOwned &that) const
Definition maybe_owned.h:90
bool operator!=(T *ptr) const
Definition maybe_owned.h:92
MaybeOwned & operator=(T *ptr)
Definition maybe_owned.h:73
bool operator!=(const OwnedType ptr) const
Definition maybe_owned.h:91
OwnedType ReleaseAndClear()
Definition maybe_owned.h:67
MaybeOwned()=default
MaybeOwned & operator=(OwnedType ptr)
Definition maybe_owned.h:81
T & operator*() const
Definition maybe_owned.h:95
MaybeOwned(MaybeOwned &&that) noexcept=default
MaybeOwned(const UnownedType &ptr)
Definition maybe_owned.h:28
void Reset(OwnedType ptr)
Definition maybe_owned.h:40
T * operator->() const
Definition maybe_owned.h:96
bool operator==(T *ptr) const
Definition maybe_owned.h:88
~MaybeOwned()=default
BlendMode
Definition fx_dib.h:49
FXDIB_Format
Definition fx_dib.h:19
#define CONSTRUCT_VIA_MAKE_RETAIN
Definition retain_ptr.h:224
FX_RECT & operator=(const FX_RECT &that)=default
int32_t bottom
int32_t right
int32_t top
int32_t left