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