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
mask.h
Go to the documentation of this file.
1// Copyright 2021 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_MASK_H_
6#define CORE_FXCRT_MASK_H_
7
8#include <type_traits>
9
10namespace fxcrt {
11
12// Provides extremely strict type-checking on masks of enum class bitflags,
13// for code where flags may not be passed consistently.
14template <typename E>
15class Mask {
16 public:
17 using UnderlyingType = typename std::underlying_type<E>::type;
18
19 // Escape hatch for when value comes aross an API, say.
20 static Mask FromUnderlyingUnchecked(UnderlyingType val) { return Mask(val); }
21
22 constexpr Mask() = default;
23 constexpr Mask(const Mask& that) = default;
24
25 // NOLINTNEXTLINE(runtime/explicit)
26 constexpr Mask(E val) : val_(static_cast<UnderlyingType>(val)) {}
27
28 // Unfortunately, std::initializer_list<> can't be used in constexpr
29 // methods per C++ standards, and we need constexpr for a zero-cost
30 // abstraction. Hence, expand out constructors of various arity.
31 constexpr Mask(E v1, E v2)
32 : val_(static_cast<UnderlyingType>(v1) |
33 static_cast<UnderlyingType>(v2)) {}
34
35 constexpr Mask(E v1, E v2, E v3)
36 : val_(static_cast<UnderlyingType>(v1) | static_cast<UnderlyingType>(v2) |
37 static_cast<UnderlyingType>(v3)) {}
38
39 constexpr Mask(E v1, E v2, E v3, E v4)
40 : val_(static_cast<UnderlyingType>(v1) | static_cast<UnderlyingType>(v2) |
41 static_cast<UnderlyingType>(v3) |
42 static_cast<UnderlyingType>(v4)) {}
43
44 constexpr Mask(E v1, E v2, E v3, E v4, E v5)
45 : val_(static_cast<UnderlyingType>(v1) | static_cast<UnderlyingType>(v2) |
46 static_cast<UnderlyingType>(v3) | static_cast<UnderlyingType>(v4) |
47 static_cast<UnderlyingType>(v5)) {}
48
49 constexpr Mask(E v1, E v2, E v3, E v4, E v5, E v6)
50 : val_(static_cast<UnderlyingType>(v1) | static_cast<UnderlyingType>(v2) |
51 static_cast<UnderlyingType>(v3) | static_cast<UnderlyingType>(v4) |
52 static_cast<UnderlyingType>(v5) |
53 static_cast<UnderlyingType>(v6)) {}
54
55 constexpr Mask(E v1, E v2, E v3, E v4, E v5, E v6, E v7)
56 : val_(static_cast<UnderlyingType>(v1) | static_cast<UnderlyingType>(v2) |
57 static_cast<UnderlyingType>(v3) | static_cast<UnderlyingType>(v4) |
58 static_cast<UnderlyingType>(v5) | static_cast<UnderlyingType>(v6) |
59 static_cast<UnderlyingType>(v7)) {}
60
61 constexpr Mask(E v1, E v2, E v3, E v4, E v5, E v6, E v7, E v8)
62 : val_(static_cast<UnderlyingType>(v1) | static_cast<UnderlyingType>(v2) |
63 static_cast<UnderlyingType>(v3) | static_cast<UnderlyingType>(v4) |
64 static_cast<UnderlyingType>(v5) | static_cast<UnderlyingType>(v6) |
65 static_cast<UnderlyingType>(v7) |
66 static_cast<UnderlyingType>(v8)) {}
67
68 explicit operator bool() const { return !!val_; }
69 Mask operator~() const { return Mask(~val_); }
70 constexpr Mask operator|(const Mask& that) const {
71 return Mask(val_ | that.val_);
72 }
73 constexpr Mask operator&(const Mask& that) const {
74 return Mask(val_ & that.val_);
75 }
76 constexpr Mask operator^(const Mask& that) const {
77 return Mask(val_ ^ that.val_);
78 }
79 Mask& operator=(const Mask& that) {
80 val_ = that.val_;
81 return *this;
82 }
83 Mask& operator|=(const Mask& that) {
84 val_ |= that.val_;
85 return *this;
86 }
87 Mask& operator&=(const Mask& that) {
88 val_ &= that.val_;
89 return *this;
90 }
91 Mask& operator^=(const Mask& that) {
92 val_ ^= that.val_;
93 return *this;
94 }
95 bool operator==(const Mask& that) const { return val_ == that.val_; }
96 bool operator!=(const Mask& that) const { return val_ != that.val_; }
97
98 bool TestAll(const Mask& that) const {
99 return (val_ & that.val_) == that.val_;
100 }
101
102 // Because ~ can't be applied to enum class without casting.
103 void Clear(const Mask& that) { val_ &= ~that.val_; }
104
105 // Escape hatch, usage should be minimized.
106 UnderlyingType UncheckedValue() const { return val_; }
107
108 private:
109 explicit constexpr Mask(UnderlyingType val) : val_(val) {}
110
111 UnderlyingType val_ = 0;
112};
113
114} // namespace fxcrt
115
116using fxcrt::Mask;
117
118#endif // CORE_FXCRT_MASK_H_
CFX_CSSFontVariant
Definition cfx_css.h:82
CFX_CSSFontStyle
Definition cfx_css.h:57
CFX_CSSDisplay
Definition cfx_css.h:48
CFX_CSSVerticalAlign
Definition cfx_css.h:70
CFX_CSSTextAlign
Definition cfx_css.h:62
CFX_CSSLengthUnit
Definition cfx_css.h:40
CFX_CSSTEXTDECORATION
Definition cfx_css.h:87
RetainPtr< CFX_CSSValueList > m_pFontFamily
Mask< CFX_CSSTEXTDECORATION > m_dwTextDecoration
void SetTextAlign(CFX_CSSTextAlign eTextAlign)
void SetNumberVerticalAlign(float fAlign)
void SetMarginWidth(const CFX_CSSRect &rect)
void SetTextDecoration(Mask< CFX_CSSTEXTDECORATION > dwTextDecoration)
void SetTextIndent(const CFX_CSSLength &textIndent)
const CFX_CSSRect * GetBorderWidth() const
void SetPaddingWidth(const CFX_CSSRect &rect)
void SetFontWeight(uint16_t wFontWeight)
void SetFontVariant(CFX_CSSFontVariant eFontVariant)
void SetFontStyle(CFX_CSSFontStyle eFontStyle)
void SetColor(FX_ARGB dwFontColor)
void SetFontSize(float fFontSize)
const CFX_CSSLength & GetTextIndent() const
void AddCustomStyle(const CFX_CSSCustomProperty &prop)
NonInheritedData m_NonInheritedData
CFX_CSSVerticalAlign GetVerticalAlign() const
Mask< CFX_CSSTEXTDECORATION > GetTextDecoration() const
CFX_CSSFontStyle GetFontStyle() const
CFX_CSSFontVariant GetFontVariant() const
const CFX_CSSRect * GetMarginWidth() const
absl::optional< WideString > GetLastFontFamily() const
~CFX_CSSComputedStyle() override
const CFX_CSSLength & GetLetterSpacing() const
CFX_CSSTextAlign GetTextAlign() const
bool GetCustomStyle(const WideString &wsName, WideString *pValue) const
CFX_CSSDisplay GetDisplay() const
void SetLetterSpacing(const CFX_CSSLength &letterSpacing)
const CFX_CSSRect * GetPaddingWidth() const
void SetLineHeight(float fLineHeight)
CFX_CSSCustomProperty(const WideString &name, const WideString &value)
CFX_CSSCustomProperty(const CFX_CSSCustomProperty &prop)
CFX_CSSLength(CFX_CSSLengthUnit eUnit, float fValue)
Definition cfx_css.h:100
CFX_CSSRect(CFX_CSSLengthUnit eUnit, float val)
Definition cfx_css.h:128
constexpr Mask(E v1, E v2, E v3, E v4, E v5, E v6)
Definition mask.h:49
bool operator==(const Mask &that) const
Definition mask.h:95
constexpr Mask operator^(const Mask &that) const
Definition mask.h:76
constexpr Mask(E v1, E v2, E v3, E v4, E v5, E v6, E v7, E v8)
Definition mask.h:61
static Mask FromUnderlyingUnchecked(UnderlyingType val)
Definition mask.h:20
Mask & operator|=(const Mask &that)
Definition mask.h:83
constexpr Mask(E v1, E v2, E v3, E v4)
Definition mask.h:39
void Clear(const Mask &that)
Definition mask.h:103
constexpr Mask(E v1, E v2, E v3)
Definition mask.h:35
constexpr Mask operator|(const Mask &that) const
Definition mask.h:70
constexpr Mask(E v1, E v2)
Definition mask.h:31
Mask operator~() const
Definition mask.h:69
Mask & operator&=(const Mask &that)
Definition mask.h:87
Mask & operator^=(const Mask &that)
Definition mask.h:91
bool operator!=(const Mask &that) const
Definition mask.h:96
operator bool() const
Definition mask.h:68
constexpr Mask(const Mask &that)=default
constexpr Mask()=default
Mask & operator=(const Mask &that)
Definition mask.h:79
UnderlyingType UncheckedValue() const
Definition mask.h:106
bool TestAll(const Mask &that) const
Definition mask.h:98
constexpr Mask(E val)
Definition mask.h:26
constexpr Mask(E v1, E v2, E v3, E v4, E v5, E v6, E v7)
Definition mask.h:55
constexpr Mask operator&(const Mask &that) const
Definition mask.h:73
constexpr Mask(E v1, E v2, E v3, E v4, E v5)
Definition mask.h:44
#define CONSTRUCT_VIA_MAKE_RETAIN
Definition retain_ptr.h:224