Lagrange
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
24
31template <typename EnumType_>
33{
34public:
36 using EnumType = EnumType_;
37
39 using UnderlyingType = std::underlying_type_t<EnumType>;
40
42 constexpr BitField()
43 : m_bits(0)
44 {}
45
51 constexpr BitField(EnumType value)
52 : m_bits(static_cast<UnderlyingType>(value))
53 {}
54
60 constexpr BitField(UnderlyingType bits)
61 : m_bits(bits)
62 {}
63
69 constexpr static BitField none() { return BitField(); }
70
76 constexpr static BitField all() { return ~BitField(); }
77
83 constexpr void set(const BitField& other) { m_bits |= other.m_bits; }
84
90 constexpr void clear(const BitField& other) { m_bits &= ~other.m_bits; }
91
95 constexpr void clear_all() { m_bits = UnderlyingType(0); }
96
104 constexpr bool test(const BitField& other) const
105 {
106 return (m_bits & other.m_bits) == other.m_bits;
107 }
108
116 constexpr bool test_any(const BitField& other) const { return (m_bits & other.m_bits) != 0; }
117
124 constexpr void set_bit(const BitField& other, bool is_set)
125 {
126 if (is_set) {
127 set(other);
128 } else {
129 clear(other);
130 }
131 }
132
136 constexpr operator UnderlyingType() const { return m_bits; }
137
143 constexpr const UnderlyingType& get_value() const { return m_bits; }
144
152 constexpr bool operator==(const BitField& other) const { return m_bits == other.m_bits; }
153
161 constexpr bool operator!=(const BitField& other) const { return m_bits != other.m_bits; }
162
170 constexpr BitField operator|(const BitField& other) const
171 {
172 return BitField(m_bits | other.m_bits);
173 }
174
182 constexpr BitField operator&(const BitField& other) const
183 {
184 return BitField(m_bits & other.m_bits);
185 }
186
194 constexpr BitField operator^(const BitField& other) const
195 {
196 return BitField(m_bits ^ other.m_bits);
197 }
198
204 constexpr BitField operator~() const { return BitField(~m_bits); }
205
206private:
207 static_assert(std::is_enum_v<EnumType>);
208
209 UnderlyingType m_bits = UnderlyingType(0);
210};
211
213
214} // namespace lagrange
Bit field utility class.
Definition: BitField.h:33
constexpr BitField operator^(const BitField &other) const
Bitwise 'exclusive or' operator.
Definition: BitField.h:194
constexpr BitField(EnumType value)
Constructor.
Definition: BitField.h:51
std::underlying_type_t< EnumType > UnderlyingType
Underlying integral type representing the enum type.
Definition: BitField.h:39
constexpr void set_bit(const BitField &other, bool is_set)
Allow to set the value of a bit.
Definition: BitField.h:124
constexpr BitField(UnderlyingType bits)
Constructor.
Definition: BitField.h:60
constexpr const UnderlyingType & get_value() const
Gets the underlying integral value.
Definition: BitField.h:143
constexpr bool operator!=(const BitField &other) const
Different operator.
Definition: BitField.h:161
constexpr void set(const BitField &other)
Set to 1 the specified bit.
Definition: BitField.h:83
constexpr BitField operator&(const BitField &other) const
Bitwise 'and' operator.
Definition: BitField.h:182
constexpr bool test_any(const BitField &other) const
Test if any bits are set.
Definition: BitField.h:116
constexpr bool test(const BitField &other) const
Test the specified bit.
Definition: BitField.h:104
constexpr void clear(const BitField &other)
Set to 0 the specified Bit.
Definition: BitField.h:90
constexpr void clear_all()
Set all bits to 0.
Definition: BitField.h:95
static constexpr BitField all()
Named constructor returning a bitfield set to 1.
Definition: BitField.h:76
constexpr bool operator==(const BitField &other) const
Equal operator.
Definition: BitField.h:152
constexpr BitField()
Default Constructor.
Definition: BitField.h:42
constexpr BitField operator~() const
Bitwise 'one's complement' operator.
Definition: BitField.h:204
EnumType_ EnumType
Enum type on top of which the bit field is built.
Definition: BitField.h:36
constexpr BitField operator|(const BitField &other) const
Bitwise 'or' operator.
Definition: BitField.h:170
static constexpr BitField none()
Named constructor returning a bitfield set to 0.
Definition: BitField.h:69
Main namespace for Lagrange.
Definition: AABBIGL.h:30