1 //===-- Flags.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_UTILITY_FLAGS_H
11 #define LLDB_UTILITY_FLAGS_H
16 namespace lldb_private {
18 //----------------------------------------------------------------------
19 /// @class Flags Flags.h "lldb/Utility/Flags.h"
20 /// @brief A class to manage flags.
22 /// The Flags class managed flag bits and allows testing and
23 /// modification of individual or multiple flag bits.
24 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 /// The value type for flags is a 32 bit unsigned integer type.
29 //----------------------------------------------------------------------
30 typedef uint32_t ValueType;
32 //----------------------------------------------------------------------
33 /// Construct with initial flag bit values.
35 /// Constructs this object with \a mask as the initial value for all
39 /// The initial value for all flags.
40 //----------------------------------------------------------------------
41 Flags(ValueType flags = 0) : m_flags(flags) {}
43 //----------------------------------------------------------------------
46 /// Construct and copy the flags from \a rhs.
49 /// A const Flags object reference to copy.
50 //----------------------------------------------------------------------
51 Flags(const Flags &rhs) : m_flags(rhs.m_flags) {}
53 //----------------------------------------------------------------------
55 //----------------------------------------------------------------------
58 //----------------------------------------------------------------------
59 /// Get accessor for all flags.
62 /// Returns all of the flags as a Flags::ValueType.
63 //----------------------------------------------------------------------
64 ValueType Get() const { return m_flags; }
66 //----------------------------------------------------------------------
67 /// Return the number of flags that can be represented in this
71 /// The maximum number bits in this flag object.
72 //----------------------------------------------------------------------
73 size_t GetBitSize() const { return sizeof(ValueType) * 8; }
75 //----------------------------------------------------------------------
76 /// Set accessor for all flags.
79 /// The bits with which to replace all of the current flags.
80 //----------------------------------------------------------------------
81 void Reset(ValueType flags) { m_flags = flags; }
83 //----------------------------------------------------------------------
84 /// Clear one or more flags.
87 /// A bitfield containing one or more flags.
90 /// The new flags after clearing all bits from \a mask.
91 //----------------------------------------------------------------------
92 ValueType Clear(ValueType mask = ~(ValueType)0) {
97 //----------------------------------------------------------------------
98 /// Set one or more flags by logical OR'ing \a mask with the current
102 /// A bitfield containing one or more flags.
105 /// The new flags after setting all bits from \a mask.
106 //----------------------------------------------------------------------
107 ValueType Set(ValueType mask) {
112 //----------------------------------------------------------------------
113 /// Test if all bits in \a mask are 1 in the current flags
116 /// \b true if all flags in \a mask are 1, \b false
118 //----------------------------------------------------------------------
119 bool AllSet(ValueType mask) const { return (m_flags & mask) == mask; }
121 //----------------------------------------------------------------------
122 /// Test one or more flags.
125 /// \b true if any flags in \a mask are 1, \b false
127 //----------------------------------------------------------------------
128 bool AnySet(ValueType mask) const { return (m_flags & mask) != 0; }
130 //----------------------------------------------------------------------
131 /// Test a single flag bit.
134 /// \b true if \a bit is set, \b false otherwise.
135 //----------------------------------------------------------------------
136 bool Test(ValueType bit) const { return (m_flags & bit) != 0; }
138 //----------------------------------------------------------------------
139 /// Test if all bits in \a mask are clear.
142 /// \b true if \b all flags in \a mask are clear, \b false
144 //----------------------------------------------------------------------
145 bool AllClear(ValueType mask) const { return (m_flags & mask) == 0; }
147 bool AnyClear(ValueType mask) const { return (m_flags & mask) != mask; }
149 //----------------------------------------------------------------------
150 /// Test a single flag bit to see if it is clear (zero).
153 /// \b true if \a bit is 0, \b false otherwise.
154 //----------------------------------------------------------------------
155 bool IsClear(ValueType bit) const { return (m_flags & bit) == 0; }
157 //----------------------------------------------------------------------
158 /// Get the number of zero bits in \a m_flags.
161 /// The number of bits that are set to 0 in the current flags.
162 //----------------------------------------------------------------------
163 size_t ClearCount() const {
165 for (ValueType shift = 0; shift < sizeof(ValueType) * 8; ++shift) {
166 if ((m_flags & (1u << shift)) == 0)
172 //----------------------------------------------------------------------
173 /// Get the number of one bits in \a m_flags.
176 /// The number of bits that are set to 1 in the current flags.
177 //----------------------------------------------------------------------
178 size_t SetCount() const {
180 for (ValueType mask = m_flags; mask; mask >>= 1) {
188 ValueType m_flags; ///< The flags.
191 } // namespace lldb_private
193 #endif // liblldb_Flags_h_