comparison mupdf-source/thirdparty/zxing-cpp/core/src/Flags.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * Copyright 2020 Axel Waggershauser
3 */
4 // SPDX-License-Identifier: Apache-2.0
5
6 #pragma once
7
8 #include "BitHacks.h"
9
10 #include <cstddef>
11 #include <iterator>
12 #include <type_traits>
13
14 namespace ZXing {
15
16 template<typename Enum>
17 class Flags
18 {
19 static_assert(std::is_enum<Enum>::value, "Flags is only usable on enumeration types.");
20
21 using Int = typename std::underlying_type<Enum>::type;
22 Int i = 0;
23
24 constexpr inline Flags(Int other) : i(other) {}
25 constexpr static inline unsigned highestBitSet(Int x) noexcept { return x < 2 ? x : 1 + highestBitSet(x >> 1); }
26
27 public:
28 using enum_type = Enum;
29
30 constexpr inline Flags() noexcept = default;
31 constexpr inline Flags(Enum flag) noexcept : i(Int(flag)) {}
32
33 // constexpr inline Flags(std::initializer_list<Enum> flags) noexcept
34 // : i(initializer_list_helper(flags.begin(), flags.end()))
35 // {}
36
37 class iterator
38 {
39 friend class Flags;
40 const Int _flags = 0;
41 int _pos = 0;
42 iterator(Int i, int p) : _flags(i), _pos(p) {}
43 public:
44 using iterator_category = std::input_iterator_tag;
45 using value_type = Enum;
46 using difference_type = std::ptrdiff_t;
47 using pointer = Enum*;
48 using reference = Enum&;
49
50 Enum operator*() const noexcept { return Enum(1 << _pos); }
51
52 iterator& operator++() noexcept
53 {
54 while (++_pos < BitHacks::HighestBitSet(_flags) && !((1 << _pos) & _flags))
55 ;
56 return *this;
57 }
58
59 bool operator==(const iterator& rhs) const noexcept { return _pos == rhs._pos; }
60 bool operator!=(const iterator& rhs) const noexcept { return !(*this == rhs); }
61 };
62
63 iterator begin() const noexcept { return {i, BitHacks::NumberOfTrailingZeros(i)}; }
64 iterator end() const noexcept { return {i, BitHacks::HighestBitSet(i) + 1}; }
65
66 bool empty() const noexcept { return i == 0; }
67 int count() const noexcept { return BitHacks::CountBitsSet(i); }
68
69 constexpr inline bool operator==(Flags other) const noexcept { return i == other.i; }
70
71 inline Flags& operator&=(Flags mask) noexcept { return i &= mask.i, *this; }
72 inline Flags& operator&=(Enum mask) noexcept { return i &= Int(mask), *this; }
73 inline Flags& operator|=(Flags other) noexcept { return i |= other.i, *this; }
74 inline Flags& operator|=(Enum other) noexcept { return i |= Int(other), *this; }
75 // inline Flags &operator^=(Flags other) noexcept { return i ^= other.i, *this; }
76 // inline Flags &operator^=(Enum other) noexcept { return i ^= Int(other), *this; }
77
78 constexpr inline Flags operator&(Flags other) const noexcept { return i & other.i; }
79 constexpr inline Flags operator&(Enum other) const noexcept { return i & Int(other); }
80 constexpr inline Flags operator|(Flags other) const noexcept { return i | other.i; }
81 constexpr inline Flags operator|(Enum other) const noexcept { return i | Int(other); }
82 // constexpr inline Flags operator^(Flags other) const noexcept { return i ^ other.i; }
83 // constexpr inline Flags operator^(Enum other) const noexcept { return i ^ Int(other); }
84 // constexpr inline Flags operator~() const noexcept { return ~i; }
85
86 // constexpr inline operator Int() const noexcept { return i; }
87 // constexpr inline bool operator!() const noexcept { return !i; }
88 // constexpr inline Int asInt() const noexcept { return i; }
89
90 constexpr inline bool testFlag(Enum flag) const noexcept
91 {
92 return (i & Int(flag)) == Int(flag) && (Int(flag) != 0 || i == Int(flag));
93 }
94 constexpr inline bool testFlags(Flags mask) const noexcept { return i & mask.i; }
95 inline Flags& setFlag(Enum flag, bool on = true) noexcept
96 {
97 return on ? (*this |= flag) : (*this &= ~Int(flag));
98 }
99 inline void clear() noexcept { i = 0; }
100
101 constexpr static Flags all() noexcept { return ~(unsigned(~0) << highestBitSet(Int(Enum::_max))); }
102
103 private:
104 // constexpr static inline Int
105 // initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
106 // typename std::initializer_list<Enum>::const_iterator end) noexcept
107 // {
108 // return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
109 // }
110 };
111
112 #define ZX_DECLARE_FLAGS(FLAGS, ENUM) \
113 using FLAGS = Flags<ENUM>; \
114 constexpr inline FLAGS operator|(FLAGS::enum_type e1, FLAGS::enum_type e2) noexcept { return FLAGS(e1) | e2; } \
115 constexpr inline FLAGS operator|(FLAGS::enum_type e, FLAGS f) noexcept { return f | e; } \
116 constexpr inline bool operator==(FLAGS::enum_type e, FLAGS f) noexcept { return FLAGS(e) == f; } \
117 constexpr inline bool operator==(FLAGS f, FLAGS::enum_type e) noexcept { return FLAGS(e) == f; }
118
119 } // ZXing
120