1 //===-- Flags.h -------------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UTILITY_FLAGS_H
10 #define LLDB_UTILITY_FLAGS_H
15 namespace lldb_private {
17 /// \class Flags Flags.h "lldb/Utility/Flags.h"
18 /// A class to manage flags.
20 /// The Flags class managed flag bits and allows testing and modification of
21 /// individual or multiple flag bits.
24 /// The value type for flags is a 32 bit unsigned integer type.
25 typedef uint32_t ValueType;
27 /// Construct with initial flag bit values.
29 /// Constructs this object with \a mask as the initial value for all of the
33 /// The initial value for all flags.
34 Flags(ValueType flags = 0) : m_flags(flags) {}
38 /// Construct and copy the flags from \a rhs.
41 /// A const Flags object reference to copy.
42 Flags(const Flags &rhs) : m_flags(rhs.m_flags) {}
47 /// Get accessor for all flags.
50 /// Returns all of the flags as a Flags::ValueType.
51 ValueType Get() const { return m_flags; }
53 /// Return the number of flags that can be represented in this object.
56 /// The maximum number bits in this flag object.
57 size_t GetBitSize() const { return sizeof(ValueType) * 8; }
59 /// Set accessor for all flags.
62 /// The bits with which to replace all of the current flags.
63 void Reset(ValueType flags) { m_flags = flags; }
65 /// Clear one or more flags.
68 /// A bitfield containing one or more flags.
71 /// The new flags after clearing all bits from \a mask.
72 ValueType Clear(ValueType mask = ~static_cast<ValueType>(0)) {
77 /// Set one or more flags by logical OR'ing \a mask with the current flags.
80 /// A bitfield containing one or more flags.
83 /// The new flags after setting all bits from \a mask.
84 ValueType Set(ValueType mask) {
89 /// Test if all bits in \a mask are 1 in the current flags
92 /// \b true if all flags in \a mask are 1, \b false
94 bool AllSet(ValueType mask) const { return (m_flags & mask) == mask; }
96 /// Test one or more flags.
99 /// \b true if any flags in \a mask are 1, \b false
101 bool AnySet(ValueType mask) const { return (m_flags & mask) != 0; }
103 /// Test a single flag bit.
106 /// \b true if \a bit is set, \b false otherwise.
107 bool Test(ValueType bit) const { return (m_flags & bit) != 0; }
109 /// Test if all bits in \a mask are clear.
112 /// \b true if \b all flags in \a mask are clear, \b false
114 bool AllClear(ValueType mask) const { return (m_flags & mask) == 0; }
116 bool AnyClear(ValueType mask) const { return (m_flags & mask) != mask; }
118 /// Test a single flag bit to see if it is clear (zero).
121 /// \b true if \a bit is 0, \b false otherwise.
122 bool IsClear(ValueType bit) const { return (m_flags & bit) == 0; }
124 /// Get the number of zero bits in \a m_flags.
127 /// The number of bits that are set to 0 in the current flags.
128 size_t ClearCount() const {
130 for (ValueType shift = 0; shift < sizeof(ValueType) * 8; ++shift) {
131 if ((m_flags & (1u << shift)) == 0)
137 /// Get the number of one bits in \a m_flags.
140 /// The number of bits that are set to 1 in the current flags.
141 size_t SetCount() const {
143 for (ValueType mask = m_flags; mask; mask >>= 1) {
151 ValueType m_flags; ///< The flags.
154 } // namespace lldb_private
156 #endif // liblldb_Flags_h_