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 /// A class to manage flags.
22 /// The Flags class managed flag bits and allows testing and modification of
23 /// 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 of the
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 object.
70 /// The maximum number bits in this flag object.
71 //----------------------------------------------------------------------
72 size_t GetBitSize() const { return sizeof(ValueType) * 8; }
74 //----------------------------------------------------------------------
75 /// Set accessor for all flags.
78 /// The bits with which to replace all of the current flags.
79 //----------------------------------------------------------------------
80 void Reset(ValueType flags) { m_flags = flags; }
82 //----------------------------------------------------------------------
83 /// Clear one or more flags.
86 /// A bitfield containing one or more flags.
89 /// The new flags after clearing all bits from \a mask.
90 //----------------------------------------------------------------------
91 ValueType Clear(ValueType mask = ~(ValueType)0) {
96 //----------------------------------------------------------------------
97 /// Set one or more flags by logical OR'ing \a mask with the current flags.
100 /// A bitfield containing one or more flags.
103 /// The new flags after setting all bits from \a mask.
104 //----------------------------------------------------------------------
105 ValueType Set(ValueType mask) {
110 //----------------------------------------------------------------------
111 /// Test if all bits in \a mask are 1 in the current flags
114 /// \b true if all flags in \a mask are 1, \b false
116 //----------------------------------------------------------------------
117 bool AllSet(ValueType mask) const { return (m_flags & mask) == mask; }
119 //----------------------------------------------------------------------
120 /// Test one or more flags.
123 /// \b true if any flags in \a mask are 1, \b false
125 //----------------------------------------------------------------------
126 bool AnySet(ValueType mask) const { return (m_flags & mask) != 0; }
128 //----------------------------------------------------------------------
129 /// Test a single flag bit.
132 /// \b true if \a bit is set, \b false otherwise.
133 //----------------------------------------------------------------------
134 bool Test(ValueType bit) const { return (m_flags & bit) != 0; }
136 //----------------------------------------------------------------------
137 /// Test if all bits in \a mask are clear.
140 /// \b true if \b all flags in \a mask are clear, \b false
142 //----------------------------------------------------------------------
143 bool AllClear(ValueType mask) const { return (m_flags & mask) == 0; }
145 bool AnyClear(ValueType mask) const { return (m_flags & mask) != mask; }
147 //----------------------------------------------------------------------
148 /// Test a single flag bit to see if it is clear (zero).
151 /// \b true if \a bit is 0, \b false otherwise.
152 //----------------------------------------------------------------------
153 bool IsClear(ValueType bit) const { return (m_flags & bit) == 0; }
155 //----------------------------------------------------------------------
156 /// Get the number of zero bits in \a m_flags.
159 /// The number of bits that are set to 0 in the current flags.
160 //----------------------------------------------------------------------
161 size_t ClearCount() const {
163 for (ValueType shift = 0; shift < sizeof(ValueType) * 8; ++shift) {
164 if ((m_flags & (1u << shift)) == 0)
170 //----------------------------------------------------------------------
171 /// Get the number of one bits in \a m_flags.
174 /// The number of bits that are set to 1 in the current flags.
175 //----------------------------------------------------------------------
176 size_t SetCount() const {
178 for (ValueType mask = m_flags; mask; mask >>= 1) {
186 ValueType m_flags; ///< The flags.
189 } // namespace lldb_private
191 #endif // liblldb_Flags_h_