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