xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Utility/Flags.h (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
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] flags
33   ///     The initial value for all flags.
m_flags(flags)34   Flags(ValueType flags = 0) : m_flags(flags) {}
35 
36   /// Get accessor for all flags.
37   ///
38   /// \return
39   ///     Returns all of the flags as a Flags::ValueType.
Get()40   ValueType Get() const { return m_flags; }
41 
42   /// Return the number of flags that can be represented in this object.
43   ///
44   /// \return
45   ///     The maximum number bits in this flag object.
GetBitSize()46   size_t GetBitSize() const { return sizeof(ValueType) * 8; }
47 
48   /// Set accessor for all flags.
49   ///
50   /// \param[in] flags
51   ///     The bits with which to replace all of the current flags.
Reset(ValueType flags)52   void Reset(ValueType flags) { m_flags = flags; }
53 
54   /// Clear one or more flags.
55   ///
56   /// \param[in] mask
57   ///     A bitfield containing one or more flags.
58   ///
59   /// \return
60   ///     The new flags after clearing all bits from \a mask.
61   ValueType Clear(ValueType mask = ~static_cast<ValueType>(0)) {
62     m_flags &= ~mask;
63     return m_flags;
64   }
65 
66   /// Set one or more flags by logical OR'ing \a mask with the current flags.
67   ///
68   /// \param[in] mask
69   ///     A bitfield containing one or more flags.
70   ///
71   /// \return
72   ///     The new flags after setting all bits from \a mask.
Set(ValueType mask)73   ValueType Set(ValueType mask) {
74     m_flags |= mask;
75     return m_flags;
76   }
77 
78   /// Test if all bits in \a mask are 1 in the current flags
79   ///
80   /// \return
81   ///     \b true if all flags in \a mask are 1, \b false
82   ///     otherwise.
AllSet(ValueType mask)83   bool AllSet(ValueType mask) const { return (m_flags & mask) == mask; }
84 
85   /// Test one or more flags.
86   ///
87   /// \return
88   ///     \b true if any flags in \a mask are 1, \b false
89   ///     otherwise.
AnySet(ValueType mask)90   bool AnySet(ValueType mask) const { return (m_flags & mask) != 0; }
91 
92   /// Test a single flag bit.
93   ///
94   /// \return
95   ///     \b true if \a bit is set, \b false otherwise.
Test(ValueType bit)96   bool Test(ValueType bit) const { return (m_flags & bit) != 0; }
97 
98   /// Test if all bits in \a mask are clear.
99   ///
100   /// \return
101   ///     \b true if \b all flags in \a mask are clear, \b false
102   ///     otherwise.
AllClear(ValueType mask)103   bool AllClear(ValueType mask) const { return (m_flags & mask) == 0; }
104 
AnyClear(ValueType mask)105   bool AnyClear(ValueType mask) const { return (m_flags & mask) != mask; }
106 
107   /// Test a single flag bit to see if it is clear (zero).
108   ///
109   /// \return
110   ///     \b true if \a bit is 0, \b false otherwise.
IsClear(ValueType bit)111   bool IsClear(ValueType bit) const { return (m_flags & bit) == 0; }
112 
113 protected:
114   ValueType m_flags; ///< The flags.
115 };
116 
117 } // namespace lldb_private
118 
119 #endif // LLDB_UTILITY_FLAGS_H
120