]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/Flags.h
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[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 /// @brief A class to manage flags.
21 ///
22 /// The Flags class managed flag bits and allows testing and
23 /// modification of 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
36   /// of the 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
68   /// object.
69   ///
70   /// @return
71   ///     The maximum number bits in this flag object.
72   //----------------------------------------------------------------------
73   size_t GetBitSize() const { return sizeof(ValueType) * 8; }
74
75   //----------------------------------------------------------------------
76   /// Set accessor for all flags.
77   ///
78   /// @param[in] flags
79   ///     The bits with which to replace all of the current flags.
80   //----------------------------------------------------------------------
81   void Reset(ValueType flags) { m_flags = flags; }
82
83   //----------------------------------------------------------------------
84   /// Clear one or more flags.
85   ///
86   /// @param[in] mask
87   ///     A bitfield containing one or more flags.
88   ///
89   /// @return
90   ///     The new flags after clearing all bits from \a mask.
91   //----------------------------------------------------------------------
92   ValueType Clear(ValueType mask = ~(ValueType)0) {
93     m_flags &= ~mask;
94     return m_flags;
95   }
96
97   //----------------------------------------------------------------------
98   /// Set one or more flags by logical OR'ing \a mask with the current
99   /// flags.
100   ///
101   /// @param[in] mask
102   ///     A bitfield containing one or more flags.
103   ///
104   /// @return
105   ///     The new flags after setting all bits from \a mask.
106   //----------------------------------------------------------------------
107   ValueType Set(ValueType mask) {
108     m_flags |= mask;
109     return m_flags;
110   }
111
112   //----------------------------------------------------------------------
113   /// Test if all bits in \a mask are 1 in the current flags
114   ///
115   /// @return
116   ///     \b true if all flags in \a mask are 1, \b false
117   ///     otherwise.
118   //----------------------------------------------------------------------
119   bool AllSet(ValueType mask) const { return (m_flags & mask) == mask; }
120
121   //----------------------------------------------------------------------
122   /// Test one or more flags.
123   ///
124   /// @return
125   ///     \b true if any flags in \a mask are 1, \b false
126   ///     otherwise.
127   //----------------------------------------------------------------------
128   bool AnySet(ValueType mask) const { return (m_flags & mask) != 0; }
129
130   //----------------------------------------------------------------------
131   /// Test a single flag bit.
132   ///
133   /// @return
134   ///     \b true if \a bit is set, \b false otherwise.
135   //----------------------------------------------------------------------
136   bool Test(ValueType bit) const { return (m_flags & bit) != 0; }
137
138   //----------------------------------------------------------------------
139   /// Test if all bits in \a mask are clear.
140   ///
141   /// @return
142   ///     \b true if \b all flags in \a mask are clear, \b false
143   ///     otherwise.
144   //----------------------------------------------------------------------
145   bool AllClear(ValueType mask) const { return (m_flags & mask) == 0; }
146
147   bool AnyClear(ValueType mask) const { return (m_flags & mask) != mask; }
148
149   //----------------------------------------------------------------------
150   /// Test a single flag bit to see if it is clear (zero).
151   ///
152   /// @return
153   ///     \b true if \a bit is 0, \b false otherwise.
154   //----------------------------------------------------------------------
155   bool IsClear(ValueType bit) const { return (m_flags & bit) == 0; }
156
157   //----------------------------------------------------------------------
158   /// Get the number of zero bits in \a m_flags.
159   ///
160   /// @return
161   ///     The number of bits that are set to 0 in the current flags.
162   //----------------------------------------------------------------------
163   size_t ClearCount() const {
164     size_t count = 0;
165     for (ValueType shift = 0; shift < sizeof(ValueType) * 8; ++shift) {
166       if ((m_flags & (1u << shift)) == 0)
167         ++count;
168     }
169     return count;
170   }
171
172   //----------------------------------------------------------------------
173   /// Get the number of one bits in \a m_flags.
174   ///
175   /// @return
176   ///     The number of bits that are set to 1 in the current flags.
177   //----------------------------------------------------------------------
178   size_t SetCount() const {
179     size_t count = 0;
180     for (ValueType mask = m_flags; mask; mask >>= 1) {
181       if (mask & 1u)
182         ++count;
183     }
184     return count;
185   }
186
187 protected:
188   ValueType m_flags; ///< The flags.
189 };
190
191 } // namespace lldb_private
192
193 #endif // liblldb_Flags_h_