Lagrange
Loading...
Searching...
No Matches
BitField.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <limits>
15#include <type_traits>
16
17namespace lagrange {
18
22
29template <typename EnumType_>
31{
32public:
34 using EnumType = EnumType_;
35
37 using UnderlyingType = std::underlying_type_t<EnumType>;
38
40 constexpr BitField()
41 : m_bits(0)
42 {}
43
49 constexpr BitField(EnumType value)
50 : m_bits(static_cast<UnderlyingType>(value))
51 {}
52
58 constexpr BitField(UnderlyingType bits)
59 : m_bits(bits)
60 {}
61
67 constexpr static BitField none() { return BitField(); }
68
74 constexpr static BitField all() { return ~BitField(); }
75
81 constexpr void set(const BitField& other) { m_bits |= other.m_bits; }
82
88 constexpr void clear(const BitField& other) { m_bits &= ~other.m_bits; }
89
93 constexpr void clear_all() { m_bits = UnderlyingType(0); }
94
102 constexpr bool test(const BitField& other) const
103 {
104 return (m_bits & other.m_bits) == other.m_bits;
105 }
106
114 constexpr bool test_any(const BitField& other) const { return (m_bits & other.m_bits) != 0; }
115
122 constexpr void set_bit(const BitField& other, bool is_set)
123 {
124 if (is_set) {
125 set(other);
126 } else {
127 clear(other);
128 }
129 }
130
134 constexpr operator UnderlyingType() const { return m_bits; }
135
141 constexpr const UnderlyingType& get_value() const { return m_bits; }
142
150 constexpr bool operator==(const BitField& other) const { return m_bits == other.m_bits; }
151
159 constexpr bool operator!=(const BitField& other) const { return m_bits != other.m_bits; }
160
168 constexpr BitField operator|(const BitField& other) const
169 {
170 return BitField(m_bits | other.m_bits);
171 }
172
180 constexpr BitField operator&(const BitField& other) const
181 {
182 return BitField(m_bits & other.m_bits);
183 }
184
192 constexpr BitField operator^(const BitField& other) const
193 {
194 return BitField(m_bits ^ other.m_bits);
195 }
196
202 constexpr BitField operator~() const { return BitField(~m_bits); }
203
204private:
205 static_assert(std::is_enum_v<EnumType>);
206
207 UnderlyingType m_bits = UnderlyingType(0);
208};
209
211
212} // namespace lagrange
constexpr BitField operator^(const BitField &other) const
Bitwise 'exclusive or' operator.
Definition BitField.h:192
constexpr BitField(EnumType value)
Constructor.
Definition BitField.h:49
std::underlying_type_t< EnumType > UnderlyingType
Underlying integral type representing the enum type.
Definition BitField.h:37
constexpr void set_bit(const BitField &other, bool is_set)
Allow to set the value of a bit.
Definition BitField.h:122
constexpr BitField(UnderlyingType bits)
Constructor.
Definition BitField.h:58
constexpr const UnderlyingType & get_value() const
Gets the underlying integral value.
Definition BitField.h:141
constexpr bool operator!=(const BitField &other) const
Different operator.
Definition BitField.h:159
constexpr void set(const BitField &other)
Set to 1 the specified bit.
Definition BitField.h:81
constexpr BitField operator&(const BitField &other) const
Bitwise 'and' operator.
Definition BitField.h:180
constexpr bool test_any(const BitField &other) const
Test if any bits are set.
Definition BitField.h:114
constexpr bool test(const BitField &other) const
Test the specified bit.
Definition BitField.h:102
constexpr void clear(const BitField &other)
Set to 0 the specified Bit.
Definition BitField.h:88
constexpr void clear_all()
Set all bits to 0.
Definition BitField.h:93
static constexpr BitField all()
Named constructor returning a bitfield set to 1.
Definition BitField.h:74
constexpr bool operator==(const BitField &other) const
Equal operator.
Definition BitField.h:150
constexpr BitField()
Default Constructor.
Definition BitField.h:40
constexpr BitField operator~() const
Bitwise 'one's complement' operator.
Definition BitField.h:202
EnumType_ EnumType
Enum type on top of which the bit field is built.
Definition BitField.h:34
constexpr BitField operator|(const BitField &other) const
Bitwise 'or' operator.
Definition BitField.h:168
static constexpr BitField none()
Named constructor returning a bitfield set to 0.
Definition BitField.h:67
Main namespace for Lagrange.