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 liblldb_Flags_h_ 11 #define liblldb_Flags_h_ 12 #if defined(__cplusplus) 13 14 15 #include <stdint.h> 16 17 namespace lldb_private { 18 19 //---------------------------------------------------------------------- 20 /// @class Flags Flags.h "lldb/Core/Flags.h" 21 /// @brief A class to manage flags. 22 /// 23 /// The Flags class managed flag bits and allows testing and 24 /// modification of individual or multiple flag bits. 25 //---------------------------------------------------------------------- 26 class Flags 27 { 28 public: 29 //---------------------------------------------------------------------- 30 /// The value type for flags is a 32 bit unsigned integer type. 31 //---------------------------------------------------------------------- 32 typedef uint32_t ValueType; 33 34 //---------------------------------------------------------------------- 35 /// Construct with initial flag bit values. 36 /// 37 /// Constructs this object with \a mask as the initial value for all 38 /// of the flags. 39 /// 40 /// @param[in] mask 41 /// The initial value for all flags. 42 //---------------------------------------------------------------------- 43 Flags (ValueType flags = 0) : m_flags(flags)44 m_flags (flags) 45 { 46 } 47 48 //---------------------------------------------------------------------- 49 /// Copy constructor. 50 /// 51 /// Construct and copy the flags from \a rhs. 52 /// 53 /// @param[in] rhs 54 /// A const Flags object reference to copy. 55 //---------------------------------------------------------------------- Flags(const Flags & rhs)56 Flags (const Flags& rhs) : 57 m_flags(rhs.m_flags) 58 { 59 } 60 61 //---------------------------------------------------------------------- 62 /// Destructor. 63 //---------------------------------------------------------------------- ~Flags()64 ~Flags () 65 { 66 } 67 68 //---------------------------------------------------------------------- 69 /// Get accessor for all flags. 70 /// 71 /// @return 72 /// Returns all of the flags as a Flags::ValueType. 73 //---------------------------------------------------------------------- 74 ValueType Get()75 Get () const 76 { 77 return m_flags; 78 } 79 80 //---------------------------------------------------------------------- 81 /// Return the number of flags that can be represented in this 82 /// object. 83 /// 84 /// @return 85 /// The maximum number bits in this flag object. 86 //---------------------------------------------------------------------- 87 size_t GetBitSize()88 GetBitSize() const 89 { 90 return sizeof (ValueType) * 8; 91 } 92 93 //---------------------------------------------------------------------- 94 /// Set accessor for all flags. 95 /// 96 /// @param[in] flags 97 /// The bits with which to replace all of the current flags. 98 //---------------------------------------------------------------------- 99 void Reset(ValueType flags)100 Reset (ValueType flags) 101 { 102 m_flags = flags; 103 } 104 105 //---------------------------------------------------------------------- 106 /// Clear one or more flags. 107 /// 108 /// @param[in] mask 109 /// A bitfield containing one or more flags. 110 /// 111 /// @return 112 /// The new flags after clearing all bits from \a mask. 113 //---------------------------------------------------------------------- 114 ValueType 115 Clear (ValueType mask = ~(ValueType)0) 116 { 117 m_flags &= ~mask; 118 return m_flags; 119 } 120 121 122 //---------------------------------------------------------------------- 123 /// Set one or more flags by logical OR'ing \a mask with the current 124 /// flags. 125 /// 126 /// @param[in] mask 127 /// A bitfield containing one or more flags. 128 /// 129 /// @return 130 /// The new flags after setting all bits from \a mask. 131 //---------------------------------------------------------------------- 132 ValueType Set(ValueType mask)133 Set (ValueType mask) 134 { 135 m_flags |= mask; 136 return m_flags; 137 } 138 139 140 //---------------------------------------------------------------------- 141 /// Test if all bits in \a mask are 1 in the current flags 142 /// 143 /// @return 144 /// \b true if all flags in \a mask are 1, \b false 145 /// otherwise. 146 //---------------------------------------------------------------------- 147 bool AllSet(ValueType mask)148 AllSet (ValueType mask) const 149 { 150 return (m_flags & mask) == mask; 151 } 152 153 //---------------------------------------------------------------------- 154 /// Test one or more flags. 155 /// 156 /// @return 157 /// \b true if any flags in \a mask are 1, \b false 158 /// otherwise. 159 //---------------------------------------------------------------------- 160 bool AnySet(ValueType mask)161 AnySet (ValueType mask) const 162 { 163 return (m_flags & mask) != 0; 164 } 165 166 //---------------------------------------------------------------------- 167 /// Test a single flag bit. 168 /// 169 /// @return 170 /// \b true if \a bit is set, \b false otherwise. 171 //---------------------------------------------------------------------- 172 bool Test(ValueType bit)173 Test (ValueType bit) const 174 { 175 return (m_flags & bit) != 0; 176 } 177 178 //---------------------------------------------------------------------- 179 /// Test if all bits in \a mask are clear. 180 /// 181 /// @return 182 /// \b true if \b all flags in \a mask are clear, \b false 183 /// otherwise. 184 //---------------------------------------------------------------------- 185 bool AllClear(ValueType mask)186 AllClear (ValueType mask) const 187 { 188 return (m_flags & mask) == 0; 189 } 190 191 bool AnyClear(ValueType mask)192 AnyClear (ValueType mask) const 193 { 194 return (m_flags & mask) != mask; 195 } 196 197 //---------------------------------------------------------------------- 198 /// Test a single flag bit to see if it is clear (zero). 199 /// 200 /// @return 201 /// \b true if \a bit is 0, \b false otherwise. 202 //---------------------------------------------------------------------- 203 bool IsClear(ValueType bit)204 IsClear (ValueType bit) const 205 { 206 return (m_flags & bit) == 0; 207 } 208 209 //---------------------------------------------------------------------- 210 /// Get the number of zero bits in \a m_flags. 211 /// 212 /// @return 213 /// The number of bits that are set to 0 in the current flags. 214 //---------------------------------------------------------------------- 215 size_t ClearCount()216 ClearCount () const 217 { 218 size_t count = 0; 219 for (ValueType shift = 0; shift < sizeof(ValueType)*8; ++shift) 220 { 221 if ((m_flags & (1u << shift)) == 0) 222 ++count; 223 } 224 return count; 225 } 226 227 //---------------------------------------------------------------------- 228 /// Get the number of one bits in \a m_flags. 229 /// 230 /// @return 231 /// The number of bits that are set to 1 in the current flags. 232 //---------------------------------------------------------------------- 233 size_t SetCount()234 SetCount () const 235 { 236 size_t count = 0; 237 for (ValueType mask = m_flags; mask; mask >>= 1) 238 { 239 if (mask & 1u) 240 ++count; 241 } 242 return count; 243 } 244 245 protected: 246 ValueType m_flags; ///< The flags. 247 }; 248 249 } // namespace lldb_private 250 251 #endif // #if defined(__cplusplus) 252 #endif // liblldb_Flags_h_ 253