]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/Flags.h
MFV r347136:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / Flags.h
1 //===-- Flags.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLDB_UTILITY_FLAGS_H
11 #define LLDB_UTILITY_FLAGS_H
12
13 #include <cstddef>
14 #include <cstdint>
15
16 namespace lldb_private {
17
18 //----------------------------------------------------------------------
19 /// @class Flags Flags.h "lldb/Utility/Flags.h"
20 /// A class to manage flags.
21 ///
22 /// The Flags class managed flag bits and allows testing and modification of
23 /// individual or multiple flag bits.
24 //----------------------------------------------------------------------
25 class Flags {
26 public:
27   //----------------------------------------------------------------------
28   /// The value type for flags is a 32 bit unsigned integer type.
29   //----------------------------------------------------------------------
30   typedef uint32_t ValueType;
31
32   //----------------------------------------------------------------------
33   /// Construct with initial flag bit values.
34   ///
35   /// Constructs this object with \a mask as the initial value for all of the
36   /// flags.
37   ///
38   /// @param[in] mask
39   ///     The initial value for all flags.
40   //----------------------------------------------------------------------
41   Flags(ValueType flags = 0) : m_flags(flags) {}
42
43   //----------------------------------------------------------------------
44   /// Copy constructor.
45   ///
46   /// Construct and copy the flags from \a rhs.
47   ///
48   /// @param[in] rhs
49   ///     A const Flags object reference to copy.
50   //----------------------------------------------------------------------
51   Flags(const Flags &rhs) : m_flags(rhs.m_flags) {}
52
53   //----------------------------------------------------------------------
54   /// Destructor.
55   //----------------------------------------------------------------------
56   ~Flags() {}
57
58   //----------------------------------------------------------------------
59   /// Get accessor for all flags.
60   ///
61   /// @return
62   ///     Returns all of the flags as a Flags::ValueType.
63   //----------------------------------------------------------------------
64   ValueType Get() const { return m_flags; }
65
66   //----------------------------------------------------------------------
67   /// Return the number of flags that can be represented in this object.
68   ///
69   /// @return
70   ///     The maximum number bits in this flag object.
71   //----------------------------------------------------------------------
72   size_t GetBitSize() const { return sizeof(ValueType) * 8; }
73
74   //----------------------------------------------------------------------
75   /// Set accessor for all flags.
76   ///
77   /// @param[in] flags
78   ///     The bits with which to replace all of the current flags.
79   //----------------------------------------------------------------------
80   void Reset(ValueType flags) { m_flags = flags; }
81
82   //----------------------------------------------------------------------
83   /// Clear one or more flags.
84   ///
85   /// @param[in] mask
86   ///     A bitfield containing one or more flags.
87   ///
88   /// @return
89   ///     The new flags after clearing all bits from \a mask.
90   //----------------------------------------------------------------------
91   ValueType Clear(ValueType mask = ~(ValueType)0) {
92     m_flags &= ~mask;
93     return m_flags;
94   }
95
96   //----------------------------------------------------------------------
97   /// Set one or more flags by logical OR'ing \a mask with the current flags.
98   ///
99   /// @param[in] mask
100   ///     A bitfield containing one or more flags.
101   ///
102   /// @return
103   ///     The new flags after setting all bits from \a mask.
104   //----------------------------------------------------------------------
105   ValueType Set(ValueType mask) {
106     m_flags |= mask;
107     return m_flags;
108   }
109
110   //----------------------------------------------------------------------
111   /// Test if all bits in \a mask are 1 in the current flags
112   ///
113   /// @return
114   ///     \b true if all flags in \a mask are 1, \b false
115   ///     otherwise.
116   //----------------------------------------------------------------------
117   bool AllSet(ValueType mask) const { return (m_flags & mask) == mask; }
118
119   //----------------------------------------------------------------------
120   /// Test one or more flags.
121   ///
122   /// @return
123   ///     \b true if any flags in \a mask are 1, \b false
124   ///     otherwise.
125   //----------------------------------------------------------------------
126   bool AnySet(ValueType mask) const { return (m_flags & mask) != 0; }
127
128   //----------------------------------------------------------------------
129   /// Test a single flag bit.
130   ///
131   /// @return
132   ///     \b true if \a bit is set, \b false otherwise.
133   //----------------------------------------------------------------------
134   bool Test(ValueType bit) const { return (m_flags & bit) != 0; }
135
136   //----------------------------------------------------------------------
137   /// Test if all bits in \a mask are clear.
138   ///
139   /// @return
140   ///     \b true if \b all flags in \a mask are clear, \b false
141   ///     otherwise.
142   //----------------------------------------------------------------------
143   bool AllClear(ValueType mask) const { return (m_flags & mask) == 0; }
144
145   bool AnyClear(ValueType mask) const { return (m_flags & mask) != mask; }
146
147   //----------------------------------------------------------------------
148   /// Test a single flag bit to see if it is clear (zero).
149   ///
150   /// @return
151   ///     \b true if \a bit is 0, \b false otherwise.
152   //----------------------------------------------------------------------
153   bool IsClear(ValueType bit) const { return (m_flags & bit) == 0; }
154
155   //----------------------------------------------------------------------
156   /// Get the number of zero bits in \a m_flags.
157   ///
158   /// @return
159   ///     The number of bits that are set to 0 in the current flags.
160   //----------------------------------------------------------------------
161   size_t ClearCount() const {
162     size_t count = 0;
163     for (ValueType shift = 0; shift < sizeof(ValueType) * 8; ++shift) {
164       if ((m_flags & (1u << shift)) == 0)
165         ++count;
166     }
167     return count;
168   }
169
170   //----------------------------------------------------------------------
171   /// Get the number of one bits in \a m_flags.
172   ///
173   /// @return
174   ///     The number of bits that are set to 1 in the current flags.
175   //----------------------------------------------------------------------
176   size_t SetCount() const {
177     size_t count = 0;
178     for (ValueType mask = m_flags; mask; mask >>= 1) {
179       if (mask & 1u)
180         ++count;
181     }
182     return count;
183   }
184
185 protected:
186   ValueType m_flags; ///< The flags.
187 };
188
189 } // namespace lldb_private
190
191 #endif // liblldb_Flags_h_